Discord.py机器人无法响应命令,请求问题排查
问题分析与解决方法
你的代码核心问题是同时创建了discord.Client和commands.Bot两个客户端实例,却只启动了Client实例,而$say命令是绑定在Bot实例上的,导致命令逻辑根本没有运行,所以机器人无响应。
commands.Bot是discord.Client的子类,已经包含了基础客户端的所有功能,完全不需要同时初始化两个实例。
修正后的代码如下:
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True # 仅创建Bot实例即可 bot = commands.Bot(intents=intents, command_prefix='$') @bot.event async def on_ready(): print(f'Logged in as {bot.user}!') @bot.command() async def say(ctx, *, arg): await ctx.send(arg) # 启动Bot实例 bot.run('MY_TOKEN')
补充说明:
- 把命令参数改成
*, arg后,你可以直接发送$say hello world,机器人会完整返回hello world(不需要加引号);如果保持原参数arg,只能接收单个单词,带空格的内容会被截断。
内容的提问来源于stack exchange,提问作者TacoSnack
相关产品推荐
相关产品推荐

