据我了解,我可以使用 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)
谢谢