mirror of https://github.com/stijndcl/didier
				
				
				
			Clean up define, create define slash command
							parent
							
								
									c47f908e57
								
							
						
					
					
						commit
						358f8693dd
					
				|  | @ -1,11 +1,8 @@ | ||||||
| import os | from data.embeds.urban_dictionary import Definition | ||||||
| 
 |  | ||||||
| from decorators import help | from decorators import help | ||||||
| import discord |  | ||||||
| from discord.ext import commands | from discord.ext import commands | ||||||
| from enums.help_categories import Category | from enums.help_categories import Category | ||||||
| from functions import checks | from functions import checks | ||||||
| import requests |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Define(commands.Cog): | class Define(commands.Cog): | ||||||
|  | @ -19,99 +16,15 @@ class Define(commands.Cog): | ||||||
|     @commands.command(name="Define", aliases=["UrbanDictionary", "Ud"], usage="[Woord]") |     @commands.command(name="Define", aliases=["UrbanDictionary", "Ud"], usage="[Woord]") | ||||||
|     @commands.check(checks.allowedChannels) |     @commands.check(checks.allowedChannels) | ||||||
|     @help.Category(category=Category.Other) |     @help.Category(category=Category.Other) | ||||||
|     async def define(self, ctx, *words): |     async def define(self, ctx, *, query): | ||||||
|         """ |         """ | ||||||
|         Command that looks up the definition of a word in the Urban Dictionary. |         Command that looks up the definition of a word in the Urban Dictionary. | ||||||
|         :param ctx: Discord Context |         :param ctx: Discord Context | ||||||
|         :param words: Word(s) to look up |         :param query: Word(s) to look up | ||||||
|         """ |         """ | ||||||
|         words = list(words) |         embed = Definition(query).to_embed() | ||||||
|         if len(words) == 0: |  | ||||||
|             return await ctx.send("Controleer je argumenten.") |  | ||||||
| 
 |  | ||||||
|         query = " ".join(words) |  | ||||||
|         answer = self.lookup(query) |  | ||||||
| 
 |  | ||||||
|         embed = discord.Embed(colour=discord.Colour.from_rgb(220, 255, 0)) |  | ||||||
|         embed.set_author(name="Urban Dictionary") |  | ||||||
| 
 |  | ||||||
|         embed.add_field(name="Woord", value=answer["word"], inline=True) |  | ||||||
|         embed.add_field(name="Auteur", value=answer["author"], inline=True) |  | ||||||
|         embed.add_field(name="Definitie", value=self.cleanString(answer["definition"]), inline=False) |  | ||||||
|         embed.add_field(name="Voorbeeld", value=self.cleanString(answer["example"]), inline=False) |  | ||||||
|         embed.add_field(name="Rating", value=str(round(self.ratio(answer), 2)) + "%") |  | ||||||
|         embed.add_field(name="Link naar de volledige definitie", |  | ||||||
|                         value="[Urban Dictionary]({})".format(str(answer["link"]))) |  | ||||||
| 
 |  | ||||||
|         await ctx.send(embed=embed) |         await ctx.send(embed=embed) | ||||||
| 
 | 
 | ||||||
|     def lookup(self, word): |  | ||||||
|         """ |  | ||||||
|         Function that sends the API request to get the definition. |  | ||||||
|         :param word: the woord to look up |  | ||||||
|         :return: a dictionary representing the info of this word |  | ||||||
|         """ |  | ||||||
|         url = "https://mashape-community-urban-dictionary.p.rapidapi.com/define" |  | ||||||
| 
 |  | ||||||
|         querystring = {"term": word} |  | ||||||
| 
 |  | ||||||
|         headers = { |  | ||||||
|             'x-rapidapi-host': "mashape-community-urban-dictionary.p.rapidapi.com", |  | ||||||
|             'x-rapidapi-key': os.getenv("URBANDICTIONARY") |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         try: |  | ||||||
|             if word.lower() == "didier": |  | ||||||
|                 raise Exception |  | ||||||
| 
 |  | ||||||
|             response = requests.request("GET", url, headers=headers, params=querystring).json()["list"] |  | ||||||
| 
 |  | ||||||
|             if len(response) > 0: |  | ||||||
|                 return {"word": response[0]["word"], "definition": response[0]["definition"], |  | ||||||
|                         "example": response[0]["example"], "thumbs_up": response[0]["thumbs_up"], |  | ||||||
|                         "thumbs_down": response[0]["thumbs_down"], "link": response[0]["permalink"], |  | ||||||
|                         "author": response[0]["author"]} |  | ||||||
| 
 |  | ||||||
|             # No valid response |  | ||||||
|             return self.defineDidier() |  | ||||||
|         except Exception: |  | ||||||
|             return self.defineDidier() |  | ||||||
| 
 |  | ||||||
|     def cleanString(self, text: str): |  | ||||||
|         """ |  | ||||||
|         Function that cuts off definitions that are too long & strips out UD markdown |  | ||||||
|         from an input string. |  | ||||||
|         :param text: the input string to clean up |  | ||||||
|         :return: the edited version of the string |  | ||||||
|         """ |  | ||||||
|         text = text.replace("[", "") |  | ||||||
|         text = text.replace("]", "") |  | ||||||
| 
 |  | ||||||
|         if not text: |  | ||||||
|             return "N/A" |  | ||||||
| 
 |  | ||||||
|         return text if len(text) < 1024 else text[:1021] + "..." |  | ||||||
| 
 |  | ||||||
|     def ratio(self, dic): |  | ||||||
|         """ |  | ||||||
|         Function that alculates the upvote/downvote ratio of the definition. |  | ||||||
|         :param dic: the dictionary representing the definition |  | ||||||
|         :return: the upvote/downvote ratio (float) |  | ||||||
|         """ |  | ||||||
|         return (100 * int(dic["thumbs_up"])) / (int(dic["thumbs_up"]) + int(dic["thumbs_down"])) \ |  | ||||||
|             if int(dic["thumbs_down"]) != 0 else 100.0 |  | ||||||
| 
 |  | ||||||
|     def defineDidier(self): |  | ||||||
|         """ |  | ||||||
|         Function that returns a stock dictionary to define Didier |  | ||||||
|         in case people call it, or no definition was found. |  | ||||||
|         :return: a dictionary that defines Didier |  | ||||||
|         """ |  | ||||||
|         return {"word": "Didier", "definition": "Didier", "example": "1: Didier\n2: Hmm?", "thumbs_up": 69420, |  | ||||||
|                 "thumbs_down": 0, "author": "Didier", |  | ||||||
|                 "link": "https://upload.wikimedia.org/wikipedia/commons/a/a5" |  | ||||||
|                         "/Didier_Reynders_in_Iranian_Parliament_02.jpg"} |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| def setup(client): | def setup(client): | ||||||
|     client.add_cog(Define(client)) |     client.add_cog(Define(client)) | ||||||
|  |  | ||||||
|  | @ -0,0 +1,25 @@ | ||||||
|  | from discord.ext import commands | ||||||
|  | from dislash import SlashInteraction, slash_command, Option, OptionType | ||||||
|  | 
 | ||||||
|  | from data.embeds.urban_dictionary import Definition | ||||||
|  | from startup.didier import Didier | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class DefineSlash(commands.Cog): | ||||||
|  |     def __init__(self, client: Didier): | ||||||
|  |         self.client: Didier = client | ||||||
|  | 
 | ||||||
|  |     @slash_command(name="define", | ||||||
|  |                    description="Urban Dictionary", | ||||||
|  |                    options=[ | ||||||
|  |                      Option("query", "Search query", OptionType.STRING, required=True) | ||||||
|  |                    ], | ||||||
|  |                    guild_ids=[728361030404538488, 880175869841277008] | ||||||
|  |                    ) | ||||||
|  |     async def _define_slash(self, interaction: SlashInteraction, query): | ||||||
|  |         embed = Definition(query).to_embed() | ||||||
|  |         await interaction.reply(embed=embed) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def setup(client: Didier): | ||||||
|  |     client.add_cog(DefineSlash(client)) | ||||||
|  | @ -0,0 +1,113 @@ | ||||||
|  | import os | ||||||
|  | from typing import Dict | ||||||
|  | 
 | ||||||
|  | import discord | ||||||
|  | import requests | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class Definition: | ||||||
|  |     def __init__(self, query: str): | ||||||
|  |         self.query = query | ||||||
|  |         self.definition = Definition.lookup(query) | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def lookup(word) -> Dict: | ||||||
|  |         """ | ||||||
|  |         Function that sends the API request to get the definition. | ||||||
|  |         :param word: the woord to look up | ||||||
|  |         :return: a dictionary representing the info of this word | ||||||
|  |         """ | ||||||
|  |         url = "https://mashape-community-urban-dictionary.p.rapidapi.com/define" | ||||||
|  | 
 | ||||||
|  |         querystring = {"term": word} | ||||||
|  | 
 | ||||||
|  |         headers = { | ||||||
|  |             'x-rapidapi-host': "mashape-community-urban-dictionary.p.rapidapi.com", | ||||||
|  |             'x-rapidapi-key': os.getenv("URBANDICTIONARY") | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         try: | ||||||
|  |             if word.lower() == "didier": | ||||||
|  |                 raise Exception | ||||||
|  | 
 | ||||||
|  |             response = requests.get(url, headers=headers, params=querystring).json()["list"] | ||||||
|  | 
 | ||||||
|  |             if len(response) > 0: | ||||||
|  |                 return {"word": response[0]["word"], "definition": response[0]["definition"], | ||||||
|  |                         "example": response[0]["example"], "thumbs_up": response[0]["thumbs_up"], | ||||||
|  |                         "thumbs_down": response[0]["thumbs_down"], "link": response[0]["permalink"], | ||||||
|  |                         "author": response[0]["author"]} | ||||||
|  | 
 | ||||||
|  |             # No valid response | ||||||
|  |             return {} | ||||||
|  |         except Exception: | ||||||
|  |             return Definition.define_didier() | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def clean_string(text: str): | ||||||
|  |         """ | ||||||
|  |         Function that cuts off definitions that are too long & strips out UD markdown | ||||||
|  |         from an input string. | ||||||
|  |         :param text: the input string to clean up | ||||||
|  |         :return: the edited version of the string | ||||||
|  |         """ | ||||||
|  |         text = text.replace("[", "") | ||||||
|  |         text = text.replace("]", "") | ||||||
|  | 
 | ||||||
|  |         if not text: | ||||||
|  |             return "N/A" | ||||||
|  | 
 | ||||||
|  |         return text if len(text) < 1024 else text[:1021] + "..." | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def ratio(dic) -> float: | ||||||
|  |         """ | ||||||
|  |         Function that alculates the upvote/downvote ratio of the definition. | ||||||
|  |         :param dic: the dictionary representing the definition | ||||||
|  |         :return: the upvote/downvote ratio (float) | ||||||
|  |         """ | ||||||
|  |         return (100 * int(dic["thumbs_up"])) / (int(dic["thumbs_up"]) + int(dic["thumbs_down"])) \ | ||||||
|  |             if int(dic["thumbs_down"]) != 0 else 100.0 | ||||||
|  | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def define_didier() -> Dict: | ||||||
|  |         """ | ||||||
|  |         Function that returns a stock dictionary to define Didier | ||||||
|  |         in case people call it, or no definition was found. | ||||||
|  |         :return: a dictionary that defines Didier | ||||||
|  |         """ | ||||||
|  |         return {"word": "Didier", "definition": "Didier", "example": "1: Didier\n2: Hmm?", "thumbs_up": 69420, | ||||||
|  |                 "thumbs_down": 0, "author": "Didier", | ||||||
|  |                 "link": "https://upload.wikimedia.org/wikipedia/commons/a/a5" | ||||||
|  |                         "/Didier_Reynders_in_Iranian_Parliament_02.jpg"} | ||||||
|  | 
 | ||||||
|  |     def to_embed(self) -> discord.Embed: | ||||||
|  |         """ | ||||||
|  |         Create an embed for this definition | ||||||
|  |         """ | ||||||
|  |         # No results found | ||||||
|  |         if not self.definition: | ||||||
|  |             return self._nothing_found_embed() | ||||||
|  | 
 | ||||||
|  |         embed = discord.Embed(colour=discord.Colour.from_rgb(220, 255, 0)) | ||||||
|  |         embed.set_author(name="Urban Dictionary") | ||||||
|  | 
 | ||||||
|  |         embed.add_field(name="Woord", value=self.definition["word"], inline=True) | ||||||
|  |         embed.add_field(name="Auteur", value=self.definition["author"], inline=True) | ||||||
|  |         embed.add_field(name="Definitie", value=Definition.clean_string(self.definition["definition"]), inline=False) | ||||||
|  |         embed.add_field(name="Voorbeeld", value=Definition.clean_string(self.definition["example"]), inline=False) | ||||||
|  |         embed.add_field(name="Rating", value=str(round(Definition.ratio(self.definition), 2)) + "%") | ||||||
|  |         embed.add_field(name="Link naar de volledige definitie", | ||||||
|  |                         value="[Urban Dictionary]({})".format(str(self.definition["link"]))) | ||||||
|  | 
 | ||||||
|  |         return embed | ||||||
|  | 
 | ||||||
|  |     def _nothing_found_embed(self) -> discord.Embed: | ||||||
|  |         """ | ||||||
|  |         Special embed when no results could be found | ||||||
|  |         """ | ||||||
|  |         embed = discord.Embed(colour=discord.Colour.red(), title=self.query[:256]) | ||||||
|  |         embed.set_author(name="Urban Dictionary") | ||||||
|  |         embed.description = "Geen resultaten gevonden" | ||||||
|  | 
 | ||||||
|  |         return embed | ||||||
		Loading…
	
		Reference in New Issue