-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (99 loc) · 3.54 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import asyncio
import time
import datetime
import discord
client = discord.Client()
id_ctr = 1
duration = {'ice':60*60*4, 'eno':60*60*4, 'col':60*60*5}
name = {'ice':'Ice', 'eno':'Eno', 'col':'Col'}
timer_list = []
help_message = \
"""
!ice depleted system
!eno depleted system
!col depleted system
!list
!status {ice, eno, col}
!status {ice, eno, col} system
!delete id
"""
@client.event
async def on_ready():
print('Discord bot online as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!'):
com = message.content[1:].split(' ')[0]
if com in ['ice', 'eno', 'col']:
task, dep, px = message.content[1:].split(' ')
if dep == 'depleted' and task in name.keys():
timer_list.append(create_timer(message.channel, task, px))
if com == 'list':
com = message.content[1:].split(' ')
stat = get_status_string(timer_list)
await message.channel.send(stat)
if com == 'status':
if len(message.content[1:].split(' ')) == 2:
com, task = message.content[1:].split(' ')
stat = get_status(task)
await message.channel.send(stat)
if len(message.content[1:].split(' ')) == 3:
com, task, px = message.content[1:].split(' ')
stat = get_status_reg(task, px)
await message.channel.send(stat)
if com == 'delete':
com, id = message.content[1:].split(' ')
delete_id(id)
stat = get_status_string(timer_list)
await message.channel.send(stat)
if com == 'help':
await message.channel.send(help_message)
def get_status_reg(task, px):
timer_sublist = []
for item in timer_list:
if item['class'] == task and item['px'] == px:
timer_sublist.append(item)
return get_status_string(timer_sublist)
def get_status(task):
timer_sublist = []
for item in timer_list:
if item['class'] == task:
timer_sublist.append(item)
return get_status_string(timer_sublist)
def get_status_string(timer_sublist):
lines = []
for i,item in enumerate(timer_sublist, 1):
line = str(item['id']) + '- ' \
+ item['class'] + ' ' \
+ item['px'] + '- '\
+ format_time(item['finish'] - time.time())
lines.append(line)
if not(len(lines)):
return 'No active countdowns'
else:
return 'Active timers: \n' + '\n'.join(lines)
def format_time(sec):
return str(datetime.timedelta(seconds=int(sec)))
async def start_timer(channel, task, px):
await channel.send(name[task] + ' marked as depleted for ' + str(px))
await asyncio.sleep(duration[task])
update_timer()
await channel.send('@here ' + name[task] + ' for ' + str(px) + ' has respawned')
def delete_id(id):
global timer_list
timer_list = [t for t in timer_list if (t['id'] != int(id))]
def update_timer():
global timer_list
timer_list = [t for t in timer_list if (t['finish'] - time.time())>0]
def create_timer(channel, task, px):
global id_ctr
timer = {'task': asyncio.ensure_future(start_timer(channel, task, px)),
'class': task,
'px': px,
'finish': time.time() + duration[task],
'id': id_ctr}
id_ctr = (id_ctr + 1) % 1000
return timer
client.run('Token goes here ')