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

Python开发的Discord Bot在线但无交互问题求助

问题排查与解决方案

1. 缺少消息内容权限(Message Content Intent)

Discord.py 2.0及以上版本要求必须开启message_content意图才能读取消息内容,你的代码未配置该权限,导致机器人无法获取消息文本,自然无法响应指令。

修改intents配置:

intents = discord.Intents.default()
intents.members = True
intents.message_content = True  # 新增该行开启消息内容权限

同时需前往Discord开发者平台的机器人页面,在「Privileged Gateway Intents」板块手动开启Message Content Intent。

2. 未排除机器人自身消息

若机器人发送的消息触发on_message事件,可能引发循环或干扰正常响应,建议在事件开头添加过滤逻辑:

@client.event
async def on_message(message):
    if message.author == client.user:  # 忽略机器人自己发送的消息
        return
    if message.content == "hello":
        await message.channel.send("Hello there!")

3. 消息匹配过于严格

你的代码是精确匹配字符串"hello",如果消息带有空格、大小写差异(如"Hello"或"hello ")都不会触发响应,可改为宽松匹配:

if message.content.strip().lower() == "hello":

完整修正代码

import discord

intents = discord.Intents.default()
intents.members = True
intents.message_content = True

client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.strip().lower() == "hello":
        await message.channel.send("Hello there!")

client.run('BOT TOKEN')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 14:15:25