-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
62 lines (50 loc) · 1.45 KB
/
bot.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
from discord.ext import commands
from whatsnext import Whatsnext
from ruamel.yaml import YAML
from datetime import datetime, timedelta
import math
bot = commands.Bot(command_prefix='$')
yaml = YAML()
with open('mvla2020-21.yaml') as f:
schedule_base = yaml.load(f)
inst = Whatsnext(schedule_base)
def view(now):
period = inst.next(now)
last_class = inst.prev(now)
start = period['start']
end = period['end']
def till(a, b):
return b - a
length = till(start, end)
# only make sense before class
between = till(last_class['end'], start)
until = till(now, start)
# only make sense during class
left = till(now, end)
elapsed = till(start, now)
try:
percent = until / between
except ZeroDivisionError:
percent = 0
diff = until
if start < now < end:
percent = elapsed / length
diff = left
return {
"last": last_class,
"next": period,
"countdown": timedelta(seconds=math.ceil(diff.total_seconds())),
"percent": percent,
"percent_formatted": f"{percent*100:0.2f}%",
}
@bot.command()
async def hello(ctx):
await ctx.send("Hello!");
@bot.command()
async def period(ctx):
period = view(datetime.now())
await ctx.send("{next[id]} {countdown} {percent_formatted}".format(**period));
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run('PUT BOT KEY HERE')