Skip to content

Commit

Permalink
update statistics command
Browse files Browse the repository at this point in the history
  • Loading branch information
TychoTheTaco committed Oct 19, 2023
1 parent 6f96633 commit a39ecb1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .idea/dataSources.local.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bot/discord_dictionary_bot/cogs/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -28,15 +28,15 @@ 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'
total_requests_job = self._bigquery_client.query('SELECT COUNT(*) AS total FROM analytics.definition_requests')
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'
Expand Down
24 changes: 20 additions & 4 deletions bot/discord_dictionary_bot/discord_bot_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a39ecb1

Please sign in to comment.