问题描述
尝试在我的 Discord Bot 上使用 discord.Embed 时收到错误消息。
2018-09-16T15:46:20.254940+00:00 app[worker.1]:忽略命令 dave 中的异常 2018-09-16T15:46:20.256521+00:00 app[worker.1]:Traceback(最近的调用)最后):2018-09-16T15:46:20.265538+00:00 app[worker.1]:文件“/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core .py”,第 50 行,包装后 2018-09-16T15:46:20.265541+00:00 app[worker.1]: ret = yield from coro(*args, **kwargs) 2018-09-16T15:46: 20.265590+00:00 app[worker.1]: 文件“bot.py”,第 83 行,在 dave 2018-09-16T15:46:20.265592+00:00 app[worker.1]: embed = discord.Embed( title=data['output'], colour=discord.Colour(0x99cc)) 2018-09-16T15:46:20.265669+00:00 app[worker.1]: AttributeError: module 'discord' has no attribute 'Embed' 2018-09-16T15:46:20.265720+00:00 app[worker.1]: 2018-09-16T15:46:20.265722+00:00 app[worker.1]:上述异常是以下的直接原因例外:2018-09-16T15:46:20.265723+00:00 app[worker.1]: 2018-09-16T15:46:20.265765+00:00 app[worker.1]: Traceback(最近的 c 最后):2018-09-16T15:46:20.265840+00:00 app[worker.1]:文件“/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/ bot.py”,第 822 行,在 process_commands 2018-09-16T15:46:20.265842+00:00 app[worker.1]: yield from command.invoke(ctx) 2018-09-16T15:46:20.265885+065885 00 app[worker.1]:文件“/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py”,第367行,在调用2018-09-16T15: 46:20.265887+00:00 app[worker.1]: yield from injection(*ctx.args, **ctx.kwargs) 2018-09-16T15:46:20.265928+00:00 app[worker.1]: File “/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py”,第 52 行,包装在 2018-09-16T15:46:20.265930+00:00 应用程序中[ worker.1]:从 e 2018-09-16T15:46:20.265995+00:00 app[worker.1]:discord.ext.commands.errors.CommandInvokeError:Command引发异常:AttributeError:模块引发CommandInvokeError(e) 'discord' 没有属性 'Embed'
是控制台中的完整输出。
我的相关代码如下:
from discord.ext.commands import Bot
from discord.ext import commands
import discord
import json
import random
import requests
client = Bot(description="My Bot", command_prefix="~")
@client.command(pass_context=True)
async def dave(ctx, *, query = ""):
''' Talks to Dave '''
if query == "":
await client.say("Sample usage: ```~dave Hello```")
else:
r = requests.get('https://example.com/dave/api.php?discord=' + str(ctx.message.author.id) + '&query=' + str(query))
data = r.json()
data = json.dumps(data)
data = json.loads(data)
if data['status'] == "unknown":
unsure = ["I don't know how to do that yet.", "Hmm... I am unsure.", "Sorry, you appear to have confused me.", "I don't know how to help you with that."]
await client.say(random.choice(unsure))
return
embed = discord.Embed(title=data['output'], colour=discord.Colour(0x99cc))
if data['discordAction'] != None:
embed.set_image(url=data['discordAction'])
embed.set_author(name="Dave", url="https://example.com/dave/", icon_url="https://example.com/Storage_new/2018/small/dave-small.png")
embed.set_footer(text="Powered by Dave", icon_url="https://example.com/drive/uploads/1/bv3gfc333mceuaqw8jlpyrnrfiqi7r/f73h4m12qdxl45n.png")
await client.say(embed=embed)
client.run('intentionally removed')
该文件也称为bot.py
。
代码已更改为示例而不是我的实际域等。
在我介绍了discord.Embed
部分之前,所有代码都有效。
1楼
在我介绍了 discord.Embed 部分之前,所有代码都有效。
我想我知道为什么......它可能已经过时了。
在这里,尝试使用以下命令: pip install --upgrade discord.py
2楼
我认为问题在于您需要导入嵌入,这就是我为修复它所做的: from discord import Embed