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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 18:55:25