我应该如何在课堂上使用装饰器@bot.tree.command?
我的主要文件:
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands, tasks
extens = ["cogs.slash_commands"]
class MyBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix='/', intents=discord.Intents.all())
async def on_ready(self):
print(f'{self.user} is now running!')
for cog in extens:
try:
await bot.load_extension(cog)
print(f"{cog} was loaded.")
except Exception as e:
print(e)
bot = MyBot()
load_dotenv()
bot.run(os.getenv('TOKEN'))
我的齿轮看起来像这样
import discord
from discord import app_commands
from discord.ext import commands
class SlashCommands(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.tree.command
async def hello(self, interaction: discord.Interaction, thing_to_say: str):
await interaction.response.send_message(f"{thing_to_say}")
async def setup(bot):
await bot.add_cog(SlashCommands(bot))
当我编译时它说:
扩展“cogs.slash_commands”引发错误:AttributeError:模块“discord.ext.commands”没有属性“tree”
我该怎么办?我应该在主文件中添加什么并在 cog 文件中编辑什么?
discord.ext.commands
没有树属性,你的 bot
有。
在我的代码中,没有类,我使用
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
#make sure your bot variable is correctly being assigned to the intents
@bot.event #dont forget this in your code
async def on_ready():
print('Bot is now running!')
try:
synced = await bot.tree.sync() #should be the same as your cog system
except Exception as e:
print(e)
#accessing the tree within the bot, not the tree within the commands
@bot.tree.command(name="website", description="Get the link to our website")
async def website(interaction: discord.Interaction):
await interaction.response.send_message("https://www.stackoverflow.com")