Created
May 8, 2021 23:53
-
-
Save Jigoku/949746e6840bc03f3671934c647fba28 to your computer and use it in GitHub Desktop.
ufobot UD command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
import discord | |
import json | |
import requests | |
#import logging | |
import asyncio | |
from datetime import datetime | |
api_term = "https://api.urbandictionary.com/v0/define?term=" | |
api_rand = "https://api.urbandictionary.com/v0/random" | |
class Urban(commands.Cog): | |
def __init__(self, bot): | |
self.bot = bot | |
@commands.command( | |
name= "urban", | |
description = "Look up definitions on the Urban Dictionary. If no argument is supplied, picks a random definition.", | |
aliases=["ud"], | |
usage = "<query>" | |
) | |
@commands.cooldown(1, 1, commands.BucketType.user) | |
async def ud_command(self, ctx, *, query=None): | |
try: | |
if query is None: | |
response = requests.get(api_rand) | |
else: | |
response = requests.get(api_term + query) | |
results = response.json() | |
if "error" in results: | |
await ctx.send(":warning: "+results["error"]) | |
return | |
cur_page = 1 | |
pages = len(results["list"]) | |
if pages < 1: | |
await ctx.send(":warning: No results") | |
return | |
def format_ud(cur_page, pages): | |
result = results["list"][cur_page-1] | |
embed = discord.Embed( | |
title=":alien: " + result["word"] + " :flying_saucer:", | |
color=0x0049D4, | |
url = result["permalink"], | |
) | |
embed.set_thumbnail( | |
url="https://i.imgur.com/WF1YxzR.png" | |
) | |
# field limit value to 1024 chars | |
if query is None: | |
embed.add_field( | |
name="Random Definition", | |
value=result["definition"][0:1024], | |
inline=False | |
) | |
else: | |
embed.add_field( | |
name="Definition" + " (" + str(cur_page) + "/" + str(pages) + ")", | |
value=result["definition"][0:1024], | |
inline=False | |
) | |
embed.add_field( | |
name="Example", | |
value=result["example"][0:1024] or "N/A", | |
inline=False | |
) | |
embed.add_field( | |
name="Votes", | |
value=":thumbsup:" + str(result["thumbs_up"]) + " | " ":thumbsdown:" + str(result["thumbs_down"]), | |
inline=True | |
) | |
utc_dt = datetime.strptime(result["written_on"], '%Y-%m-%dT%H:%M:%S.%fZ') | |
embed.add_field( | |
name = "Date", | |
value = utc_dt.strftime("%Y-%m-%d"), | |
inline = True | |
) | |
embed.set_footer( | |
text = f"Requested by {ctx.author.name}", | |
icon_url = ctx.author.avatar_url | |
) | |
return embed | |
message = await ctx.send(embed=format_ud(cur_page, pages)) | |
for i in range(0, len(results["list"][cur_page-1]["sound_urls"])): | |
r = requests.get(results["list"][cur_page-1]["sound_urls"][i], stream = True) | |
with open ("data/sound.wav", "wb") as file: | |
for chunk in r.iter_content(chunk_size=16*1024): | |
file.write(chunk) | |
await ctx.send(file=discord.File("data/sound.wav")) | |
break | |
if query is None: | |
return | |
await message.add_reaction("◀️") | |
await message.add_reaction("▶️") | |
def check(reaction, user): | |
return user == ctx.author and str(reaction.emoji) in ["◀️", "▶️"] | |
while True: | |
try: | |
# wait for reaction, time out after x seconds | |
reaction, user = await self.bot.wait_for("reaction_add", timeout=10, check=check) | |
if str(reaction.emoji) == "▶️" and cur_page != pages: | |
cur_page += 1 | |
await message.edit(embed=format_ud(cur_page, pages)) | |
elif str(reaction.emoji) == "◀️" and cur_page > 1: | |
cur_page -= 1 | |
await message.edit(embed=format_ud(cur_page, pages)) | |
else: | |
await message.remove_reaction(reaction, user) | |
except asyncio.TimeoutError: | |
try: | |
await message.clear_reactions() | |
break | |
except: | |
pass | |
except Exception as e: | |
# await ctx.send(":warning: Kratom was depleted\n ```python\n" + logging.traceback.format_exc() + "```") | |
await ctx.send(":warning: Can't find definition.") | |
return | |
def setup(bot): | |
bot.add_cog(Urban(bot)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment