Discord.py Cog加载异常:hello命令未找到及成员欢迎失效问题
问题:Discord机器人Cog无法加载,命令未找到且无欢迎消息
我使用Python开发Discord机器人,采用Cogs功能开发,严格遵循官方文档及视频教程操作,但Cog始终无法正常加载。执行hello命令时触发错误:
discord.ext.commands.bot: Ignoring exception in command None discord.ext.commands.errors.CommandNotFound: Command "hello" is not found
且新成员加入时无欢迎消息推送。
Cog代码
import discord from discord.ext import commands, tasks class Greetings(commands.Cog): def __init__(self, bot): self.bot = bot self._last_member = None @commands.Cog.listener() async def on_member_join(self, member): channel = member.guild.system_channel if channel is not None: await channel.send(f'Welcome {member.mention}.') @commands.command() async def hello(self, ctx, *, member: discord.Member = None): """Says hello""" member = member or ctx.author if self._last_member is None or self._last_member.id != member.id: await ctx.send(f'Hello {member.name}~') else: await ctx.send(f'Hello {member.name}... This feels familiar.') self._last_member = member async def setup(): await bot.add_cog(Greetings(bot))
main.py中的Cog加载函数
# Load cogs async def load(): for file in os.listdir('./cogs'): if file.endswith('.py'): await bot.load_extension(f'cogs.{file[:-3]}')
尝试的解决方法
- 重写
hello命令 - 调整Cog加载逻辑
以上尝试均未解决问题。
最终解决方案
排查发现问题根源是缺少调用load()并启动机器人的主函数,添加后问题解决,主函数代码如下:
async def main(): async with bot: await load() await bot.start(token)
内容的提问来源于stack exchange,提问作者paxanoA1607
相关产品推荐
相关产品推荐

