mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 19:28:54 -05:00
Randomize !gif results
Let the bot pick a random result out of the returned giphy array. Make sure to not choose a number higher than the length of the array.
This commit is contained in:
parent
6ba43bb86a
commit
c3febe0746
@ -3,6 +3,7 @@ from discord.ext import commands
|
|||||||
from random import randint
|
from random import randint
|
||||||
from imgurpython import ImgurClient
|
from imgurpython import ImgurClient
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import random
|
||||||
|
|
||||||
class Image:
|
class Image:
|
||||||
"""Image related commands."""
|
"""Image related commands."""
|
||||||
@ -45,6 +46,7 @@ class Image:
|
|||||||
|
|
||||||
@commands.command(no_pm=True)
|
@commands.command(no_pm=True)
|
||||||
async def gif(self, *text):
|
async def gif(self, *text):
|
||||||
|
random.seed()
|
||||||
""" gif [keyword] - retrieves first search result from giphy """
|
""" gif [keyword] - retrieves first search result from giphy """
|
||||||
if len(text) > 0:
|
if len(text) > 0:
|
||||||
if len(text[0]) > 1 and len(text[0]) < 20:
|
if len(text[0]) > 1 and len(text[0]) < 20:
|
||||||
@ -54,7 +56,8 @@ class Image:
|
|||||||
async with aiohttp.get(search) as r:
|
async with aiohttp.get(search) as r:
|
||||||
result = await r.json()
|
result = await r.json()
|
||||||
if result["data"] != []:
|
if result["data"] != []:
|
||||||
url = result["data"][0]["url"]
|
maxarray = len(result)
|
||||||
|
url = result["data"][random.randint(0,maxarray)]["url"]
|
||||||
await self.bot.say(url)
|
await self.bot.say(url)
|
||||||
else:
|
else:
|
||||||
await self.bot.say("Your search terms gave no results.")
|
await self.bot.say("Your search terms gave no results.")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user