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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 13:00:23