From e701249f0f3f2140d339169eec0dccc50a01568d Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 5 Jun 2026 13:41:22 -0700 Subject: [PATCH 1/5] Add a dictionary extension --- techsupport_bot/commands/dictionary.py | 77 ++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 techsupport_bot/commands/dictionary.py diff --git a/techsupport_bot/commands/dictionary.py b/techsupport_bot/commands/dictionary.py new file mode 100644 index 00000000..b7042bde --- /dev/null +++ b/techsupport_bot/commands/dictionary.py @@ -0,0 +1,77 @@ +"""Module for the dictionary extension for the discord bot.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Self + +import discord +from core import auxiliary, cogs +from discord import app_commands + +if TYPE_CHECKING: + import bot + + +async def setup(bot: bot.TechSupportBot) -> None: + """Loading the dictionary plugin into the bot + + Args: + bot (bot.TechSupportBot): The bot object to register the cogs to + """ + + # Don't load without the API key + try: + if not bot.file_config.api.api_keys.dictionary: + raise AttributeError("Dictionary was not loaded due to missing API key") + except AttributeError as exc: + raise AttributeError( + "Dictionary was not loaded due to missing API key" + ) from exc + + await bot.add_cog(Dictionary(bot=bot)) + + +class Dictionary(cogs.BaseCog): + """The class for the dictionary command + + Attributes: + DICT_API_URL (str): The URL for the dict API + """ + + DICT_APT_URL: str = ( + "https://www.dictionaryapi.com/api/v3/references/collegiate/json/{}?key={}" + ) + + @app_commands.command( + name="dictionary", + description="Looks up a word in the dictionary", + extras={"module": "dictionary"}, + ) + async def dictionary_lookup( + self: Self, interaction: discord.Interaction, word: str + ) -> None: + """This calls the dictionary API and sends the definitions + + Args: + interaction (discord.Interaction): The interaction that called this command + word (str): The word to lookup in the dictionary + """ + await interaction.response.defer() + url = self.DICT_APT_URL.format( + word, self.bot.file_config.api.api_keys.dictionary + ) + response = await self.bot.http_functions.http_call("get", url) + if not response: + embed = auxiliary.prepare_deny_embed( + f"I could not find any definition for {word}" + ) + await interaction.followup.send(embed=embed) + return + + definition = response[0].shortdef + + embed = discord.Embed(title=f"Definition of {word}") + embed.color = discord.Color.orange() + embed.description = "\n".join(f"- {string}" for string in definition) + + await interaction.followup.send(embed=embed) From da0a469a5e5ad0d0fedd1035c3904bad80cfb1c2 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 5 Jun 2026 13:42:04 -0700 Subject: [PATCH 2/5] Add dictionary API key to config default --- config.default.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config.default.yml b/config.default.yml index c40a481e..215904de 100644 --- a/config.default.yml +++ b/config.default.yml @@ -36,6 +36,7 @@ api: password: api_keys: cat: + dictionary: dumpdbg: giphy: google: From ded233fe0b170fb9acfaf98ce5b696f691b101ff Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 5 Jun 2026 13:44:13 -0700 Subject: [PATCH 3/5] Formatting --- techsupport_bot/commands/dictionary.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/techsupport_bot/commands/dictionary.py b/techsupport_bot/commands/dictionary.py index b7042bde..37b6e10f 100644 --- a/techsupport_bot/commands/dictionary.py +++ b/techsupport_bot/commands/dictionary.py @@ -17,6 +17,9 @@ async def setup(bot: bot.TechSupportBot) -> None: Args: bot (bot.TechSupportBot): The bot object to register the cogs to + + Raises: + AttributeError: Raised if an API key is missing to prevent unusable commands from loading """ # Don't load without the API key @@ -38,7 +41,7 @@ class Dictionary(cogs.BaseCog): DICT_API_URL (str): The URL for the dict API """ - DICT_APT_URL: str = ( + DICT_API_URL: str = ( "https://www.dictionaryapi.com/api/v3/references/collegiate/json/{}?key={}" ) @@ -57,7 +60,7 @@ async def dictionary_lookup( word (str): The word to lookup in the dictionary """ await interaction.response.defer() - url = self.DICT_APT_URL.format( + url = self.DICT_API_URL.format( word, self.bot.file_config.api.api_keys.dictionary ) response = await self.bot.http_functions.http_call("get", url) From 92799574808fae61ad58942fce3c088aff54dbb0 Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 5 Jun 2026 13:45:00 -0700 Subject: [PATCH 4/5] Formatting 2 --- techsupport_bot/commands/dictionary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/techsupport_bot/commands/dictionary.py b/techsupport_bot/commands/dictionary.py index 37b6e10f..b37dadf8 100644 --- a/techsupport_bot/commands/dictionary.py +++ b/techsupport_bot/commands/dictionary.py @@ -17,7 +17,7 @@ async def setup(bot: bot.TechSupportBot) -> None: Args: bot (bot.TechSupportBot): The bot object to register the cogs to - + Raises: AttributeError: Raised if an API key is missing to prevent unusable commands from loading """ From f4b899b5704b16538f8204af4a18116963cf619c Mon Sep 17 00:00:00 2001 From: ajax146 <31014239+ajax146@users.noreply.github.com> Date: Fri, 5 Jun 2026 13:48:36 -0700 Subject: [PATCH 5/5] Add backticks to word in output embeds --- techsupport_bot/commands/dictionary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/techsupport_bot/commands/dictionary.py b/techsupport_bot/commands/dictionary.py index b37dadf8..69209dc2 100644 --- a/techsupport_bot/commands/dictionary.py +++ b/techsupport_bot/commands/dictionary.py @@ -66,14 +66,14 @@ async def dictionary_lookup( response = await self.bot.http_functions.http_call("get", url) if not response: embed = auxiliary.prepare_deny_embed( - f"I could not find any definition for {word}" + f"I could not find any definition for `{word}`" ) await interaction.followup.send(embed=embed) return definition = response[0].shortdef - embed = discord.Embed(title=f"Definition of {word}") + embed = discord.Embed(title=f"Definition of `{word}`") embed.color = discord.Color.orange() embed.description = "\n".join(f"- {string}" for string in definition)