【问题标题】:如何通过我的 Discord 机器人发送嵌入,使用 python?
【发布时间】:2017-12-05 08:06:19
【问题描述】:

我一直在开发一个新的 Discord 机器人。

我学到了一些东西,现在,我想让这些东西变得更加定制化。

我一直在尝试让机器人发送嵌入消息,而不是普通消息。

embed=discord.Embed(title="Tile", description="Desc", color=0x00ff00)
embed.add_field(name="Fiel1", value="hi", inline=False)
embed.add_field(name="Field2", value="hi2", inline=False)
await self.bot.say(embed=embed)

执行此代码时,我收到“嵌入”不是模块“不和谐”模块的有效成员的错误。所有网站,给我看这段代码,我不知道有什么其他的方式来发送嵌入。

【问题讨论】:

  • discord.py 错误处理会忽略此类错误,因为在生产中,单个错误可能会导致整个机器人崩溃。因此,我建议您在每次测试时在函数内放置一个 try except 块,这将有助于理解错误

标签: python embed discord discord.py


【解决方案1】:

为了让它工作,我将你的 send_message 行更改为 await message.channel.send(embed=embed)

这是一个完整的示例代码,展示了它是如何适应的:

@client.event
async def on_message(message):
    if message.content.startswith('!hello'):
        embedVar = discord.Embed(title="Title", description="Desc", color=0x00ff00)
        embedVar.add_field(name="Field1", value="hi", inline=False)
        embedVar.add_field(name="Field2", value="hi2", inline=False)
        await message.channel.send(embed=embedVar)

我使用 discord.py 文档来帮助找到它。 https://discordpy.readthedocs.io/en/latest/api.html#discord.TextChannel.send 用于发送方法的布局。

https://discordpy.readthedocs.io/en/latest/api.html#embed 用于 Embed 类。

1.0之前的版本:如果您使用的是1.0之前的版本,请改用await client.send_message(message.channel, embed=embed)方法。

【讨论】:

  • 不知何故,它给了我一个新的错误。 “预期一个意图块”,箭头显示“embed =”的“d”中的错误
  • 确保您没有意外缩进某些内容。检查 if 语句和该行的缩进。
  • 我做到了,一切正常。它以某种方式一直这么说。
  • 那我不确定。对不起。尝试在这个不和谐服务器中询问discord.gg/discord-api,或者您可以尝试在 github 上将其报告为问题。其他人似乎也有这个问题,所以也许也可以监视该帖子stackoverflow.com/questions/44474955/…
  • 你应该为变量“embed”选择一个不同的名称,不确定它是否会导致错误,无论如何,bat,embed=embed 不是那么清楚..
【解决方案2】:

执行此代码时,我收到“嵌入”不是模块“不和谐”模块的有效成员的错误。所有网站,给我看这段代码,我不知道有什么其他的方式来发送嵌入。

这意味着你已经过时了。使用 pip 更新您的库版本。

pip install --upgrade discord.py

【讨论】:

  • 说安装位有语法错误。
  • 它在哪里说您遇到了语法错误?您在命令行中运行此命令
  • 我在 bot 的脚本中完成了这项工作,lmao。无论如何,我尝试在 cmds 中运行它,但它没有用。让我发送打印。 prntscr.com/fvjbff我做错了什么?
  • @Norby 我有点晚了,但你还没有将pip 添加到path
【解决方案3】:
@bot.command()
async def displayembed(ctx):
    embed = discord.Embed(title="Your title here", description="Your desc here") #,color=Hex code
    embed.add_field(name="Name", value="you can make as much as fields you like to")
    embed.set_footer(name="footer") #if you like to
    await ctx.send(embed=embed)

【讨论】:

  • 也许你应该指出问题中发布的代码有什么问题
【解决方案4】:

@client.event 代替@bot.command() 怎么样,当我输入@client.event 时它会修复所有问题... @bot.command() 不起作用,你可以输入

@client.event
async def displayembed(ctx):
    embed = discord.Embed(title="Your title here", description="Your desc here") #,color=Hex code
    embed.add_field(name="Name", value="you can make as much as fields you like to")
    embed.set_footer(name="footer") #if you like to
    await ctx.send(embed=embed)

【讨论】:

    猜你喜欢
    • 2021-04-10
    • 2021-07-20
    • 2021-07-24
    • 2020-06-29
    • 2018-06-23
    • 2021-04-14
    • 2021-01-12
    • 2020-04-11
    • 2021-06-13
    相关资源
    最近更新 更多