-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlastseen.py
More file actions
87 lines (68 loc) · 2.73 KB
/
Copy pathlastseen.py
File metadata and controls
87 lines (68 loc) · 2.73 KB
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
from itertools import chain
from datetime import datetime
from errbot import BotPlugin, botcmd
USAGE_STR = 'Nick not specified. (usage: !last_seen <nick>)'
CONFIG_TEMPLATE = {
# locale-preffered format is default
'TIME_FORMAT': '%c',
'SEEN_MSG_MAX_CHARS': 40
}
class LastSeen(BotPlugin):
def configure(self, configuration):
if configuration is not None and configuration != {}:
config = dict(chain(CONFIG_TEMPLATE.items(),
configuration.items()))
else:
config = CONFIG_TEMPLATE
super(LastSeen, self).configure(config)
def get_configuration_template(self):
return CONFIG_TEMPLATE
def activate(self):
super(LastSeen, self).activate()
try:
if type(self['last_seen']) is not dict:
self['last_seen'] = {}
except KeyError:
self['last_seen'] = {}
def callback_message(self, msg):
user = msg.nick
last_seens = self['last_seen']
last_seens[user] = {}
last_seens[user]['time'] = datetime.now()
last_seens[user]['msg'] = msg.body
self['last_seen'] = last_seens
def generate_seen_message(self, timedelta, user, time, text):
if timedelta.days == 0:
hours = timedelta.seconds // 3600
minutes = (timedelta.seconds - (hours * 3600)) // 60
min_format = 'minute' if minutes == 1 else 'minutes'
if hours > 0:
hour_format = 'hour' if hours == 1 else 'hours'
params = [user, hours, hour_format, minutes, min_format, text]
msg = '{} was last seen {} {} and {} {} ago, saying "{}".'
return msg.format(*params)
params = [user, minutes, min_format, text]
msg = '{} was last seen {} {} ago, saying "{}".'
return msg.format(*params)
form = self.config['TIME_FORMAT']
time = time.strftime(form)
return '{} was last seen on {}, saying "{}".'.format(user, time, text)
@botcmd(split_args_with=' ')
def last_seen(self, msg, args):
"""Return last activity and its time of given user.
Examples:
!last_seen <user>
"""
last_seens = self['last_seen']
if len(args) < 1:
return USAGE_STR
user = args[0]
if user not in last_seens:
return 'We have not seen {} yet.'.format(user)
max_chars = self.config['SEEN_MSG_MAX_CHARS']
time = last_seens[user]['time']
text = last_seens[user]['msg']
timedelta = datetime.now() - time
if len(text) > max_chars:
text = text[:max_chars] + '...'
return self.generate_seen_message(timedelta, user, time, text)