簡體   English   中英

JSON 嵌入 Discord.PY

[英]JSON to Embed Discord.PY

我目前正在 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 更改為漂亮且有條理的嵌入式消息。 我怎樣才能做到這一點? 謝謝。

Command response.json() convert JSON string to PYthon data (like list, dictionary` and when you use f-string then you convert Python data to string. If you want format it then you could use json.dumps(data, indent= 2) 將 Python 數據轉換回 JSON 字符串。或者使用 pprint 等模塊(漂亮的打印)

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM