-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (76 loc) · 2.59 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
from dabc import get_allocated_products, get_limited_products, generate_ascii_tables, products_to_embeds, generate_drawing_embeds, products_to_embeds_tables
from discord import generate_random_color, send_discord_message
import schedule
import argparse
import os
from sys import argv
from time import sleep
from logs import my_logger
logger = my_logger(__name__)
# Environment variables required for the script
REQUIRED_ENV_VARS = (
"ALLOCATED_HOOK",
"LIMITED_HOOK",
"DRAWINGS_HOOK",
)
# Check for required environment variables
for var in REQUIRED_ENV_VARS:
if not os.getenv(var):
raise EnvironmentError(f"Missing environment variable: {var}")
def post_allocated_items():
"""
Post allocated items to Discord with random color embeds.
"""
color = generate_random_color()
for batch in get_allocated_products():
embeds = products_to_embeds(batch, color)
send_discord_message('Allocated', embeds)
def post_limited_items():
"""
Post limited items to Discord with random color embeds.
"""
color = generate_random_color()
tables = generate_ascii_tables(get_limited_products())
for table in tables:
embeds = products_to_embeds_tables(table, color)
send_discord_message('Limited', embeds)
def post_drawings():
"""
Post about drawings if any are available.
"""
drawings = generate_drawing_embeds()
if drawings:
send_discord_message('Drawings', drawings)
else:
logger.info("No Drawings today")
def parse_arguments(args):
"""
Parse command line arguments.
:param args: List of command line arguments
:return: Parsed arguments
"""
parser = argparse.ArgumentParser(description='Bot for posting DABC updates to Discord')
parser.add_argument('--now', action='store_true', help='Run the bot immediately')
return parser.parse_args(args)
def main(args):
"""
Main function to control the bot's execution.
:param args: Command line arguments
"""
parsed_args = parse_arguments(args)
logger.info("Starting Bot...")
if parsed_args.now:
post_allocated_items()
post_limited_items()
post_drawings()
else:
# Get run time from environment variable or use default
run_time = os.getenv('BOOZE_TIME', '11:00')
schedule.every().day.at(run_time).do(post_allocated_items)
schedule.every().day.at(run_time).do(post_limited_items)
schedule.every().day.at(run_time).do(post_drawings)
while True:
schedule.run_pending()
sleep(1)
if __name__ == "__main__":
main(argv[1:])