diff --git a/bot.py b/bot.py index c026b09..e7c16d3 100755 --- a/bot.py +++ b/bot.py @@ -16,6 +16,7 @@ # along with Emote Manager. If not, see . import base64 +import hashlib import logging import traceback @@ -42,7 +43,7 @@ class Bot(Bot): with open('data/config.py', encoding='utf-8') as f: config = eval(f.read(), {}) - super().__init__(config=config, **kwargs) + super().__init__(config=config, setup_db=True, **kwargs) # allow use of the bot's user ID before ready() token_part0 = self.config['tokens']['discord'].partition('.')[0].encode() self.user_id = int(base64.b64decode(token_part0 + b'=' * (3 - len(token_part0) % 3))) @@ -57,6 +58,13 @@ class Bot(Bot): utils.SUCCESS_EMOJIS = utils.misc.SUCCESS_EMOJIS = ( self.config.get('response_emojis', {}).get('success', default)) + 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( + 'INSERT INTO invokes (guild_id, user_id_md5, command) VALUES ($1, $2, $3)', + ctx.guild.id, user_id_md5, ctx.command.qualified_name, + ) + def main(): import sys diff --git a/data/config.example.py b/data/config.example.py index 8e015af..d0c0727 100644 --- a/data/config.example.py +++ b/data/config.example.py @@ -13,6 +13,11 @@ 'discord': 'sek.rit.token', }, + # required for metrics + # configure it according to https://magicstack.github.io/asyncpg/current/api/index.html#connection + 'database': { + }, + 'ignore_bots': { 'default': True, 'overrides': { diff --git a/data/short-license.txt b/data/short-license.txt index 44436dd..b7c5bcb 100644 --- a/data/short-license.txt +++ b/data/short-license.txt @@ -10,5 +10,5 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. -You may find a copy of the GNU Affero General Public License at https://github.com/EmoteBot/EmoteManager/blob/master/LICENSE.md. +You may find a copy of the GNU Affero General Public License at https://github.com/EmoteBot/EmoteManager/blob/metrics/LICENSE.md. The rest of the source code is also there. diff --git a/requirements.txt b/requirements.txt index ca1d9be..f9b15f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ bot_bin>=1.5.0,<2.0.0 discord.py>=1.5.0,<2.0.0 jishaku wand +asyncpg<1.0.0 diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..49599d0 --- /dev/null +++ b/schema.sql @@ -0,0 +1,13 @@ +SET TIME ZONE 'UTC'; + +-- inserted for every time a command is invoked +CREATE TABLE invokes ( + guild_id BIGINT NOT NULL, + -- hashed for privacy + user_id_md5 BYTEA NOT NULL, + -- the qualified name of the command invoked (https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=qualified_name#discord.ext.commands.Command.qualified_name) + command TEXT NOT NULL, + invoked_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX invokes_invoked_at_idx ON invokes (invoked_at);