为什么我的命令中没有定义消息?

问题描述 投票:0回答:1

所以,我已经使用我的discord bot发出了一个帮助命令,当我将它作为嵌入消息发送时,它看起来更加整洁。但是,它确实占用了很多空间,所以我想知道我是否可以将它作为DM发送到message.author。这是我到目前为止所拥有的:

import discord
from discord.ext.commands import Bot
from discord.ext import commands

Client = discord.Client()
bot_prefix = "."
bot = commands.Bot(command_prefix=bot_prefix)

@bot.event
async def on_ready():
    print("Bot Online!")
    print("Name: {}".format(bot.user.name))
    print("ID: {}".format(bot.user.id))

bot.remove_command("help")

# .help
@bot.command(pass_context=True)
async def help(ctx):
embed=discord.Embed(title="Commands", description="Here are the commands:", color=0xFFFF00)
embed.add_field(name="Command1", value="What it does", inline= True)
embed.add_field(name="Command2", value="What it does", inline= True)
await bot.send_message(message.author, embed=embed)

bot.run("TOKEN")

但是,在运行该命令后,您会收到错误“NameError:name'消息'未定义。”即使我用message.author替换message.channel部分,此错误消息仍然显示。我可以获得发送消息的唯一方法是当我用bot.send_message替换await bot.say(embed=embed)时。有没有解决的办法?

python bots discord discord.py
1个回答
1
投票

您没有直接引用message。你必须从你经过的Context对象那里得到它。

@bot.command(pass_context=True)
async def help(ctx):
    embed=discord.Embed(title="Commands", description="Here are the commands:", color=0xFFFF00)
    embed.add_field(name="Command1", value="What it does", inline= True)
    embed.add_field(name="Command2", value="What it does", inline= True)
    await bot.send_message(ctx.message.author, embed=embed)
© www.soinside.com 2019 - 2024. All rights reserved.