From 39f1a777f93844350de7f411b07f0f33cb64ff56 Mon Sep 17 00:00:00 2001 From: DasMoorhuhn Date: Tue, 8 Jul 2025 23:47:51 +0200 Subject: [PATCH] Add http cat command --- src/addons/__init__.py | 1 + src/addons/http_cat.py | 9 +++++++++ src/bot.py | 7 +++++-- src/client.py | 5 ++--- src/cogs/help_commands.py | 9 +++++---- src/cogs/http_cat_commands.py | 36 +++++++++++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/addons/http_cat.py create mode 100644 src/cogs/http_cat_commands.py diff --git a/src/addons/__init__.py b/src/addons/__init__.py index ddac5cd..4a0d229 100644 --- a/src/addons/__init__.py +++ b/src/addons/__init__.py @@ -4,3 +4,4 @@ sys.path.append('..') from src.addons.date_time_helper import DateTimeHelper from src.addons.programmer_excuses import ProgrammerExcuses from src.addons.xkcd import XKCD +from src.addons.http_cat import HttpCat diff --git a/src/addons/http_cat.py b/src/addons/http_cat.py new file mode 100644 index 0000000..2d71d33 --- /dev/null +++ b/src/addons/http_cat.py @@ -0,0 +1,9 @@ +import requests + + +class HttpCat: + def __init__(self) -> None: + self.url = "https://http.cat/images" + + def get_http_cat_code(self, http_code:int): + return f"{self.url}/{http_code}.jpg" diff --git a/src/bot.py b/src/bot.py index 49e9379..64b70e2 100644 --- a/src/bot.py +++ b/src/bot.py @@ -10,7 +10,6 @@ from discord import Interaction from client import Client import secret_handler -bot_info = {'version': '1.1.4', 'date': '20.02.2024'} logger = logging.getLogger('discord') logger.setLevel(logging.DEBUG) @@ -23,7 +22,7 @@ formatter = logging.Formatter('%(asctime)s|%(levelname)s|%(name)s|:%(message)s') handler.setFormatter(formatter) logger.addHandler(handler) -bot = Client(intents=discord.Intents.all(), command_prefix="!", log=logger, bot_info=bot_info) +bot = Client(intents=discord.Intents.all(), command_prefix="!", log=logger) @bot.event @@ -49,6 +48,10 @@ async def on_ready(): logger.info(f"Synced {len(synced)} command(s)") await bot.update_presence() + # for guild in bot.guilds: + # if guild.system_channel is not None: + # await guild.system_channel.send("NEW COMMAND: /http") + @bot.event async def on_app_command_error(interaction: Interaction, error): diff --git a/src/client.py b/src/client.py index 0d72656..fa95772 100644 --- a/src/client.py +++ b/src/client.py @@ -10,13 +10,12 @@ import addons class Client(commands.Bot): """Custom bot class""" - def __init__(self, command_prefix, *, intents: Intents, log:logging, bot_info:dict): + def __init__(self, command_prefix, *, intents: Intents, log:logging): super().__init__(command_prefix, intents=intents) self.log:logging = log self.addons:addons = addons self.start_time:float = self.addons.DateTimeHelper.get_unix_time() - self.version:str = bot_info['version'] - self.date:str = bot_info['date'] + self.version:str = "2025-07-08" async def startup(self): await self.wait_until_ready() diff --git a/src/cogs/help_commands.py b/src/cogs/help_commands.py index 41ad997..a62ceb2 100644 --- a/src/cogs/help_commands.py +++ b/src/cogs/help_commands.py @@ -32,7 +32,8 @@ class HelpCommands(Cog): command_list_string += "`/help` : Get this view\n" command_list_string += "`/get-random-comic`: Get a randowm XCCD comic\n" command_list_string += "`/get-latest-comic`: Get latest comic from XKCD\n" - command_list_string += "`/excuse` : Get a random excuse from programmingexcuses" + command_list_string += "`/excuse` : Get a random excuse from programmingexcuses\n" + command_list_string += "`/http` : Get a cat picture for your HTTP Code. If the Code doesn't exists, it will give you HTTP Cat 404" embed = Embed(title=f"Help", description="List of commands", color=Color.blue()) embed.add_field(name="Commands", value=command_list_string, inline=True) @@ -44,11 +45,11 @@ class HelpCommands(Cog): bot_string = "" bot_string += f"Uptime : {await self.bot.get_uptime()}s\n" - bot_string += f"Version : {self.bot.version} from {self.bot.date}\n" + bot_string += f"Version : {self.bot.version}\n" bot_string += f"On Servers: {await self.bot.count_guilds()}\n" bot_string += f"Developer : dasmoorhuhn\n" - bot_string += f"Sourcecode: [Gitlab](https://gitlab.com/DasMoorhuhn/tux-discord-bot)\n" - bot_string += f"Privacy : [Read about privacy](https://gitlab.com/DasMoorhuhn/tux-discord-bot/-/blob/main/README.md?ref_type=heads#privacy)" + bot_string += f"Sourcecode: [Gitlab](https://git.pinguin-software.de/DasMoorhuhn/tux-discord-bot)\n" + bot_string += f"Privacy : [Read about privacy](https://git.pinguin-software.de/DasMoorhuhn/tux-discord-bot#privacy)" embed = Embed(title=f"Info", description="about this Bot", color=Color.blue()) diff --git a/src/cogs/http_cat_commands.py b/src/cogs/http_cat_commands.py new file mode 100644 index 0000000..af163ea --- /dev/null +++ b/src/cogs/http_cat_commands.py @@ -0,0 +1,36 @@ +import discord +from discord import Embed +from discord import Color +from discord import app_commands +from discord import Interaction +from discord.ext import commands +from discord.ext.commands import Cog + +import sys +sys.path.append('..') + +from src.client import Client as Bot + + +class HttpCatCommands(Cog): + def __init__(self, bot:Bot): + self.bot = bot + self.log = bot.log + self.http_cat = bot.addons.HttpCat() + + @commands.Cog.listener() + async def on_ready(self): + self.log.info("Done: HttpCat") + + @app_commands.command(name="http", description="Get a cat picture for your HTTP Code. If the Code doesn't exists, it will give you HTTP Cat 404.") + async def http(self, interaction: Interaction, http_code:int): + self.log.info("Command: HTTP") + cat = self.http_cat.get_http_cat_code(http_code=http_code) + embed = Embed(title=f"HTTP Code {http_code}", color=Color.blue(), url=cat) + embed.set_image(url=cat) + await interaction.response.send_message(embed=embed) + + +async def setup(bot:Bot): + await bot.add_cog(HttpCatCommands(bot)) + bot.log.info(f"Loaded HttpCat")