Skip to content

Commit

Permalink
Did the requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
FirePlank committed Nov 7, 2023
1 parent 0712a27 commit 13f13dc
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 32 deletions.
2 changes: 2 additions & 0 deletions bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
class AoC(BaseModel):
channel_id: int
role_id: int
leaderboard_id: int
leaderboard_code: str
session_cookie: str


Expand Down
56 changes: 31 additions & 25 deletions bot/extensions/adventofcode/commands.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import logging
import re
from datetime import datetime
from typing import Optional

import aiohttp
import discord
import pytz
from bs4 import BeautifulSoup
from discord import app_commands
from discord.ext import commands
from pydantic import BaseModel, validator

from bot import core
from bot.config import settings

log = logging.getLogger(__name__)

YEAR = datetime.now(tz=pytz.timezone("EST")).year
API_URL = f"https://adventofcode.com/{YEAR}/leaderboard/private/view/975452.json"
INTERVAL = 120
AOC_REQUEST_HEADER = {"user-agent": "TWT AoC Event Bot"}

LEADERBOARD_ID = settings.aoc.leaderboard_id
LEADERBOARD_CODE = settings.aoc.leaderboard_code
API_URL = f"https://adventofcode.com/{YEAR}/leaderboard/private/view/{LEADERBOARD_ID}.json"
AOC_REQUEST_HEADER = {"user-agent": "Tech With Tim Discord Bot https://github.com/SylteA/Discord-Bot"}
AOC_SESSION_COOKIE = {"session": settings.aoc.session_cookie}


Expand All @@ -29,15 +30,20 @@ def ordinal(n: int):
return str(n) + suffix


class Member:
def __init__(self, results):
self.global_score = results["global_score"]
self.name = results["name"]
self.stars = results["stars"]
self.last_star_ts = results["last_star_ts"]
self.completion_day_level = results["completion_day_level"]
self.id = results["id"]
self.local_score = results["local_score"]
class Member(BaseModel):
global_score: int
name: str
stars: int
last_star_ts: int
completion_day_level: dict
id: int
local_score: int

@validator("name", pre=True)
def set_name_to_anonymous(cls, val: Optional[str]) -> str:
if val is None:
return "Anonymous User"
return val


class AdventOfCode(commands.GroupCog, group_name="aoc"):
Expand All @@ -49,7 +55,7 @@ def role(self) -> discord.Role:
return self.bot.get_role(settings.aoc.role_id)

@app_commands.command()
async def subscribe(self, interaction: core.InteractionType) -> None:
async def subscribe(self, interaction: core.InteractionType):
"""Subscribe to receive notifications for new puzzles"""

if self.role not in interaction.user.roles:
Expand All @@ -67,7 +73,7 @@ async def subscribe(self, interaction: core.InteractionType) -> None:
)

@app_commands.command()
async def unsubscribe(self, interaction: core.InteractionType) -> None:
async def unsubscribe(self, interaction: core.InteractionType):
"""Unsubscribe from receiving notifications for new puzzles"""

if self.role in interaction.user.roles:
Expand All @@ -83,7 +89,7 @@ async def unsubscribe(self, interaction: core.InteractionType) -> None:
)

@app_commands.command()
async def countdown(self, interaction: core.InteractionType) -> None:
async def countdown(self, interaction: core.InteractionType):
"""Get the time left until the next puzzle is released"""

