简体   繁体   English

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

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

I have been getting into coding my very own discord bot using Discord.py in Thonny, using python version 3.7.6.我一直在使用 python 版本 3.7.6 在 Thonny 中使用 Discord.py 编写我自己的 discord 机器人。 I want to have an embed when a certain command is typed (.submit) to have the users name as the title and the content of the message as the description.我想在输入某个命令(.submit)时嵌入,以将用户名作为标题,将消息内容作为描述。 I am fine having,submit ' ' in my embed but if there is any way of taking that out and only having the content of the message minus the.submit i would highly appreciate it.我很好,在我的嵌入中提交“”,但如果有任何方法可以取出它并且只有消息的内容减去.submit,我将非常感激。 Right now with my code i am getting two errors, one is that the client:user:name is the name of the bot (submit bot) and not the author (old code), and i am getting this message 'Command raised an exception: TypeError: Object of type Member is not JSON serializable' with my new code(below), if anyone could offer insight please reply with the appropriate fixes!现在我的代码出现两个错误,一个是客户端:用户:名称是机器人的名称(提交机器人)而不是作者(旧代码),我收到这条消息“命令引发异常: 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')

Problems : 1. channel is defined twice, 2. function commands in discord.py take an implicit context argument, usually called ctx .问题:1. channel定义了两次,2. discord.py 中的 function 命令采用隐式上下文参数,通常称为ctx Overall, it looks like you aren't understand the underlying OOP concepts that discord.py has to offer.总体而言,您似乎不了解 discord.py 必须提供的基本 OOP 概念。 It might be helpful to refresh your memory with an online class or article.使用在线 class 或文章刷新您的 memory 可能会有所帮助。

You're pretty close...你很接近...

The following items should help:以下项目应该会有所帮助:

  1. Working with commands you generally define the context of the command as "ctx", which is the first and sometime only argument.使用命令时,您通常将命令的上下文定义为“ctx”,这是第一个参数,有时也是唯一的参数。 "message" is generally used on message events like async def on_message(message): . “消息”通常用于消息事件,例如async def on_message(message):
  2. To break out the message from the command itself you add arguments to the function definition.要从命令本身中提取消息,请将 arguments 添加到 function 定义中。
  3. To get the name of the user you need to cast to a string to avoid the TypeError.要获取用户名,您需要转换为字符串以避免 TypeError。
  4. To send the message back in the same channel as the,submit was entered, you can use await ctx.send(embed=embed)要将消息发送回与输入提交相同的频道,您可以使用await ctx.send(embed=embed)

Try:尝试:

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

Result:结果:

在此处输入图像描述

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

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