【问题标题】:Why is message not defined in my Command?为什么我的命令中没有定义消息?
【发布时间】:2018-12-10 04:34:54
【问题描述】:

所以,我用我的不和谐机器人发出了一个帮助命令,当我将它作为嵌入消息发送时它看起来更加整洁。但是,它确实占用了很多空间,所以我想知道是否可以将其作为 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' not defined。”即使我将message.author 部分替换为message.channel,此错误消息仍然会出现。我可以让消息发送的唯一方法是将bot.send_message 替换为await bot.say(embed=embed)。有没有办法解决这个问题?

【问题讨论】:

    标签: python bots discord discord.py


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2011-01-28
      • 2011-08-07
      • 2012-11-06
      相关资源
      最近更新 更多