add command invocation metrics

This commit is contained in:
io mintz 2021-04-06 03:51:54 +00:00
parent 6b0dc50689
commit f9d272fee2
5 changed files with 29 additions and 2 deletions

10
bot.py
View File

@ -16,6 +16,7 @@
# along with Emote Manager. If not, see <https://www.gnu.org/licenses/>.
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

View File

@ -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': {

View File

@ -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.

View File

@ -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

13
schema.sql Normal file
View File

@ -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);