From a39ecb1124718b20d2bdb0b5f5acff962b083287 Mon Sep 17 00:00:00 2001 From: Tycho Bellers Date: Wed, 18 Oct 2023 21:16:30 -0700 Subject: [PATCH] update statistics command --- .idea/dataSources.local.xml | 2 +- bot/discord_dictionary_bot/cogs/statistics.py | 6 ++--- .../discord_bot_client.py | 24 +++++++++++++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.idea/dataSources.local.xml b/.idea/dataSources.local.xml index 8375d77..152d2da 100644 --- a/.idea/dataSources.local.xml +++ b/.idea/dataSources.local.xml @@ -1,6 +1,6 @@ - + " diff --git a/bot/discord_dictionary_bot/cogs/statistics.py b/bot/discord_dictionary_bot/cogs/statistics.py index 0d9fcd4..38bed27 100644 --- a/bot/discord_dictionary_bot/cogs/statistics.py +++ b/bot/discord_dictionary_bot/cogs/statistics.py @@ -15,7 +15,7 @@ def __init__(self, bot: Bot): self._bot = bot self._bigquery_client = bigquery.Client() - @app_commands.command(name='stats') + @app_commands.command(name='stats', description='Shows some statistics about the bot.') async def stats(self, interaction: Interaction): await interaction.response.defer(ephemeral=True) reply = '**----- Statistics -----**\n' @@ -28,7 +28,7 @@ async def stats(self, interaction: Interaction): # Guild count reply += '**Guilds**\n' - reply += f'Active in **{len(self._bot.guilds)}** guilds and **{channel_count}** channels.\n\n' + reply += f'Active in **{len(self._bot.guilds):,}** guilds and **{channel_count:,}** channels.\n\n' # Total requests reply += '**Total Requests**\n' @@ -36,7 +36,7 @@ async def stats(self, interaction: Interaction): total_requests_results = total_requests_job.result() for row in total_requests_results: total_requests = row.total - reply += f'**{total_requests}** requests.\n\n' + reply += f'**{total_requests:,}** requests.\n\n' # Most common words reply += '**Most Common Words**\n' diff --git a/bot/discord_dictionary_bot/discord_bot_client.py b/bot/discord_dictionary_bot/discord_bot_client.py index 7504934..aa2a0a4 100644 --- a/bot/discord_dictionary_bot/discord_bot_client.py +++ b/bot/discord_dictionary_bot/discord_bot_client.py @@ -2,11 +2,13 @@ import logging import sys from pathlib import Path -from typing import Union, Any +from typing import Union, Any, Optional, Sequence import discord.ext.commands from discord import Message, Guild, Interaction +from discord.abc import Snowflake from discord.app_commands import ContextMenu, Command +from discord.ext.commands import Cog from discord.ext.commands.bot import Bot from google.cloud import firestore @@ -92,13 +94,27 @@ def __init__(self, dictionary_apis: [DictionaryAPI], ffmpeg_path: Union[str, Pat ]) async def setup_hook(self) -> None: + guild_ids = [] + + async def add_cog_wrapper(cog: Cog, guilds: Optional[Sequence[Snowflake]] = None): + if not guilds: + guilds = [] + guild_ids.extend(guilds) + await self.add_cog(cog, guilds=guilds) + # Add cogs - await self.add_cog(Dictionary(self, self._dictionary_apis, self._ffmpeg_path)) - await self.add_cog(Settings(self._scoped_property_manager)) - await self.add_cog(Statistics(self), guild=discord.Object(id='799455809297842177')) + await add_cog_wrapper(Dictionary(self, self._dictionary_apis, self._ffmpeg_path)) + await add_cog_wrapper(Settings(self._scoped_property_manager)) + await add_cog_wrapper(Statistics(self), guilds=[discord.Object(id='799455809297842177'), discord.Object(id='454852632528420876')]) # Sync slash commands await self.tree.sync() + for guild in guild_ids: + try: + await self.tree.sync(guild=guild) + except discord.errors.Forbidden: + # If the bot isn't in the guild, we will get a Forbidden error + logger.warning(f'Failed to sync commands for guild {guild.id}') async def on_app_command_completion(self, interaction: Interaction, command: Union[Command, ContextMenu]): if isinstance(command, Command):