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

discord.py机器人@bot.command无响应问题排查(附代码)

问题原因及解决方法

你代码里的核心问题是同时创建了discord.Client()和commands.Bot()两个独立的机器人实例,但最后启动的是client实例,而你的slap命令是注册在bot实例上的——相当于给一个没启动的机器人写了命令,自然不会有响应。另外,bot实例的intents配置也不对,没开启message_content,就算启动了也读不到消息里的命令前缀。

修正步骤:

  • 删掉多余的client = discord.Client(intents=intent),只用commands.Bot(它是Client的子类,完全能实现Client的所有功能)
  • 把on_ready事件绑定到bot实例上,而非client
  • 给bot配置正确的intents(和你之前给client配的一致即可)
  • 最后用bot.run(TOKEN)启动机器人

修改后的完整代码:

import discord
from discord.ext import commands
import random

TOKEN = "MY TOKEN"

# 配置intents
intent = discord.Intents.default()
intent.members = True
intent.message_content = True

# 只创建一个Bot实例
bot = commands.Bot(command_prefix='--', intents=intent)

slap_gif = ['https://media.tenor.com/GBShVmDnx9kAAAAC/anime-slap.gif', 'https://media.tenor.com/CvBTA0GyrogAAAAC/anime-slap.gif', 'https://i.pinimg.com/originals/fe/39/cf/fe39cfc3be04e3cbd7ffdcabb2e1837b.gif', 'https://i.pinimg.com/originals/68/de/67/68de679cc20000570e8a7d9ed9218cd3.gif']
slap_txt = ['Ya, you deserve it', 'Get slapped', 'Ya, get slapped hard']

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

@bot.command()
async def slap(ctx):
    embed = discord.Embed(
        color=discord.Color("#ee5253"),
        description = f"{ctx.author.mention} {random.choice(slap_txt)}"
    )
    embed.set_image(url=random.choice(slap_gif))
    await ctx.send(embed=embed)

# 启动Bot实例
bot.run(TOKEN)

现在启动机器人,发送--slap就能触发命令了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:21:35