Discord机器人仅在DM中响应,服务器频道无反应问题求助
解决Discord机器人仅在DM响应、服务器频道无反应的问题
核心问题及解决方案
- 缺少消息内容读取权限
Discord当前要求显式开启message_content意图才能读取服务器频道的消息内容,默认的discord.Intents.default()不包含该权限,这是机器人在服务器频道无响应的主要原因。需要在初始化客户端时添加该意图。 - 语法错误
代码中command_prefix = ">未闭合引号,会导致代码运行失败,需补充完整。
修正后的完整代码
import discord # 优化token读取逻辑,避免不必要的类型转换 with open("bot_token.txt", "r") as token_file: TOKEN = token_file.read().strip() command_prefix = ">" # 修复引号闭合问题 # 配置所需的Intents intents = discord.Intents.default() intents.message_content = True # 关键:添加消息内容读取权限 client = discord.Client(intents=intents) @client.event async def on_ready(): print("Logged in as: {0.user}".format(client)) @client.event async def on_message(message): if message.author == client.user: return if message.content.startswith(command_prefix): if message.content == ">help": await message.channel.send("What do you need help with?") elif message.content == ">hello": await message.channel.send("Hi there!") else: await message.channel.send("This command is not recognised by our bot, please use the help menu if required.") client.run(TOKEN)
额外注意事项
除了代码中开启意图,还需要在Discord开发者门户的机器人页面,找到"Privileged Gateway Intents"部分,开启"Message Content Intent"权限,否则机器人仍无法读取消息内容。
内容的提问来源于stack exchange,提问作者TheEmeraldFishy
相关产品推荐
相关产品推荐

