-
Notifications
You must be signed in to change notification settings - Fork 0
/
offliner.py
90 lines (69 loc) · 2.58 KB
/
offliner.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env pyton3
from disco.bot import Plugin
from disco.types import UNSET
from disco.types.user import Status
from gevent import sleep, kill, GreenletExit
from datetime import datetime, timedelta
class Offliner(Plugin):
"""
Plugin to count the days a person has been offline, along with
statistics for comparison.
"""
def load(self, ctx):
super().load(ctx)
self.maximum = self.storage.guild('offliner_maximum')
self.history = self.storage.guild('offliner_history')
self.status = self.storage.guild('offliner_status')
def unload(self, ctx):
super().unload(ctx)
@Plugin.listen('PresenceUpdate')
def member_presence_update(self, event):
userid = str(event.user.id)
time = datetime.now()
timestamp = time.hour * 3600 + time.minute * 60 + time.second
timestamp //= 10
timestamp %= 10
if event.status == Status.OFFLINE:
try:
status = self.status[timestamp]
except KeyError:
status = {}
status[userid] = 0
print(timestamp)
self.status[timestamp] = status
else:
try:
status = self.status[timestamp]
del status[userid]
self.status[timestamp] = status
except KeyError:
pass
@Plugin.schedule(10, repeat=True, init=False)
def offliner_schedule(self):
time = datetime.now()
timestamp = time.hour * 3600 + time.minute * 60 + time.second
timestamp //= 10
timestamp %= 10
print(timestamp)
for guild in self.state.guilds.values():
self.offliner_guild_check(guild, timestamp)
def offliner_guild_check(self, guild, stamp):
self.ctx['guild'] = guild
try:
status = self.status[stamp]
newstatus = {}
for uid, val in status.items():
if uid in self.state.users:
user = self.state.users[uid]
if user.presence == UNSET or user.presence == Status.OFFLINE:
newstatus[uid] = status[uid] + 1
self.send_off_line(guild, user, newstatus[uid])
self.status[stamp] = newstatus
except KeyError:
pass
finally:
self.ctx.drop()
def send_off_line(self, guild, user, lines):
channel = next(iter(guild.channels.values()))
points = '~~||||~~ ' * (lines // 5) + '|' * (lines % 5)
channel.send_message('<@{}>: {}'.format(user.id, points))