Discord机器人启动无报错但@bot.command()指令无法生效问题求助
问题原因
- 重复实例化客户端:
commands.Bot是discord.Client的子类,本身具备Client的全部功能,你额外创建了独立的client实例并实际运行它,所有绑定在bot对象上的指令自然不会生效。 - 缺少必要的Intent权限:Discord API v10及以上版本要求显式开启
message_content权限,才能读取用户发送的消息内容、识别前缀指令。 - 重复定义就绪事件:两个客户端各自绑定了独立的
on_ready事件,只有实际运行的客户端对应的事件会被触发。
修复后的代码
import random from discord.ext import commands import asyncio, discord, time permissions = 397287782400 # 声明Intents并开启消息内容权限 intents = discord.Intents.default() intents.message_content = True # 直接使用Bot实例,不需要额外创建Client bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print(f"I have logged in as {bot.user}") game = discord.Game('배교닐응디만지기') await bot.change_presence(status=discord.Status.online, activity=game) @bot.command() async def 안녕(ctx): await ctx.send("안녕") @bot.command() async def 이호똥(ctx): # 请确保dice()函数在此处调用前已经完成定义,否则会抛出未定义错误 result, _color, bot1, bot2, user1, user2, a, b = dice() embed = discord.Embed(title = "이호똥 게임 결과", description = None, color = _color) embed.add_field(name = "이호가 싼 똥 " + bot1 + "+" + bot2, value = ":: " + a, inline = False) embed.add_field(name = ctx.author.name+"의 똥 " + user1 + "+" + user2, value = ":poop: " + b, inline = False) embed.set_footer(text="결과: " + result) await ctx.send(embed=embed) # 替换为你的Bot令牌启动即可 bot.run("YOUR_BOT_TOKEN")
额外配置要求
你需要登录Discord开发者平台,进入对应Bot的设置页面,找到Privileged Gateway Intents分类,打开Message Content Intent的开关,否则代码中开启的权限不会生效。
内容的提问来源于stack exchange,提问作者하얀곰.
相关产品推荐
相关产品推荐

