You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人可检测消息却不回复,附代码求技术解决

解决Discord机器人无法回复消息的问题

你的机器人能检测到消息但无法回复,核心原因是消息内容特权意图未开启,以及缺少自身消息过滤逻辑导致潜在限制。以下是具体解决步骤:

1. 启用Message Content特权意图

Discord.py v2.0及以上版本中,message_content属于特权意图,必须同时在代码和Discord开发者平台开启,否则机器人无法正常处理消息收发操作。

修改代码中的Intents配置:

intents = discord.Intents.default()
intents.message_content = True  # 开启消息内容意图
client = discord.Client(intents=intents)

同时在Discord开发者平台完成设置:

  • 进入你的Discord应用页面,打开Bot标签页
  • 在「Privileged Gateway Intents」区域,找到「Message Content Intent」并开启
  • 点击页面底部的「Save Changes」保存设置

2. 过滤机器人自身消息

如果不判断消息发送者,机器人会回复自己发出的消息,引发无限循环,甚至被Discord API限制发送请求。在on_message事件中添加过滤逻辑:

@client.event
async def on_message(message):
    # 忽略机器人自身发送的消息
    if message.author == client.user:
        return
    print("Works here, too")
    await message.channel.send('Bot is online and functioning properly.')

完整修正后的代码

import os
import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'{client.user.name} has connected to Discord!')
    print("Code is Working")

@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )
    print("What do do?")

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    print("Works here, too")
    await message.channel.send('Bot is online and functioning properly.')

client.run(TOKEN)

完成以上修改后重启机器人,即可正常回复消息。

内容的提问来源于stack exchange,提问作者David Hammond

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 13:18:16