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

PyCharm中Python编写的Discord Bot匹配Hello无响应问题

Discord Bot 无响应问题排查与修复

问题根源

  1. 事件嵌套错误:你把on_message消息监听事件写在了on_ready内部,Discord客户端只会识别全局定义的@client.event装饰器事件,这种嵌套写法会导致消息监听逻辑无法被正确注册,自然不会响应普通的hello/Hello消息。
  2. 触发条件单一:原代码只处理以$hello开头的消息,没有针对纯hello/Hello的情况做判断。

修复后的代码

import discord, os

TOKEN = 'I CANT POST IT IN THE FORM'

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

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('We have Logged in as {0.user}'.format(client))

# 将on_message移到全局范围,不再嵌套在on_ready内
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    # 统一转小写,兼容大小写并覆盖多场景
    content_lower = message.content.lower()
    if content_lower.startswith('$hello'):
        await message.channel.send('Hello!')
        if "hello" in content_lower:
            await message.channel.send('i found hello in the message,SUCSESS!')
    elif content_lower == 'hello':
        await message.channel.send('Hello there!')

client.run(TOKEN)

修复说明

  • 把on_message事件从on_ready内部移出,确保Discord客户端能正常注册并监听所有频道消息。
  • 新增纯hello消息的判断逻辑,通过转小写统一处理大小写差异,保证用户发送hello/Hello时Bot能正常响应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 21:52:33