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

基于discord.py的机器人升级至2.0版本后无法正常运行求助

问题排查与修复

你的代码核心问题出在类定义与实例化不匹配:你自定义了继承自discord.Client的MyClient类,包含on_ready和on_message的事件逻辑,但最终实例化的是原生discord.Client而非你自己的MyClient,导致写好的事件处理函数完全没被调用。

修复后的完整代码

import discord
from keep_alive import keep_alive

class MyClient(discord.Client):
    async def on_ready(self):
        print('bot is online now', self.user)

    async def on_message(self, message):
        word_list = ['fdsfds','fsa']

        if message.author == self.user:
            return

        messageContent = message.content
        if len(messageContent) > 0:
            for word in word_list:
                if word in messageContent:
                    await message.delete()
                    await message.channel.send('Do not say that!')

# 若需要保活功能,取消下面一行的注释
# keep_alive()
intents = discord.Intents.default()                  
intents.message_content = True                  
# 实例化自定义的MyClient类而非原生discord.Client
client = MyClient(intents=intents)
client.run('OTkxMDcxMTUx')

额外检查项

  • 确认Discord开发者门户中已开启Message Content Intent(消息内容意图)
  • 确保机器人在目标服务器拥有删除消息和发送消息的权限
  • 若需要持续在线,记得取消keep_alive()的注释

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:15:45