【发布时间】:2019-04-12 13:39:06
【问题描述】:
据我了解,我可以使用 GIPHY 文档 (https://gyazo.com/1b6c0094162a54fe49029f665badf8df) 中的这个示例来打开一个 url,但我不太了解它。补充一点,当我运行这段代码时,我得到了错误:
discord.ext.commands.errors.CommandInvokeError:命令引发异常: AttributeError: 模块 'urllib' 没有属性 'urlopen'
我的问题是,一旦用户在文本通道中键入#giphy,我如何才能从某些标签中随机导入 GIF
这是我当前的代码:(代码已更新)
@bot.command(pass_context = True)
@commands.cooldown(1, 3, commands.BucketType.user)
async def gif(ctx, *, search):
channel = ctx.message.channel
session = aiohttp.ClientSession()
msg = await bot.send_message(channel, "**searching for " + search + "..**")
randomMessage = await bot.send_message(channel, "**showing a random image due to no images found from your search or you just didn't search anything**")
if search == "":
randomImage = True
print("random")
randomMessage
response = await session.get("https://api.giphy.com/v1/gif/random?api_keyY=4hnrG09EqYcNnv63Sj2gJvmy9ilDPx5&limit=10")
else:
msg
print("searching")
correct_search = search.replace(" ", "+")
reponse = await session.get("http://api.giphy.com/v1/gifs/search?q=" + correct_search + "&api_key=Y4hnrG09EqYcNnv63Sj2gJvmy9ilDPx5&limit=10")
data = json.loads(await reponse.text())
await session.close()
embed = discord.Embed(
description = '**showing result for ' + search + '**',
colour = discord.Colour.blue()
)
gif_choice = random.randint(0,9)
embed.set_image(url=data["data"][gif_choice]["images"]["original"]["url"])
if randomImage:
await bot.delete_message(randomMessage)
else:
await bot.delete_message(msg)
await bot.send_message(channel, embed=embed)
谢谢
【问题讨论】:
-
您可以尝试以问题的形式表达您的问题吗?
-
哎呀我完全忘记问这个问题了哈哈
-
您的导入是什么样的?您可能正在寻找
urllib.request.urlopen -
我已经更新了我的代码并修复了我的导入,但我似乎仍然无法让它工作。它输出:(文件路径)第 96 行,在 giphy await bot.send_message(channel, embed=embed) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\discord\ client.py”,第 1152 行,在 send_message 中数据 = 来自 self.http.send_message(channel_id,内容,guild_id=guild_id,tts=tts,embed=embed
标签: python bots discord discord.py giphy