为什么在我的命令中未定义消息? [英] Why is message not defined in my Command?

查看:76
本文介绍了为什么在我的命令中未定义消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经用我的discord机器人发出了一条帮助命令,当我将其作为嵌入消息发送时,它看起来更加简洁.但是,它确实占用了大量空间,因此我想知道是否可以将它作为DM发送给message.author.这是我到目前为止的内容:

So, I have made a help command with my discord bot and it looks much more neat when I send it as an embed message. However, it does take up a lot of space, so I was wondering if I could send it as a DM to the message.author. Here's what I have so far:

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:未定义名称'消息'."即使我将message.author部分替换为message.channel,此错误消息仍然会出现.完全可以发送消息的唯一方法是将bot.send_message替换为await bot.say(embed=embed).有办法解决吗?

However after running the command you get the error "NameError: name 'message' not defined." This error message still shows up even if I replace the message.author part with message.channel. The only way I can get the message to send at all is when I replace the bot.send_message with await bot.say(embed=embed). Is there a way around this?

推荐答案

您没有对message的直接引用.您必须从要传递的Context对象中获取它.

You don't have a direct reference to the message. You have to get it from the Context object you're passing.

@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)

这篇关于为什么在我的命令中未定义消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