Discord py cogs 不加载命令

Discord py cogs not loading commands

我制作了一个 discord 机器人,其中包含一个主文件和一个加载了 cogs 的命令文件,但问题是这些命令未加载。

我简化了代码三位一体来解决错误,但即使这样也不起作用:

bot.py:

import discord
from discord.ext import commands

import config

# declaring intents and launching client
intents = discord.Intents.default()
intents.members = True
intents.presences = True

bot = commands.Bot(command_prefix='.', description="Useful bot", intents=intents)

startup_extensions = ["private_channels"]

# listener
@bot.event
async def on_ready():
    print(f'Connected to Discord as {bot.user} !')

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    print("message received")

# starting extensions
for extension in startup_extensions:
    try:
        bot.load_extension(extension)
    except Exception as e:
        exc = '{}: {}'.format(type(e).__name__, e)
        print('Failed to load extension {}\n{}'.format(extension, exc))

# starting bot
bot.run(config.token)

private_channels.py:

from discord.ext import commands

class PrivateChannels(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    # creates a new private channel
    @commands.command(name="test")
    async def test(self, ctx):
        await ctx.send("test")

def setup(bot):
    print("Loading private channels")
    bot.add_cog(PrivateChannels(bot))
    print("Private channels loaded")

好的,我遇到的主要问题是 on_message 事件覆盖了命令。为了纠正这个问题,我将 await bot.process_commands(message) 添加到 on_message 来处理命令,现在它可以工作了。