当用户输入命令“!gif'(他们想要的gif在哪里),我希望机器人返回该名称的gif
我正在尝试的代码是基于Tenor的API,但我不介意使用任何其他站点来获取GIF。我现有的代码不起作用,我对如何做这件事有点不知所措
注意:我是作为个人项目来做的。我不介意你给我答案,但如果可以,请解释你为什么要这么做
以下是我目前的代码:
import discord
import requests
import json
from constants import (TenorToken, DiscordToken)
client = discord.Client()
embedColour = 0xff0000
#Retrieves GIF from site
def getGIF(searchTerm):
response = requests.get("https://g.tenor.com/v1/search?q=%s&key=%s&limit=1" %(searchTerm, TenorToken))
gif = json.loads(response.content)
print(gif)
@client.event
async def on_ready():
print(f"{client.user}"[:-5] + " is now Online!")
@client.event
async def on_message(message):
if (message.author == client.user):
return
if (message.content.lower().startswith(f"{CommandKey}gif")):
getGIF(message.content.lower()[5:]) #Collects word after !gif
client.run(DiscordToken)
谢谢你的帮助
我检查了您的代码,并且
Tensor
不发送GIF数据,只发送图像的URL这段代码在第一个结果`
我通过手动挖掘
JSON
并创建此代码来获得它当我得到
url
时,我可以使用Embed
发送url
,discord将下载并显示它-我不必下载它完整工作代码
编辑:
下载
gif
到本地计算机内存(使用io.BytesIO
)并作为普通文件发送时也是如此它需要更长的时间来显示,因为它必须将其传送到本地计算机,然后发送到internet
它可用于使用pillow、wand、gizeh编辑图像(即添加文本、更改颜色、绘制图形),使用
opencv
检测人脸/人物/对象,使用moviepy生成视频等顺便说一句:
在回答问题How to make a canvas profile card in discord python bot?时,我使用
pillow
向图像添加元素。但它是静态图像,而不是动画GIF,这需要更多的工作(每一帧都是单独的)相关问题 更多 >
编程相关推荐