From 5a4e1da4728c791ff33ad91a11857918a8862f69 Mon Sep 17 00:00:00 2001 From: DasMoorhuhn Date: Tue, 20 Feb 2024 23:34:01 +0100 Subject: [PATCH] added update_presence --- src/bot.py | 17 ++++++++--------- src/client.py | 27 +++++++++++++++++++++++---- src/cogs/help_commands.py | 3 ++- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/bot.py b/src/bot.py index 17a3f74..49e9379 100644 --- a/src/bot.py +++ b/src/bot.py @@ -10,7 +10,7 @@ from discord import Interaction from client import Client import secret_handler -bot_info = {'version': '1.1.3', 'date': '20.02.2024'} +bot_info = {'version': '1.1.4', 'date': '20.02.2024'} logger = logging.getLogger('discord') logger.setLevel(logging.DEBUG) @@ -26,35 +26,34 @@ logger.addHandler(handler) bot = Client(intents=discord.Intents.all(), command_prefix="!", log=logger, bot_info=bot_info) -async def load_cogs(): - for filename in os.listdir("./cogs"): - if filename.endswith("py") and filename != "__init__.py": - await bot.load_extension(f"cogs.{filename[:-3]}") - - @bot.event async def on_guild_join(guild: Guild): logger.info("Joined guild") + logger.info(f"Guilds: {len(bot.guilds)}") + await bot.update_presence() @bot.event async def on_guild_remove(guild: Guild): logger.info("Left guild") + logger.info(f"Guilds: {len(bot.guilds)}") + await bot.update_presence() @bot.event async def on_ready(): logger.info(f"Logged in as: {bot.user.name} with ID {bot.user.id}") - await load_cogs() + await bot.load_cogs() await asyncio.sleep(1) synced = await bot.tree.sync() logger.info(f"Synced {len(synced)} command(s)") + await bot.update_presence() @bot.event async def on_app_command_error(interaction: Interaction, error): if isinstance(error, app_commands.MissingPermissions): - await interaction.response.send_message(content="Du hast keine Adminrechte", ephemeral=True) + await interaction.response.send_message(content="You need the rights, to use this command", ephemeral=True) else: raise error diff --git a/src/client.py b/src/client.py index d1dee31..0d72656 100644 --- a/src/client.py +++ b/src/client.py @@ -1,14 +1,19 @@ +import os +import logging + from discord import Intents +from discord import Status +from discord import Game from discord.ext import commands import addons class Client(commands.Bot): """Custom bot class""" - def __init__(self, command_prefix, *, intents: Intents, log, bot_info): + def __init__(self, command_prefix, *, intents: Intents, log:logging, bot_info:dict): super().__init__(command_prefix, intents=intents) - self.log = log - self.addons = addons + 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'] @@ -16,7 +21,21 @@ class Client(commands.Bot): async def startup(self): await self.wait_until_ready() - def get_uptime(self): + async def count_guilds(self): + """Count the number of guilds, where the bot is joined""" + return len(self.guilds) + + async def update_presence(self): + """Update the presence""" + await self.change_presence(activity=Game(name=f"v{self.version} | {await self.count_guilds()}"), status=Status.online) + self.log.info("Updated presence") + + async def get_uptime(self): """Returns the uptime in seconds""" time_now = self.addons.DateTimeHelper.get_unix_time() return int(round(time_now - self.start_time, 0)) + + async def load_cogs(self): + for filename in os.listdir("./cogs"): + if filename.endswith("py") and filename != "__init__.py": + await self.load_extension(f"cogs.{filename[:-3]}") diff --git a/src/cogs/help_commands.py b/src/cogs/help_commands.py index ab8d98a..41ad997 100644 --- a/src/cogs/help_commands.py +++ b/src/cogs/help_commands.py @@ -43,8 +43,9 @@ class HelpCommands(Cog): self.log.info("Command: info") bot_string = "" - bot_string += f"Uptime : {self.bot.get_uptime()}s\n" + 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"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)"