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

升级discord.py至2.0版本后Bot命令无响应问题求助

问题原因及解决方法

你的核心问题是同时创建了commands.Bot和discord.Client两个客户端实例,这会导致事件处理冲突,且你把消息事件绑定在了未实际运行的client实例上,所以升级后所有交互无响应。

具体修复步骤:

  1. 移除多余的客户端实例
    删除代码中的client = discord.Client(intents=intents),因为commands.Bot继承自discord.Client,已经包含所有客户端功能,无需重复创建。

  2. 修正事件绑定对象
    将@client.event替换为@bot.event,确保消息事件绑定到正在使用的Bot实例上。

  3. 添加命令处理逻辑(若使用前缀命令)
    在on_message事件末尾加入await bot.process_commands(message),否则commands.Bot的前缀命令无法被识别处理。

  4. 启动正确的实例
    运行机器人时使用bot.run("你的TOKEN"),而非client.run。

  5. 替换阻塞式延迟
    代码中的time.sleep(2)会阻塞事件循环,导致机器人假死,需替换为异步环境下的await asyncio.sleep(2)。

修改后的完整代码示例:

import discord, os, requests, json, random, datetime
from discord.ext import commands
from replit import db
import urllib
import asyncio
from discord import Member
from discord.ui import Select,Button,View

intents = discord.Intents.default()  
intents.members = True
# 仅保留Bot实例
bot = commands.Bot(command_prefix="e!", intents=intents)

# 绑定到Bot实例的消息事件
@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    if message.content.startswith('whats the meaning of life'):
        await message.channel.send(
            'The meaning of life is "freedom from suffering" through apatheia (Gr: απαθεια), that is, being objective and having "clear judgement", not indifference. - Wikipedia.'
        )
        # 异步延迟,避免阻塞事件循环
        await asyncio.sleep(2)
        await message.channel.send(
            'There, someone just explained your life. Kind of depressing, isnt it?'
        )
    # 处理前缀命令的必要语句
    await bot.process_commands(message)

# 启动机器人
bot.run("你的机器人TOKEN")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 17:50:34