aboutsummaryrefslogtreecommitdiffstats
path: root/bot
diff options
context:
space:
mode:
Diffstat (limited to 'bot')
-rw-r--r--bot/seasons/pride/pride_facts.py49
1 files changed, 36 insertions, 13 deletions
diff --git a/bot/seasons/pride/pride_facts.py b/bot/seasons/pride/pride_facts.py
index 95ca4d24..d2ea0461 100644
--- a/bot/seasons/pride/pride_facts.py
+++ b/bot/seasons/pride/pride_facts.py
@@ -1,3 +1,4 @@
+import asyncio
import json
import logging
import random
@@ -6,10 +7,16 @@ from pathlib import Path
from typing import Union
import dateutil.parser
+import discord
from discord.ext import commands
+from bot.constants import Channels
+from bot.constants import Colours
+
log = logging.getLogger(__name__)
+Sendable = Union[commands.Context, discord.TextChannel]
+
class PrideFacts(commands.Cog):
"""Provides a new fact every day during the Pride season!"""
@@ -24,39 +31,46 @@ class PrideFacts(commands.Cog):
with open(Path("bot/resources/pride/facts.json"), "r", encoding="utf-8") as f:
return json.load(f)
- async def random_fact(self, ctx: commands.Context) -> None:
- """Provides a fact from any valid day."""
+ async def send_pride_fact_daily(self) -> None:
+ """Background task to post the daily pride fact every day."""
+ channel = self.bot.get_channel(Channels.seasonalbot_chat)
+ while True:
+ await self.send_select_fact(channel, datetime.utcnow())
+ await asyncio.sleep(24 * 60 * 60)
+
+ async def send_random_fact(self, ctx: commands.Context) -> None:
+ """Provides a fact from any previous day, or today."""
now = datetime.utcnow()
previous_years_facts = (self.facts[x] for x in self.facts.keys() if int(x) < now.year)
current_year_facts = self.facts.get(str(now.year), [])[:now.day]
previous_facts = current_year_facts + [x for y in previous_years_facts for x in y]
try:
- await ctx.send(random.choice(previous_facts))
+ await ctx.send(embed=self.make_embed(random.choice(previous_facts)))
except IndexError:
await ctx.send("No facts available")
- async def select_fact(self, ctx: commands.Context, _date: Union[str, datetime]) -> None:
- """Provides the fact for the specified day, if valid."""
+ async def send_select_fact(self, target: Sendable, _date: Union[str, datetime]) -> None:
+ """Provides the fact for the specified day, if the day is today, or is in the past."""
now = datetime.utcnow()
if isinstance(_date, str):
try:
date = dateutil.parser.parse(_date, dayfirst=False, yearfirst=False, fuzzy=True)
except (ValueError, OverflowError) as err:
- await ctx.send(f"Error parsing date: {err}")
+ await target.send(f"Error parsing date: {err}")
return
else:
date = _date
if date.year < now.year or (date.year == now.year and date.day <= now.day):
try:
- await ctx.send(self.facts[str(date.year)][date.day - 1])
+ await target.send(embed=self.make_embed(self.facts[str(date.year)][date.day - 1]))
except KeyError:
- await ctx.send(f"The year {date.year} is not yet supported")
+ await target.send(f"The year {date.year} is not yet supported")
return
except IndexError:
- await ctx.send(f"Day {date.day} of {date.year} is not yet support")
+ await target.send(f"Day {date.day} of {date.year} is not yet support")
return
else:
- await ctx.send("The fact for the selected day is not yet available.")
+ await target.send("The fact for the selected day is not yet available.")
@commands.command(name="pridefact", aliases=["pridefacts"])
async def pridefact(self, ctx: commands.Context) -> None:
@@ -70,14 +84,23 @@ class PrideFacts(commands.Cog):
"""
message_body = ctx.message.content[len(ctx.invoked_with) + 2:]
if message_body == "":
- await self.select_fact(ctx, datetime.utcnow())
+ await self.send_select_fact(ctx, datetime.utcnow())
elif message_body.lower().startswith("rand"):
- await self.random_fact(ctx)
+ await self.send_random_fact(ctx)
else:
- await self.select_fact(ctx, message_body)
+ await self.send_select_fact(ctx, message_body)
+
+ def make_embed(self, fact) -> discord.Embed:
+ """Makes a nice embed for the fact to be sent."""
+ return discord.Embed(
+ colour=Colours.pink,
+ title="Pride Fact!",
+ description=fact
+ )
def setup(bot: commands.Bot) -> None:
"""Cog loader for pride facts."""
+ bot.loop.create_task(PrideFacts(bot).send_pride_fact_daily())
bot.add_cog(PrideFacts(bot))
log.info("Pride facts cog loaded!")