简体   繁体   中英

JSON to Embed Discord.PY

I'm currently writing a bot in discord.py and I need some help with making the JSON response into an embedded message that the bot sends. Here's the code:

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()))

When running the code, I get:

{'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} 

I want to change all that JSON into a nice and organised embedded message. How can I do that? Thanks.

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) to convert Python data back to JSON string. Or use modules like 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 json

text = json.dumps(data, indent=2)

print(text)

Result:

{
  "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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM