-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetbot.py
69 lines (61 loc) · 3.25 KB
/
leetbot.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
import discord
from datetime import datetime
from discord.ext import commands, tasks
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
import dlcp as dlcp
import pytz # for timezone conversions
import traceback
import cowsay
#TODO: Fix Cowsay to look nice in discord chat box
#TODO: Retrieve daily question for all bot instances at once.
DEBUG = False
class DailyLeetClient(commands.Bot):
EST = pytz.timezone("US/Eastern")
UTC = pytz.utc
XMAS_EMOJIS1 = " :christmas_tree: :santa: :snowman2: "
XMAS_EMOJIS2 = " :snowman2: :santa: :christmas_tree: "
WELCOME = "Hello. I will post the Leetcode question every time the new question drops at 4:00PM PST (12:00AM UTC)"
DLCP_NULL_ERROR = "I don't feel so good. 🤢 \n Please check logs/dlcp.log "
def __init__(self, *args, target_channel_name=None, cowsay_enabled=False, **kwargs):
super().__init__(*args, **kwargs)
self.logger = open('logs/leetbot.log', 'w')
self.target_channel_name = target_channel_name
self.cowsay_enabled = cowsay_enabled
def prepare_message(self, message):
if self.cowsay_enabled:
message = cowsay.get_output_string('cow',message)
return message
async def on_ready(self):
print(f'leetbot.py: Logged on as {self.user}!')
scheduler = AsyncIOScheduler()
scheduler.add_job(self.post_daily_question, CronTrigger(hour="16", minute="0", second="0")) # 4pm PST = 12am UTC
if DEBUG:
scheduler.add_job(self.post_daily_question, CronTrigger(second="0"))
scheduler.add_job(self.post_advent_of_code, CronTrigger(hour="5", minute="0", second="0", month="12")) # 9pm PST = 12am EST = 5AM UTC
scheduler.start()
for server in self.guilds:
for channel in server.channels:
if isinstance(channel, discord.TextChannel) and channel.name == self.target_channel_name:
await channel.send(self.prepare_message(self.WELCOME))
async def post_daily_question(self):
for server in self.guilds:
for channel in server.channels:
if isinstance(channel, discord.TextChannel) and channel.name == self.target_channel_name:
challenge = dlcp.get_daily_challenge()
if challenge is not None:
await channel.send(self.prepare_message("Problem of the day " + challenge))
else:
await channel.send(self.prepare_message(self.DLCP_NULL_ERROR))
async def post_advent_of_code(self):
for server in self.guilds:
for channel in server.channels:
if isinstance(channel, discord.TextChannel) and channel.name == self.target_channel_name:
aoc = dlcp.get_advent_of_code()
if aoc is not None:
await channel.send(self.prepare_message(self.XMAS_EMOJIS1 + "Ho ho ho! Advent of code challenge of the day: " + aoc + self.XMAS_EMOJIS2))
else:
await channel.send(self.prepare_message(self.DLCP_NULL_ERROR))
async def on_error(self, event_method, *args, **kwargs):
self.logger.write(traceback.format_exc())
self.logger.flush()