aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/pride/pride_anthem.py
diff options
context:
space:
mode:
authorGravatar Eivind Teig <[email protected]>2020-04-02 14:39:24 +0200
committerGravatar GitHub <[email protected]>2020-04-02 14:39:24 +0200
commitd77a2bbc50305d05371197f4cfe3354cfca4c627 (patch)
treebe1eed54972d9843f66114311f93b68b579046ac /bot/exts/pride/pride_anthem.py
parentMerge pull request #382 from ks129/game-fuzzy (diff)
parentMerge master: adjust `Space` cog location (diff)
Merge pull request #329 from python-discord/seasonal-purge
Deseasonify: Make all cogs available year-round, and manage only branding by season.
Diffstat (limited to 'bot/exts/pride/pride_anthem.py')
-rw-r--r--bot/exts/pride/pride_anthem.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/bot/exts/pride/pride_anthem.py b/bot/exts/pride/pride_anthem.py
new file mode 100644
index 00000000..186c5fff
--- /dev/null
+++ b/bot/exts/pride/pride_anthem.py
@@ -0,0 +1,57 @@
+import json
+import logging
+import random
+from pathlib import Path
+
+from discord.ext import commands
+
+log = logging.getLogger(__name__)
+
+
+class PrideAnthem(commands.Cog):
+ """Embed a random youtube video for a gay anthem!"""
+
+ def __init__(self, bot: commands.Bot):
+ self.bot = bot
+ self.anthems = self.load_vids()
+
+ def get_video(self, genre: str = None) -> dict:
+ """
+ Picks a random anthem from the list.
+
+ If `genre` is supplied, it will pick from videos attributed with that genre.
+ If none can be found, it will log this as well as provide that information to the user.
+ """
+ if not genre:
+ return random.choice(self.anthems)
+ else:
+ songs = [song for song in self.anthems if genre.casefold() in song["genre"]]
+ try:
+ return random.choice(songs)
+ except IndexError:
+ log.info("No videos for that genre.")
+
+ @staticmethod
+ def load_vids() -> list:
+ """Loads a list of videos from the resources folder as dictionaries."""
+ with open(Path("bot/resources/pride/anthems.json"), "r", encoding="utf-8") as f:
+ anthems = json.load(f)
+ return anthems
+
+ @commands.command(name="prideanthem", aliases=["anthem", "pridesong"])
+ async def prideanthem(self, ctx: commands.Context, genre: str = None) -> None:
+ """
+ Sends a message with a video of a random pride anthem.
+
+ If `genre` is supplied, it will select from that genre only.
+ """
+ anthem = self.get_video(genre)
+ if anthem:
+ await ctx.send(anthem["url"])
+ else:
+ await ctx.send("I couldn't find a video, sorry!")
+
+
+def setup(bot: commands.Bot) -> None:
+ """Cog loader for pride anthem."""
+ bot.add_cog(PrideAnthem(bot))