diff --git a/bot.py b/bot.py index e7c16d3..79c1676 100755 --- a/bot.py +++ b/bot.py @@ -58,6 +58,8 @@ class Bot(Bot): utils.SUCCESS_EMOJIS = utils.misc.SUCCESS_EMOJIS = ( self.config.get('response_emojis', {}).get('success', default)) + # Metrics + async def on_command(self, ctx): user_id_md5 = hashlib.md5(ctx.author.id.to_bytes(8, byteorder='big'), usedforsecurity=False).digest() await self.pool.execute( @@ -65,6 +67,19 @@ class Bot(Bot): ctx.guild.id, user_id_md5, ctx.command.qualified_name, ) + # we use on_shard_ready rather than on_ready because the latter is a bit less reliable + async def on_shard_ready(self, shard_id): + member_count = sum(guild.member_count for guild in self.guilds if guild.shard_id == shard_id) + await self.pool.execute( + """ + INSERT INTO shard_member_counts (shard_id, member_count) + VALUES ($1, $2) + ON CONFLICT (shard_id) DO UPDATE + SET member_count = EXCLUDED.member_count + """, + shard_id, member_count, + ) + def main(): import sys diff --git a/schema.sql b/schema.sql index 49599d0..69155b2 100644 --- a/schema.sql +++ b/schema.sql @@ -11,3 +11,9 @@ CREATE TABLE invokes ( ); CREATE INDEX invokes_invoked_at_idx ON invokes (invoked_at); + +CREATE TABLE shard_member_counts ( + shard_id INT2 PRIMARY KEY, + -- sum(guild.member_count for guild in shard) + member_count INT4 NOT NULL +);