JSON 嵌入 Discord.PY
Posted
技术标签:
【中文标题】JSON 嵌入 Discord.PY【英文标题】:JSON to Embed Discord.PY 【发布时间】:2022-01-01 07:20:17 【问题描述】:我目前正在 discord.py 中编写一个机器人,我需要一些帮助来将 JSON 响应转换为机器人发送的嵌入式消息。代码如下:
import discord
import requests
import json
from discord.ext.commands import Bot
bot = Bot("!")
client = discord.Client()
url = "https://api-mainnet.magiceden.io/collections/angomon"
response = requests.get(url)
json_response = response.json()
@client.event
async def on_ready():
print(f'We have logged in as client.user')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('test'):
await message.channel.send(f'```response.json() ```')
@bot.command()
async def displayembed():
embed = discord.Embed(
title = 'Test',
colour = discord.Color.blue()
)
embed.set_footer(text='Test')
embed.add_field(name='Details', value = response, inline=False)
await client.say(embed=embed)
@bot.command(name='list')
async def cmd_list(ctx):
with open(response.json(), 'r') as read_file:
users = json.load(read_file)
embedlist = discord.Embed(title='Title', description='User List')
embedlist.add_field(name='User Name', value=join(users.values()))
embedlist.add_field(name='User ID', value=join(users.keys()))
运行代码时,我得到:
'symbol': 'angomon', 'candyMachineIds': ['Exwuvrjz11h3pXgEt7AiYGRTstTcVhtBFhmbWGY9K569', '58Z9dKEcr9V6EPMWRnTzUzV8vtvURcgg1DsM9duLDnUW'], 'name': 'Angomon', 'image': 'https://dl.airtable.com/.attachmentThumbnails/4a51d0f3422fa43b3fae90c20cb11a48/88cf3e7e', 'description': 'Angomon are 3500 snazzy inhabitants of the Angoverse', 'createdAt': '2021-11-20T18:20:11.637Z', 'enabledAttributesFilters': True, 'hasAllItems': False
我想将所有 JSON 更改为一个漂亮且有条理的嵌入式消息。我怎样才能做到这一点?谢谢。
【问题讨论】:
欢迎来到 Stack Overflow。该特定输出的结果应该是什么样的?我们无法为您决定问题规格。如果您想与人们聊天以找出一个设计,那么您需要一个讨论论坛,但事实并非如此。考虑尝试 Reddit 或 Quora。 命令response.json()
将 JSON 字符串转换为 PYthon 数据(如列表、字典`,当您使用 f-string
时,您会将 Python 数据转换为字符串。如果您想要格式化它,那么您可以使用 @987654325 @ 将 Python 数据转换回 JSON 字符串。或使用 pprint
(pretty print
) 等模块
【参考方案1】:
命令response.json()
将JSON string
转换为Python data
(使用list
、dictionary
等),当您使用f-string
时,它会使用标准方法将Python data
转换为string
。
如果你想格式化它,那么你可以使用json.dumps(data, indent=2)
将Python data
转换回JSON string
。
data = 'symbol': 'angomon', 'candyMachineIds': ['Exwuvrjz11h3pXgEt7AiYGRTstTcVhtBFhmbWGY9K569', '58Z9dKEcr9V6EPMWRnTzUzV8vtvURcgg1DsM9duLDnUW'], 'name': 'Angomon', 'image': 'https://dl.airtable.com/.attachmentThumbnails/4a51d0f3422fa43b3fae90c20cb11a48/88cf3e7e', 'description': 'Angomon are 3500 snazzy inhabitants of the Angoverse', 'createdAt': '2021-11-20T18:20:11.637Z', 'enabledAttributesFilters': True, 'hasAllItems': False
import json
text = json.dumps(data, indent=2)
print(text)
结果:
"symbol": "angomon",
"candyMachineIds": [
"Exwuvrjz11h3pXgEt7AiYGRTstTcVhtBFhmbWGY9K569",
"58Z9dKEcr9V6EPMWRnTzUzV8vtvURcgg1DsM9duLDnUW"
],
"name": "Angomon",
"image": "https://dl.airtable.com/.attachmentThumbnails/4a51d0f3422fa43b3fae90c20cb11a48/88cf3e7e",
"description": "Angomon are 3500 snazzy inhabitants of the Angoverse",
"createdAt": "2021-11-20T18:20:11.637Z",
"enabledAttributesFilters": true,
"hasAllItems": false
或者您可以使用pprint
(pretty print
) 之类的模块。
data = 'symbol': 'angomon', 'candyMachineIds': ['Exwuvrjz11h3pXgEt7AiYGRTstTcVhtBFhmbWGY9K569', '58Z9dKEcr9V6EPMWRnTzUzV8vtvURcgg1DsM9duLDnUW'], 'name': 'Angomon', 'image': 'https://dl.airtable.com/.attachmentThumbnails/4a51d0f3422fa43b3fae90c20cb11a48/88cf3e7e', 'description': 'Angomon are 3500 snazzy inhabitants of the Angoverse', 'createdAt': '2021-11-20T18:20:11.637Z', 'enabledAttributesFilters': True, 'hasAllItems': False
import pprint
text = pprint.pformat(data, indent=2)
print(text)
结果:
'candyMachineIds': [ 'Exwuvrjz11h3pXgEt7AiYGRTstTcVhtBFhmbWGY9K569',
'58Z9dKEcr9V6EPMWRnTzUzV8vtvURcgg1DsM9duLDnUW'],
'createdAt': '2021-11-20T18:20:11.637Z',
'description': 'Angomon are 3500 snazzy inhabitants of the Angoverse',
'enabledAttributesFilters': True,
'hasAllItems': False,
'image': 'https://dl.airtable.com/.attachmentThumbnails/4a51d0f3422fa43b3fae90c20cb11a48/88cf3e7e',
'name': 'Angomon',
'symbol': 'angomon'
或者您应该编写自己的代码来自行格式化。
【讨论】:
以上是关于JSON 嵌入 Discord.PY的主要内容,如果未能解决你的问题,请参考以下文章