Discord.py试图让消息作者进入我的嵌入

时间:2020-05-07 04:22:27

标签: python discord discord.py

我一直在使用Thonny中的Discord.py(使用python 3.7.6版)来编码自己的discord机器人。我想在键入某个命令(!submit)时使用嵌入,以用户名作为标题,并以消息的内容作为描述。我在嵌入中有!submit''很好,但是如果有任何方法可以删除它,并且仅使消息内容减去!submit,我将非常感激。现在,在我的代码中,我遇到两个错误,一个是client.user.name是机器人的名称(提交机器人),而不是作者的名称(旧代码),并且我收到此消息“命令引发了异常:TypeError:类型为Member的对象不可通过我的新代码(以下)进行JSON序列化”,如果有人可以提供见解,请提供相应的修复程序!


Glide.with(this).load("https://animevaya.000webhostapp.com/wp-content/uploads/2020/05/ic_launcher-web.png").placeholder(R.drawable.window).error(R.drawable.bg_white_text_red).into(ImgX);

2 个答案:

答案 0 :(得分:0)

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中的函数命令采用一个隐式的 context 参数,通常称为ctx。 总体而言,您似乎不了解discord.py必须提供的基本OOP概念。通过在线课程或文章刷新记忆可能会有所帮助。

答案 1 :(得分:0)

您非常接近...

以下各项应有所帮助:

  1. 使用命令时,通常将命令的上下文定义为“ ctx”,这是第一个且有时是唯一的参数。通常在诸如async def on_message(message):之类的消息事件上使用“消息”。
  2. 要从命令本身发出消息,请向函数定义中添加参数。
  3. 要获取用户名,您需要转换为字符串以避免TypeError。
  4. 要将邮件发送回与输入!submit相同的频道,可以使用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)

结果:

enter image description here