我如何将GIPHY Python API与不和谐的bot一起使用?

时间:2018-11-09 00:12:32

标签: python bots discord discord.py giphy

根据我的理解,我可以使用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)

谢谢

1 个答案:

答案 0 :(得分:0)

API给出的响应格式为json。您需要解析它以找到您想要嵌入的网址。加载后,它将是python中的字典。

以下代码是如何执行此操作的示例。它将调用giphy API,并返回前10个结果,并将随机选择一个结果作为消息。

请注意,使用aiohttp是异步的,这意味着它不会阻塞您的代码。我还修改了命令,以便您可以搜索任何内容。要匹配先前的请求网址,可以使用!giphy ryan gosling。如果用户未指定搜索值,则将使用giphy随机端点。

from discord.ext import commands
import discord
import json
import aiohttp
import random

client = commands.Bot(command_prefix='!')


@client.command(pass_context=True)
async def giphy(ctx, *, search):
    embed = discord.Embed(colour=discord.Colour.blue())
    session = aiohttp.ClientSession()

    if search == '':
        response = await session.get('https://api.giphy.com/v1/gifs/random?api_key=API_KEY_GOES_HERE')
        embed.set_image(url=data['data']['images']['original']['url'])
    else:
        search.replace(' ', '+')
        response = await session.get('http://api.giphy.com/v1/gifs/search?q=' + search + '&api_key=API_KEY_GOES_HERE&limit=10')
        gif_choice = random.randint(0, 9)
        embed.set_image(url=data['data'][gif_choice]['images']['original']['url'])

    data = json.loads(await response.text())
    await session.close()

    await client.send_message(embed=embed)

client.run('token')

此外,discord本身也支持giphy。在测试时,它已经发出了自己的giphy调用。我已经使用一些不同的字符(!,〜,')和空格进行了测试,它似乎总是可以正常工作。请参见以下示例。

native giphy discord example: using bot prefix !

native giphy discord example: using ~

native giphy discord example using space