我有这样的代码:
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)