-
Notifications
You must be signed in to change notification settings - Fork 0
/
talk.py
98 lines (94 loc) · 3.34 KB
/
talk.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
91
92
93
94
95
96
97
98
import requests
import memory
import datetime
def error():
return '''
**Options:**
$test - Test bot
$event {eventID} - Track a new event
$ls {(optional) eventID} - List event/events
$rm {eventID} - Stop tracking an event
'''
def str_to_time(str):
return datetime.datetime.fromisoformat(str).replace(second=0, microsecond=0)
def notify():
keys = memory.getEvent()
if not keys:
return
for key in keys:
title = memory.getEvent(key)[0]
url = memory.getEvent(key)[4]
now = datetime.datetime.utcnow().replace(second=0, microsecond=0)
start = str_to_time(memory.getEvent(key)[1]).replace(tzinfo=None) - now
finish = now - str_to_time(memory.getEvent(key)[2]).replace(tzinfo=None)
# if start = 1 day exactly
if datetime.timedelta(1) == start:
return f'@everyone **{title}** starts in 24 hours! {url}'
# if start = 2 h exactly
elif datetime.timedelta(0,0,0,0,0,2) == start:
return f'@everyone **{title}** starts in 2 hours! {url}'
# if start = 1 h exactly
elif datetime.timedelta(0,0,0,0,0,1) == start:
return f'@everyone **{title}** starts in 1 hour! {url}'
# if start = 1min STARTING NOW
elif datetime.timedelta(0,0,0,0,1,0) == start:
return f'@everyone **{title}** has started! {url}'
# if finish = -1 day exactly
elif datetime.timedelta(-1) == finish:
return f'@everyone **{title}** ends in 24 hours! {url}'
# if finish = -12h
elif datetime.timedelta(0,0,0,0,0,-12) == finish:
return f'@everyone **{title}** ends in 12 hours! {url}'
# if finish = -1h
elif datetime.timedelta(0,0,0,0,0,-1) == finish:
return f'@everyone **{title}** ends in 1 hour! {url}'
# if finish = -1min ENDING NOW and delete
elif datetime.timedelta(0,0,0,0,-1,0) <= finish:
memory.deleteEvent(key)
return f'@everyone **{title}** has ended! {url}'
return
def answer(*args):
try:
if args[0] == 'test':
return 'I am not a bot, I am your mother.'
elif args[0] == 'event':
headers = {'user-agent':'bot'}
r = requests.get(f'https://ctftime.org/api/v1/events/{int(args[1])}/', headers=headers)
if r.status_code == 200:
r = r.json()
memory.addEvent(args[1], r['title'], r['start'], r['finish'], r['duration'], r['url'], r['ctftime_url'], r['format'], r['description'][:200]+'...')
else:
return 'Could not find that CTF Time event.'
return f'Added new event: {r["title"]}!'
elif args[0] == 'rm':
memory.deleteEvent(args[1])
return f'Event {args[1]} deleted.'
elif args[0] == 'ls':
if len(args) > 1:
res = memory.getEvent(args[1])
if not res:
return f'Could not find an entry for {args[1]}'
return f'''
**{res[0]}**
*Start* - {str(str_to_time(res[1]))[:-9]} UTC
*End* - {str(str_to_time(res[2]))[:-9]} UTC
*Duration* - {str(str_to_time(res[2]) - str_to_time(res[1]))[:-9]}
*URL* - {res[4]}
*CTF Time* - {res[5]}
*Format* - {res[6]}
*Description* - {res[7]}
'''
else:
keys = memory.getEvent()
if not keys:
return 'No events planned'
res = ''
for key in keys:
event = memory.getEvent(key)
res += key+' - '+event[0]+' - '+event[5]+'\n'
return res
else:
return error()
except Exception as e:
print(f'User input: {args}, error: {e}')
return error()