Discord.py中Suggest命令未被识别问题求助
解决Discord.py命令提示“未找到”的问题
嘿,我看了你的代码和报错情况,“命令未找到”这类问题基本都是命令注册环节出了问题,结合你的代码里带了self参数,我整理了几个最可能的修复方向:
1. 检查Cog类里的装饰器是否正确
你的函数带了self,说明这个命令应该是写在Cog类里的,但你用了@bot.command()——这是错误的!Cog内部的命令必须用@commands.command()装饰器,而且类要继承commands.Cog,还要记得把Cog注册到bot上。
修复后的完整Cog代码示例:
from discord.ext import commands import discord class SuggestionCog(commands.Cog): def __init__(self, bot): self.bot = bot @commands.command(name="suggest") # 这里必须用commands.command async def suggest(self, ctx, *, suggestion): # 先删除触发命令的消息 await ctx.channel.purge(limit=1) # 找到建议频道 channel = discord.utils.get(ctx.guild.text_channels, name='💡suggestions') if not channel: await ctx.send("找不到建议频道,请检查频道名称!") return # 构建嵌入消息 suggestEmbed = discord.Embed(colour=0xFF0000) # 注意:discord.py v2.x里avatar_url要改成avatar.url avatar_url = ctx.author.avatar.url if ctx.author.avatar else ctx.author.default_avatar.url suggestEmbed.set_author(name=f'Suggested by {ctx.message.author}', icon_url=avatar_url) suggestEmbed.add_field(name='Suggestion', value=suggestion) # 发送到建议频道 message = await channel.send(embed=suggestEmbed) # 添加反应 await message.add_reaction('✅') await message.add_reaction('❌') # 别忘了在主脚本里注册这个Cog # bot.add_cog(SuggestionCog(bot))
2. 如果没用Cog,直接移除self参数
要是你的命令是直接写在主脚本里(不是类的方法),那函数根本不需要self参数,装饰器用@bot.command()就没问题,修改后代码如下:
import discord from discord.ext import commands bot = commands.Bot(command_prefix="!", intents=discord.Intents.all()) @bot.command(name="suggest") async def suggest(ctx, *, suggestion): # 去掉多余的self await ctx.channel.purge(limit=1) channel = discord.utils.get(ctx.guild.text_channels, name='💡suggestions') if not channel: await ctx.send("找不到建议频道,请检查名称!") return suggestEmbed = discord.Embed(colour=0xFF0000) avatar_url = ctx.author.avatar.url if ctx.author.avatar else ctx.author.default_avatar.url suggestEmbed.set_author(name=f'Suggested by {ctx.message.author}', icon_url=avatar_url) suggestEmbed.add_field(name='Suggestion', value=suggestion) message = await channel.send(embed=suggestEmbed) await message.add_reaction('✅') await message.add_reaction('❌') bot.run("你的Bot令牌")
3. 额外要检查的细节
- 权限问题:确保你的Bot账号有管理消息(用于
purge)、发送消息到建议频道、添加反应的权限,没有权限也会导致命令看似无法触发。 - discord.py版本:如果是v2.x版本,
ctx.author.avatar_url已经被弃用,要改成ctx.author.avatar.url,如果用户没有头像,还要加个默认头像的 fallback,不然会报错。 - 频道名称匹配:确认
💡suggestions这个频道名称完全一致,包括那个灯泡符号,别用了相似的全角/半角符号或者大小写错误。
内容的提问来源于stack exchange,提问作者Akkeuu
相关产品推荐
相关产品推荐

