对于2022年遇到这个问题的人:
你可以尝试将 @bot.command()
替换为 @client.event
,我替换后一切都正常。@bot.command() 不起作用,你可以试一下。
对于上述情况,我不建议使用 @client.event
/ @bot.event
来注册命令,因为你需要将命令注册为一个命令。
如果你想在 main.py
文件中简单地创建一个带有嵌入内容的命令,请确保有类似以下内容:
import discord
from discord.ext import commands
intents = discord.Itents.default()
bot = commands.Bot(command_prefix='YOURPREFIX', description='description', intents=intents)
@bot.command(name="embed")
async def embed(ctx):
embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
embed.add_field(name="Name", value="Value", inline=False)
await ctx.send(embed=embed)
然而,我个人喜欢将我的命令分成一个/commands
文件夹,并为每个命令单独创建文件,因为这是更整洁的代码的良好实践。
为此,我使用cogs。
/commands/embed.py
from discord.ext import commands
import discord
class EmbedCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(name="embed")
async def embed(self, ctx):
embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
embed.add_field(name="Name", value="Value", inline=False)
await ctx.send(embed=embed)
然后将所有内容导入到你的 main.py 文件中:
from commands.embed import EmbedCog
bot.add_cog(EmbedCog(bot))