如何将GIPHY Python API与discord bot一起使用?

如何将GIPHY Python API与discord bot一起使用?,python,bots,discord,discord.py,giphy,Python,Bots,Discord,Discord.py,Giphy,据我所知,我可以使用GIPHY文档中的这个示例来打开url,但我不太了解它。此外,当我运行此代码时,会出现以下错误: discord.ext.commands.errors.CommandInvokeError:命令引发异常: AttributeError:模块“urllib”没有属性“urlopen” 我的问题是,一旦用户在文本频道中键入giphy,如何从某些标记中随机导入GIF 这是我当前的代码:代码已更新 @bot.command(pass_context = True) @command

据我所知,我可以使用GIPHY文档中的这个示例来打开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)

谢谢

API给出的响应格式为json。您需要对其进行解析,以找到要嵌入的url。加载后,它将成为python中的词典

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

请注意,使用aiohttp是因为它是异步的,这意味着它不会阻塞您的代码。我还修改了命令,以便您可以搜索任何内容。要匹配您以前的请求url,您可以使用!吉菲·瑞安·戈斯林。如果用户未指定搜索值,则将使用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')
        data = json.loads(await response.text())
        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')
        data = json.loads(await response.text())
        gif_choice = random.randint(0, 9)
        embed.set_image(url=data['data'][gif_choice]['images']['original']['url'])

    await session.close()

    await client.send_message(embed=embed)

client.run('token')
此外,不和谐似乎天生支持giphy。在我测试的时候,它已经自己打了电话。我已经用一些不同的字符测试过了!,~,'和空间,它似乎总是工作。参见下面的示例


你能试着用一个问题的形式来表达你的问题吗?哎呀,我完全忘了问这个问题,你的导入是什么样子的?您可能正在寻找我已经更新了代码并修复了导入,但我似乎仍然无法让它工作。它输出:文件路径第96行,在giphy wait bot.send_messagechannel中,嵌入=嵌入文件C:\Program Files x86\Microsoft Visual Studio\Shared\Python36\u 64\lib\site packages\discord\client.py,第1152行,在send_message data=yield from self.http.send_messagechannel_id,content,guild_id=guild_id,tts,非常感谢!我不知道迪斯科像这样支持吉菲,说实话,这很酷。因为我找不到任何方法来绕过discord,所以每当你在句首键入giphy时,我都会自动尝试将文本更改为giphy url,我决定将命令更改为gif,现在它工作得非常好!还有一件事要问,如果用户没有键入任何要搜索的内容,我如何随机搜索GIF?很抱歉问了这么多问题,我仍在学习python,这对我帮助很大,但我测试命令越多,我注意到显示的GIF总是相同的GIF。如果我搜索terry,不管我搜索terry多少次,唯一弹出的gif就是他喊我的acrylics!。我注意到的另一件事是,我无法在搜索中添加空格。搜索Terry Crews会导致一个错误,我如何用ux替换空格?我已经编辑了我的答案,以包含您的两个场景。如果未指定搜索,则使用随机端点。如果指定,则返回10个结果,并随机选择一个。上面的命令已满足空间的要求。这是通过异步def giphyctx中包含的*字符完成的,*,搜索:。没有它,你不能使用空格,或者你必须用引号括起你的参数,例如!吉菲·特里·克鲁斯。这里的文档:感谢不同的结果工作,只有一件事,我仍然无法搜索terry crews。我已经更新了我的原始帖子,以显示我目前拥有的内容。出现以下错误:从e discord.ext.commands.errors.CommandInvokeError:Command引发异常:KeyError:'data'@ceiltechbladhm捕获良好。我的上一次编辑没有正确处理数据,它需要在响应之后进行,所以在这两种情况下都必须进行编辑。还要记住,这是针对旧版本的discord.py,新版本不使用client.send_消息