繁体   English   中英

Discord.py 试图在我的嵌入中获取消息作者

[英]Discord.py trying to get the message author in my embed

我一直在使用 python 版本 3.7.6 在 Thonny 中使用 Discord.py 编写我自己的 discord 机器人。 我想在输入某个命令(.submit)时嵌入,以将用户名作为标题,将消息内容作为描述。 我很好,在我的嵌入中提交“”,但如果有任何方法可以取出它并且只有消息的内容减去.submit,我将非常感激。 现在我的代码出现两个错误,一个是客户端:用户:名称是机器人的名称(提交机器人)而不是作者(旧代码),我收到这条消息“命令引发异常: TypeError: Object of type Member is not JSON serializable' 使用我的新代码(如下),如果有人能提供见解,请回复适当的修复!

client = commands.Bot(command_prefix = '!')
channel = client.get_channel(707110628254285835)

@client.event
async def on_ready():
 print ("bot online")


@client.command()
async def submit(message):
 embed = discord.Embed(
    title = message.message.author,
    description = message.message.content,
    colour = discord.Colour.dark_purple()
 )
 channel = client.get_channel(707110628254285835)
 await channel.send(embed=embed)





client.run('TOKEN')
client = commands.Bot(command_prefix = '!')
#got rid of the old channel variable here
@client.event
async def on_ready():
 print ("bot online")

@client.command()
async def submit(ctx, *, message): #the `*` makes it so that the message can include spaces
 embed = discord.Embed(        
    title = ctx.author, #author is an instance of the context class
    description = message, #No need to get the content of a message object
    colour = discord.Colour.dark_purple()
 )
 channel = client.get_channel(707110628254285835)
 await channel.send(embed=embed)

client.run('TOKEN')

问题:1. channel定义了两次,2. discord.py 中的 function 命令采用隐式上下文参数,通常称为ctx 总体而言,您似乎不了解 discord.py 必须提供的基本 OOP 概念。 使用在线 class 或文章刷新您的 memory 可能会有所帮助。

你很接近...

以下项目应该会有所帮助:

  1. 使用命令时,您通常将命令的上下文定义为“ctx”,这是第一个参数,有时也是唯一的参数。 “消息”通常用于消息事件,例如async def on_message(message):
  2. 要从命令本身中提取消息,请将 arguments 添加到 function 定义中。
  3. 要获取用户名,您需要转换为字符串以避免 TypeError。
  4. 要将消息发送回与输入提交相同的频道,您可以使用await ctx.send(embed=embed)

尝试:

@client.command()
async def submit(ctx, *, extra=None):
    embed = discord.Embed(
        title=str(ctx.author.display_name),
        description=extra,
        colour=discord.Colour.dark_purple()
    )
    await ctx.send(embed=embed)

结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM