forked from cpssd-students/steely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
38 lines (27 loc) · 952 Bytes
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
'''
show plugin stats
'''
from tinydb import TinyDB, Query
from operator import itemgetter
__author__ = 'sentriz'
COMMAND = '.stats'
CMD_DB = TinyDB('databases/stats.json')
CMD = Query()
LIMIT = 10
def parse_stats(stats):
for stat in CMD_DB.all():
yield stat['command'], stat['count']
def sort_stats(stats):
return sorted(stats, key=itemgetter(1), reverse=True)
def main(bot, author_id, message, thread_id, thread_type, **kwargs):
clean_stats = list(parse_stats(CMD_DB))
sorted_stats = sort_stats(clean_stats)[:LIMIT]
max_command = max(len(command) for command, count in sorted_stats)
message = f'```\ntop {LIMIT}\n――――――\n'
for command, count in sorted_stats:
if count == 100:
count = '💯'
message += f'{command:<{max_command}} {count:>3,}\n'
message += '```'
bot.sendMessage(message, thread_id=thread_id, thread_type=thread_type)