我该如何使用Python编写Discord机器人发送嵌入式消息?

24

我一直在开发一个新的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)

执行此代码时,我收到错误消息:“Embed”不是“discord”模块的有效成员。所有的网站都展示了这段代码,但我不知道其他发送嵌入式信息的方法。

discord.py 的错误处理会忽略这样的错误,因为在生产环境中,一个错误可能会导致整个机器人崩溃。因此,我建议每次测试时在函数内部放置 try except 块,这将有助于理解错误。 - Joel
6个回答

41
为了使其正常工作,我更改了您的发送消息行到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 是send方法的布局。 https://discordpy.readthedocs.io/en/latest/api.html#embed 是嵌入类的文档。
版本1.0之前:如果您使用的是1.0版本之前的旧版本,请改用方法await client.send_message(message.channel, embed=embed)

现在出现了一个新的错误,提示“需要缩进块”,箭头指向“embed =”中的“d”。 - Norberto A.
确保你没有意外缩进了什么。检查if语句和那一行的缩进。 - Tim
我不确定。抱歉。你可以尝试在这个Discord服务器上询问 https://discord.gg/discord-api 或者你可以将其报告为Github上的问题。还有其他人似乎也遇到了这个问题,所以也许可以关注一下这篇帖子 https://stackoverflow.com/questions/44474955/discord-bot-python-no-attribute?rq=1。 - Tim
你应该为变量“embed”选择一个不同的名称,不确定它是否会导致错误,但无论如何,“embed = embed”并不是很清晰。 - Wise Man
@WiseMan 我已经将其更改为 embedVar。您认为这个名称足够清晰,还是有其他建议? - Tim
self.bot.say更改为ctx.send - Westlenando

7
当执行这段代码时,我收到一个错误信息,指出“Embed”不是模块“discord”的有效成员。所有的网站都展示这段代码,但我不知道其他发送嵌入消息的方法。
这意味着你的库版本过旧,请使用pip更新库版本。
pip install --upgrade discord.py

1
它说安装部分有语法错误。 - Norberto A.
你在哪里看到语法错误的提示?你是在命令行中运行这个命令吗? - khazhyk
我已经在机器人脚本中完成了这个,哈哈。 不管怎样,我尝试在命令中运行它,但它没有起作用。让我发送打印内容。http://prntscr.com/fvjbff我做错了什么? - Norberto A.
1
@Norby,我有点晚了,但是你还没有将pip添加到path中。 - Person

2
如何在代码中将 @bot.command() 替换为 @client.event ,当我将其替换为 @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)

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

2
也许您应该指出问题所在,针对提问中发布的代码。 - knee-cola

1

对于2022年遇到这个问题的人:

你可以尝试将 @bot.command() 替换为 @client.event,我替换后一切都正常。@bot.command() 不起作用,你可以试一下。

对于上述情况,我不建议使用 @client.event / @bot.event 来注册命令,因为你需要将命令注册为一个命令

如果你想在 main.py 文件中简单地创建一个带有嵌入内容的命令,请确保有类似以下内容:

import discord
from discord.ext import commands

intents = discord.Itents.default()

bot = commands.Bot(command_prefix='YOURPREFIX', description='description', intents=intents)

@bot.command(name="embed")
async def embed(ctx):
    embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
    embed.add_field(name="Name", value="Value", inline=False)
    await ctx.send(embed=embed)

然而,我个人喜欢将我的命令分成一个/commands文件夹,并为每个命令单独创建文件,因为这是更整洁的代码的良好实践。

为此,我使用cogs。

/commands/embed.py

from discord.ext import commands
import discord

class EmbedCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    
    @commands.command(name="embed")
    async def embed(self, ctx):
        embed = discord.Embed(title='Title', description='Desc', color=discord.Color.random())
        embed.add_field(name="Name", value="Value", inline=False)
        await ctx.send(embed=embed)

然后将所有内容导入到你的 main.py 文件中:

from commands.embed import EmbedCog

bot.add_cog(EmbedCog(bot))

0

在使用嵌入之前

embed(嵌入)- 要发送的内容的丰富嵌入。这不能与embeds参数混合使用。

embeds(List [Embed])- 要与内容一起发送的嵌入列表。最多10个。这不能与嵌入混合使用

TypeError - 您同时指定了嵌入和embeds或文件和files或线程和thread_name。

@client.event
async def on_message(message):
    if message.content.startswith('!hi'):
        embed = discord.Embed(title="This is title of embedded element", description="Desc", color=0x00ff00)
        embed.add_field(name="Like header in HTML", value="Text of field  1", inline=False)
        embed.add_field(name="Like header in html", value="text of field 2", inline=False)
        await message.channel.send(embed=embed)

参考资料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接