if (
Expand All @@ -105,17 +111,17 @@ async def countdown(self, interaction: core.InteractionType) -> None:
await interaction.response.send_message("Advent of Code is not currently running.", ephemeral=True)

@app_commands.command(name="join")
async def join_leaderboard(self, interaction: core.InteractionType) -> None:
async def join_leaderboard(self, interaction: core.InteractionType):
"""Learn how to join the leaderboard"""

await interaction.response.send_message(
"Head over to https://adventofcode.com/leaderboard/private"
"with the code `975452-d90a48b0` to join the TWT private leaderboard!",
f"with the code `{LEADERBOARD_CODE}` to join the TWT private leaderboard!",
ephemeral=True,
)

@app_commands.command()
async def leaderboard(self, interaction: core.InteractionType) -> None:
async def leaderboard(self, interaction: core.InteractionType):
"""Get a snapshot of the TWT private AoC leaderboard"""

if interaction.channel_id != settings.aoc.channel_id:
Expand All @@ -130,12 +136,12 @@ async def leaderboard(self, interaction: core.InteractionType) -> None:
else:
resp.raise_for_status()

members = [Member(leaderboard["members"][id]) for id in leaderboard["members"]]
members = [Member(**member_data) for member_data in leaderboard["members"].values()]

embed = discord.Embed(
title=f"{interaction.guild.name} Advent of Code Leaderboard",
colour=discord.Colour(0x68C290),
url=f"https://adventofcode.com/{YEAR}/leaderboard/private/view/975452",
colour=0x68C290,
url=f"https://adventofcode.com/{YEAR}/leaderboard/private/view/{LEADERBOARD_ID}",
)

leaderboard = {
Expand Down Expand Up @@ -205,7 +211,7 @@ async def global_leaderboard(self, interaction: core.InteractionType, number_of_

embed = discord.Embed(
title="Advent of Code Global Leaderboard",
colour=discord.Colour(0x68C290),
colour=0x68C290,
url="https://adventofcode.com",
description=s_desc,
)
Expand Down
33 changes: 31 additions & 2 deletions bot/extensions/adventofcode/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import datetime as dt
import logging
import traceback
from datetime import datetime

import aiohttp
Expand All @@ -9,6 +11,9 @@

from bot import core
from bot.config import settings
from bot.services import http, paste

log = logging.getLogger(__name__)

aoc_time = dt.time(hour=0, minute=0, second=1, tzinfo=pytz.timezone("EST"))
YEAR = datetime.now(tz=pytz.timezone("EST")).year
Expand All @@ -21,15 +26,15 @@ def __init__(self, bot: core.DiscordBot):
self.bot = bot
self.daily_puzzle.start()

def cog_unload(self) -> None:
def cog_unload(self):
self.daily_puzzle.cancel()

@property
def channel(self) -> discord.TextChannel:
return self.bot.get_channel(settings.aoc.channel_id)

@tasks.loop(time=aoc_time)
async def daily_puzzle(self) -> None:
async def daily_puzzle(self):
"""Post the daily Advent of Code puzzle"""

day = datetime.now(tz=pytz.timezone("EST")).day
Expand All @@ -51,3 +56,27 @@ async def daily_puzzle(self) -> None:
f"View it online now at {puzzle_url}. Good luck!",
allowed_mentions=discord.AllowedMentions(roles=True),
)

@daily_puzzle.error
async def daily_puzzle_error(self, error: Exception):
"""Log any errors raised by the daily puzzle task"""

content = "\n".join(traceback.format_exception(type(error), error, error.__traceback__))
header = "Ignored exception in daily puzzle task"

def wrap(code: str) -> str:
code = code.replace("`", "\u200b`")
return f"```py\n{code}\n```"

if len(content) > 1024:
document = await paste.create(content)
content = wrap(content[:1024]) + f"\n\n [Full traceback]({document.url})"
else:
content = wrap(content)

embed = discord.Embed(
title=header, description=content, color=discord.Color.red(), timestamp=discord.utils.utcnow()
)
await discord.Webhook.from_url(url=settings.errors.webhook_url, session=http.session).send(embed=embed)

log.error("Daily puzzle task failed", exc_info=error)
8 changes: 4 additions & 4 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ async def main(ctx):
prefixes = ("t.",)
extensions = (
"jishaku",
"bot.extensions.adventofcode",
"bot.extensions.challenges",
"bot.extensions.readthedocs",
"bot.extensions.suggestions",
"bot.extensions.github",
"bot.extensions.tags",
"bot.extensions.levelling",
"bot.extensions.persistent_roles",
"bot.extensions.polls",
"bot.extensions.adventofcode",
"bot.extensions.readthedocs",
"bot.extensions.suggestions",
"bot.extensions.tags",
"bot.cogs._help",
"bot.cogs.clashofcode",
"bot.cogs.roles",
Expand Down
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# --- AoC
AOC__CHANNEL_ID=0
AOC__ROLE_ID=0
AOC__LEADERBOARD_ID = 975452
AOC__LEADERBOARD_CODE = 975452-d90a48b0
AOC__SESSION_COOKIE=...

# --- Bots
Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jishaku = {extras = ["procinfo", "profiling"], version = "^2.5.1"}
beautifulsoup4 = "^4.12.2"
tabulate = "^0.9.0"
pillow = "^10.1.0"
pytz = "^2023.3.post1"

[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"
Expand Down

0 comments on commit 13f13dc

Please sign in to comment.