[英]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 可能会有所帮助。
你很接近...
以下项目应该会有所帮助:
async def on_message(message):
。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.