Discord.py使用嵌入显示字典中的信息


import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
import requests
import bloons
bot = commands.Bot(command_prefix="$")
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
@bot.command("info")
async def displayembed(ctx,args):
try:
info = bloons.data["monkeys"][args]["Description"]
monkey_name = bloons.data["monkeys"][args]
monkey_img = bloons.data["monkeys"][args]["Image"]
except KeyError:
return "You spelled the name incorrectly try again"
else:
embed = discord.Embed(
title=monkey_name, #using name grabbed from dictionary
description=info, #using info grabbed from dictionary
)
embed.set_footer(text="This is a footer.")
embed.set_image(url=monkey_img) #using image grabbed from dictionary
embed.set_thumbnail(
url="https://www.google.com/imgres?imgurl=https%3A%2F%2Fimg.wattpad.com%2Fcover%2F120707245-256-k661817.jpg&imgrefurl=https%3A%2F%2Fwww.wattpad.com%2F460990650-how-to-be-a-kawaii-mango-step-1&tbnid=AyEg5rQ9APuWAM&vet=10CAMQxiAoAGoXChMI8PjRsreK7gIVAAAAAB0AAAAAEAY..i&docid=VKow1SDnSKMNOM&w=256&h=400&itg=1&q=kawaii%20mango&ved=0CAMQxiAoAGoXChMI8PjRsreK7gIVAAAAAB0AAAAAEAY")
embed.set_author(name="Author Name",
icon_url="https://www.google.com/imgres?imgurl=https%3A%2F%2Fimg.wattpad.com%2Fcover%2F120707245-256-k661817.jpg&imgrefurl=https%3A%2F%2Fwww.wattpad.com%2F460990650-how-to-be-a-kawaii-mango-step-1&tbnid=AyEg5rQ9APuWAM&vet=10CAMQxiAoAGoXChMI8PjRsreK7gIVAAAAAB0AAAAAEAY..i&docid=VKow1SDnSKMNOM&w=256&h=400&itg=1&q=kawaii%20mango&ved=0CAMQxiAoAGoXChMI8PjRsreK7gIVAAAAAB0AAAAAEAY")
embed.add_field(name="Field Name", value="Field Value", inline=False)
embed.add_field(name="Field Name", value="Field Value", inline=True)
embed.add_field(name="Field Name", value="Field Value", inline=True)
await ctx.send(embed=embed)

bot.run(TOKEN)

这是我试图从获取信息的词典

data={
"monkeys":{
"Quincy":{
"Description":"Proud, strong and intelligent, Quincy uses his bow to perform feats of amazing skill.",
"Image":"https://static.wikia.nocookie.net/b__/images/a/a8/QuincyPortrait.png/revision/latest?cb=20190612021048&path-prefix=bloons"
}
}

我收到的错误:

Ignoring exception in command info:
Traceback (most recent call last):
File "C:UsersVimalananaconda3envstutoriallibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/Vimalan/PycharmProjects/100CODECHALLENGE/discordbot/main.py", line 60, in displayembed
await ctx.send(embed=embed)
File "C:UsersNameanaconda3envstutoriallibsite-packagesdiscordabc.py", line 891, in send
nonce=nonce, allowed_mentions=allowed_mentions)
File "C:UsersNameanaconda3envstutoriallibsite-packagesdiscordhttp.py", line 245, in request
raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In embed.title: Could not interpret "{'Description': 'Proud, strong and intelligent, Quincy uses his bow to perform feats of amazing skill.', 'Image': 'https://static.wikia.nocookie.net/b__/images/a/a8/QuincyPortrait.png/revision/latest?cb=20190612021048&path-prefix=bloons'}" as string.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersNameanaconda3envstutoriallibsite-packagesdiscordextcommandsbot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:UsersNameanaconda3envstutoriallibsite-packagesdiscordextcommandscore.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersVimalananaconda3envstutoriallibsite-packagesdiscordextcommandscore.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In embed.title: Could not interpret "{'Description': 'Proud, strong and intelligent, Quincy uses his bow to perform feats of amazing skill.', 'Image': 'https://static.wikia.nocookie.net/b__/images/a/a8/QuincyPortrait.png/revision/latest?cb=20190612021048&path-prefix=bloons'}" as string.

我试图做的是从我创建的字典中获取信息,字典中列出了猴子的名字、描述和图像。我想做的是,当用户输入$info "monkey name"时,它会用给定的信息填充嵌入模板并显示它。但我在尝试这样做时遇到了很多错误,我不太确定如何修复它们。

它是在访问字典,而不是获取猴子的名字,这可能导致了错误。我应该把arg本身作为标题。。。

最新更新