📅  最后修改于: 2023-12-03 14:50:36.561000             🧑  作者: Mango
本文介绍如何使用 discord.py 库在 Python 中发送嵌入消息到 Discord。discord.py 是一个功能强大的库,用于创建 Discord 机器人和与 Discord API 进行交互。
要开始在 Python 中使用 discord.py,首先需要安装它。可以使用以下命令通过 pip 安装 discord.py:
pip install discord.py
要发送嵌入消息到 Discord,首先需要创建一个 Discord 机器人。可以按照以下步骤进行操作:
接下来,我们将使用以下 Python 代码来发送嵌入消息到 Discord:
import discord
from discord import Embed
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
TOKEN = 'YOUR_DISCORD_BOT_TOKEN'
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
if message.content.startswith('!embed'):
embed = Embed(title='Embed Title',
description='Embed Description',
color=discord.Color.blue())
embed.add_field(name='Field 1', value='Value 1', inline=False)
embed.add_field(name='Field 2', value='Value 2', inline=False)
await message.channel.send(embed=embed)
client.run(TOKEN)
在上面的代码中,替换 YOUR_DISCORD_BOT_TOKEN
为你在 Discord 开发者门户中创建机器人时获得的令牌。
要运行上面的代码,保存文件为 bot.py
(或任意名称),然后通过以下命令运行代码:
python bot.py
确保你的 Discord 机器人在线,并嵌入消息可以通过在 Discord 中输入 !embed
命令来触发。
在 on_message
事件处理程序中,我们创建了一个嵌入消息对象 embed
,并使用 add_field
方法添加字段。然后,我们使用 message.channel.send
发送嵌入消息。
以上就是使用 discord.py 库在 Python 中发送嵌入消息到 Discord 的介绍。希望本文对你有所帮助!