-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
68 lines (53 loc) · 2.02 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
63
64
65
66
67
68
#!/usr/bin/env python3
import random
import time
from datetime import datetime, timedelta
import requests
from callout import CallOut
from datetimerangecollection import DateTimeRangeCollection
from exercisecollection import ExerciseCollection
from settings import Settings
from user import UserCollection
class Bot:
def __init__(self):
self.settings = Settings()
self.users = UserCollection(self.settings)
self.exercises = ExerciseCollection(self.settings)
self.date_time_ranges = DateTimeRangeCollection(self.settings)
def run(self):
self.call_out_if_in_range()
while True:
self.wait()
self.reload()
self.call_out()
def reload(self):
self.settings.reload()
self.users.reload()
def wait(self):
wait_seconds = int(self.date_time_ranges.draw_seconds())
today = datetime.today()
next_draw = today + timedelta(seconds=wait_seconds)
message = "NEXT LOTTERY IS"
if next_draw.day != today.day:
message += " ON " + next_draw.strftime("%A, %b %d")
message += " AT " + next_draw.strftime("%H:%M")
print(message)
if not self.settings.is_debug:
requests.post(self.settings.post_url, data=message)
time.sleep(wait_seconds)
else:
time.sleep(5)
def call_out_if_in_range(self):
today = datetime.today()
start, stop = self.date_time_ranges.next_range_today()
if start <= today <= stop:
self.call_out()
def call_out(self):
exercise = self.exercises.draw()
reps = random.randrange(exercise["minReps"], exercise["maxReps"] + 1)
# TODO: Don't convert to handle here; but need a special "ChannelUser"
if random.random() < self.settings.group_callout_chance:
winner_handles = ["@channel"]
else:
winner_handles = [user.handle for user in self.users.draw()]
CallOut(self.settings, exercise, reps, winner_handles).publish()