我有这样的代码:

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

    @commands.Cog.listener()
    async def on_ready(self):
        print("TEST")

    @app_commands.command(name="setup", description="setup")
    async def setup(self, interaction):
        await interaction.response.send_message("test")


async def setup(bot):
    await bot.add_cog(Test(bot))

问题是,当加载齿轮时,它不会打印"test",而且on_ady中的代码似乎没有触发.

我在输出中没有得到任何错误,我认为ON_READY没有被触发,但是async def on_message(self, message)可以很好地工作.这可能是什么问题呢?

Main.py如下所示:

    import os
    import discord
    from discord.ext import commands
    
    intents = discord.Intents.all()
    intents.members = True
    bot = commands.Bot(command_prefix="!", intents=intents)
    
    
    async def load():
        print("Loading cogs:")
        for filename in os.listdir("./cogs"):
            if filename.endswith(".py"):
                print(filename)
                await bot.load_extension(f'cogs.{filename[:-3]}')
    
    
    @bot.event
    async def on_ready():
        print(f'Logged in as {bot.user.name}')
        await load()
    
    
    @bot.command()
    async def sync(ctx):
        if ctx.author.id == MY_USER_ID:
            b = await bot.tree.sync()
            await ctx.send("Currently synced " + str(len(b)) + " commands!")
            print("Sync command was issued, synced " + str(len(b)) + " commands!")
        else:
            await ctx.send('You must be me to use this command!')
    
    
    @bot.command()
    async def reload(ctx):
        if ctx.author.id == MY_USER_ID:
            print("RELOADING SELECTED COGS:")
            await ctx.send("Succesfully reloaded these cogs:")
            for filename in os.listdir("./cogs"):
                if filename.endswith(".py"):
                    await ctx.send(filename)
                    await bot.reload_extension(f'cogs.{filename[:-3]}')
        else:
            await ctx.send('You must be me to use this command!')
    
    
    with open('token.txt', 'r') as file:
        bot_token = file.read().strip()
    
    bot.run(bot_token)

推荐答案

请注意,您的齿轮是在触发on_ready事件后加载的.我建议你在启动机器人之前加载你的齿轮:

import asyncio


async def main():
    async with bot:
        print("Loading cogs:")
        for filename in os.listdir("./cogs"):
            if filename.endswith(".py"):
                print(filename)
                await bot.load_extension(f'cogs.{filename[:-3]}')
        await bot.start(bot_token)

asyncio.run(main())

通过这种方式启动机器人,您可以删除load()功能,并将其从on_ready事件中删除.

Python相关问答推荐

将行从一个DF添加到另一个DF

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

具有多个选项的计数_匹配

使用SciPy进行曲线匹配未能给出正确的匹配

大Pandas 胚胎中产生组合

try 与gemini-pro进行多轮聊天时出错

在Python中处理大量CSV文件中的数据

为什么带有dropna=False的groupby会阻止后续的MultiIndex.dropna()工作?

我如何使法国在 map 中完全透明的代码?

如何在solve()之后获得症状上的等式的值

如何在Python脚本中附加一个Google tab(已经打开)

如何在Python数据框架中加速序列的符号化

Streamlit应用程序中的Plotly条形图中未正确显示Y轴刻度

使用groupby方法移除公共子字符串

如何合并两个列表,并获得每个索引值最高的列表名称?

使用Python和文件进行模糊输出

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

Pandas:计算中间时间条目的总时间增量

Polars Group by描述扩展

将字节序列解码为Unicode字符串