如何通过我的 Discord bot 发送嵌入文件,w/python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44862112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send an embed via my Discord bot, w/python?
提问by Norberto A.
I've been working a new Discord bot.
我一直在工作一个新的 Discord 机器人。
I've learnt a few stuff,and, now, I'd like to make the things a little more custom.
我已经学到了一些东西,现在,我想让这些东西变得更加定制化。
I've been trying to make the bot send embeds, instead, of a common message.
我一直在尝试让机器人发送嵌入信息,而不是普通消息。
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)
When executing this code, I get the error that 'Embed' is not a valid member of the module 'discord'. All websites, show me this code, and I have no idea of any other way to send a embed.
执行此代码时,出现“嵌入”不是模块“discord”的有效成员的错误。所有网站,给我看这个代码,我不知道有什么其他的方式来发送嵌入。
回答by Tim
To get it to work I changed your send_message line to
await message.channel.send(embed=embed)
为了让它工作,我将您的 send_message 行更改为
await message.channel.send(embed=embed)
Here is a full example bit of code to show how it all fits:
这是一个完整的示例代码,用于展示它是如何适合的:
@client.event
async def on_message(message):
if message.content.startswith('!hello'):
embed = discord.Embed(title="Title", description="Desc", color=0x00ff00)
embed.add_field(name="Field1", value="hi", inline=False)
embed.add_field(name="Field2", value="hi2", inline=False)
await message.channel.send(embed=embed)
I used the discord.py docs to help find this. https://discordpy.readthedocs.io/en/latest/api.html#discord.Webhook.sendfor the layout of the send method.
我使用了 discord.py 文档来帮助找到它。 https://discordpy.readthedocs.io/en/latest/api.html#discord.Webhook.send用于发送方法的布局。
https://discordpy.readthedocs.io/en/latest/api.html#embedfor the Embed class.
https://discordpy.readthedocs.io/en/latest/api.html#embed用于嵌入类。
Before version 1.0: If you're using a version before 1.0, use the method await client.send_message(message.channel, embed=embed)
instead.
1.0版之前:如果您使用的是 1.0 版之前的版本,请改用 方法await client.send_message(message.channel, embed=embed)
。
回答by khazhyk
When executing this code, I get the error that 'Embed' is not a valid member of the module 'discord'. All websites, show me this code, and I have no idea of any other way to send a embed.
执行此代码时,出现“嵌入”不是模块“discord”的有效成员的错误。所有网站,给我看这个代码,我不知道有什么其他的方式来发送嵌入。
This means you're out of date. Use pip
to update your version of the library.
这意味着你已经过时了。使用pip
更新您的库的版本。
pip install --upgrade discord.py
回答by Mad
@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)