我正在开发我的第一个python discord机器人,事实证明它相当不错,但我想对某些响应使用嵌入。其中之一包括发送使用许多嵌入功能的漫游器的所有功能。我没有关于Javascript或JSON的经验,因此我使用网站“ https://discohook.org/”创建了一个基于GUI的嵌入。但是结果是,我只得到JSON文件,却找不到加载JSON文件并以嵌入方式发送的方法。
JSON文件看起来像这样-
{
"content": "Bot Name",
"embeds": [
{
"description": "This command lists all the possible commands which can be used to interact with the bot.",
"author": {
"name": "Help",
"icon_url": "http://4.bp.blogspot.com/-wuUbc-OPOt4/T3ioPh2pAAI/AAAAAAAAAdM/BFFvS5fxVMY/w1200-h630-p-k-no-nu/Questionmark.jpg"
}
},
{
"description": "Bot blesses you. ",
"author": {
"name": "Bless ",
"icon_url": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp4775695.jpg&f=1&nofb=1"
}
},
{
"description": "Set status as idle",
"color": 16777215,
"author": {
"name": "Go Sleep",
"icon_url": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.forgecdn.net%2Favatars%2F170%2F652%2F636723641550179947.png&f=1&nofb=1"
}
}
]
答案 0 :(得分:0)
您可能只需加载各个变量。
with open(“JSON_FILE.txt”) as json_file:
data = json.load(json_file)
data = data[“embeds”]
然后,您可以通过某种方式获取特定的字典。
for embed in data:
pass
然后将数据定义为您要使用的字典。
embed = discord.Embed(description = data[“description”])
embed.set_author(name = data[“author”][“name”], icon_url = data[“author”][“icon_url”]
答案 1 :(得分:0)
也许已经很久了,但我遇到了完全相同的问题,我找到了自己的解决方案。这是:
from json import loads
from discord import Embed
# Just for our convinience let's make a function
def parse_embed_json(json_file):
embeds_json = loads(json_file)['embeds']
for embed_json in embeds_json:
embed = Embed().from_dict(embed_json)
yield embed
# And the main code looks like this
with open("bot/embeds/temp_ban_embeds.json", "r") as file:
temp_ban_embeds = parse_embed_json(file.read())
for embed in temp_ban_embeds:
await ctx.send(embed=embed)
于是我找到了一个特殊的方法from_dict
。它似乎适用于我们可以从 json.loads
方法获得的字典。因此您可以找到文档 here。