diff options
Diffstat (limited to 'bot/exts')
| -rw-r--r-- | bot/exts/halloween/8ball.py | 17 | ||||
| -rw-r--r-- | bot/exts/halloween/candy_collection.py | 35 | ||||
| -rw-r--r-- | bot/exts/halloween/hacktober-issue-finder.py | 73 | ||||
| -rw-r--r-- | bot/exts/halloween/hacktoberstats.py | 108 | ||||
| -rw-r--r-- | bot/exts/halloween/halloween_facts.py | 15 | ||||
| -rw-r--r-- | bot/exts/halloween/halloweenify.py | 13 | ||||
| -rw-r--r-- | bot/exts/halloween/monsterbio.py | 10 | ||||
| -rw-r--r-- | bot/exts/halloween/monstersurvey.py | 6 | ||||
| -rw-r--r-- | bot/exts/halloween/scarymovie.py | 74 | ||||
| -rw-r--r-- | bot/exts/halloween/spookygif.py | 23 | ||||
| -rw-r--r-- | bot/exts/halloween/spookynamerate.py | 21 | ||||
| -rw-r--r-- | bot/exts/halloween/spookyrating.py | 14 | ||||
| -rw-r--r-- | bot/exts/halloween/spookyreact.py | 29 | ||||
| -rw-r--r-- | bot/exts/halloween/timeleft.py | 8 | 
14 files changed, 216 insertions, 230 deletions
| diff --git a/bot/exts/halloween/8ball.py b/bot/exts/halloween/8ball.py index 1df48fbf..59d4acc5 100644 --- a/bot/exts/halloween/8ball.py +++ b/bot/exts/halloween/8ball.py @@ -6,28 +6,27 @@ from pathlib import Path  from discord.ext import commands +from bot.bot import Bot +  log = logging.getLogger(__name__) -with open(Path("bot/resources/halloween/responses.json"), "r", encoding="utf8") as f: -    responses = json.load(f) +with Path("bot/resources/halloween/responses.json").open("r", encoding="utf8") as f: +    RESPONSES = json.load(f)  class SpookyEightBall(commands.Cog):      """Spooky Eightball answers.""" -    def __init__(self, bot: commands.Bot): -        self.bot = bot -      @commands.command(aliases=('spooky8ball',))      async def spookyeightball(self, ctx: commands.Context, *, question: str) -> None:          """Responds with a random response to a question.""" -        choice = random.choice(responses['responses']) +        choice = random.choice(RESPONSES["responses"])          msg = await ctx.send(choice[0])          if len(choice) > 1:              await asyncio.sleep(random.randint(2, 5))              await msg.edit(content=f"{choice[0]} \n{choice[1]}") -def setup(bot: commands.Bot) -> None: -    """Spooky Eight Ball Cog Load.""" -    bot.add_cog(SpookyEightBall(bot)) +def setup(bot: Bot) -> None: +    """Load the Spooky Eight Ball Cog.""" +    bot.add_cog(SpookyEightBall()) diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py index 40e21f40..5441d8a5 100644 --- a/bot/exts/halloween/candy_collection.py +++ b/bot/exts/halloween/candy_collection.py @@ -6,6 +6,7 @@ import discord  from async_rediscache import RedisCache  from discord.ext import commands +from bot.bot import Bot  from bot.constants import Channels, Month  from bot.utils.decorators import in_month @@ -40,7 +41,7 @@ class CandyCollection(commands.Cog):      candy_messages = RedisCache()      skull_messages = RedisCache() -    def __init__(self, bot: commands.Bot): +    def __init__(self, bot: Bot):          self.bot = bot      @in_month(Month.OCTOBER) @@ -60,15 +61,15 @@ class CandyCollection(commands.Cog):          # do random check for skull first as it has the lower chance          if random.randint(1, ADD_SKULL_REACTION_CHANCE) == 1:              await self.skull_messages.set(message.id, "skull") -            return await message.add_reaction(EMOJIS['SKULL']) +            await message.add_reaction(EMOJIS["SKULL"])          # check for the candy chance next -        if random.randint(1, ADD_CANDY_REACTION_CHANCE) == 1: +        elif random.randint(1, ADD_CANDY_REACTION_CHANCE) == 1:              await self.candy_messages.set(message.id, "candy") -            return await message.add_reaction(EMOJIS['CANDY']) +            await message.add_reaction(EMOJIS["CANDY"])      @in_month(Month.OCTOBER)      @commands.Cog.listener() -    async def on_reaction_add(self, reaction: discord.Reaction, user: discord.Member) -> None: +    async def on_reaction_add(self, reaction: discord.Reaction, user: Union[discord.User, discord.Member]) -> None:          """Add/remove candies from a person if the reaction satisfies criteria."""          message = reaction.message          # check to ensure the reactor is human @@ -81,7 +82,7 @@ class CandyCollection(commands.Cog):          # if its not a candy or skull, and it is one of 10 most recent messages,          # proceed to add a skull/candy with higher chance -        if str(reaction.emoji) not in (EMOJIS['SKULL'], EMOJIS['CANDY']): +        if str(reaction.emoji) not in (EMOJIS["SKULL"], EMOJIS["CANDY"]):              recent_message_ids = map(                  lambda m: m.id,                  await self.hacktober_channel.history(limit=10).flatten() @@ -90,14 +91,14 @@ class CandyCollection(commands.Cog):                  await self.reacted_msg_chance(message)              return -        if await self.candy_messages.get(message.id) == "candy" and str(reaction.emoji) == EMOJIS['CANDY']: +        if await self.candy_messages.get(message.id) == "candy" and str(reaction.emoji) == EMOJIS["CANDY"]:              await self.candy_messages.delete(message.id)              if await self.candy_records.contains(user.id):                  await self.candy_records.increment(user.id)              else:                  await self.candy_records.set(user.id, 1) -        elif await self.skull_messages.get(message.id) == "skull" and str(reaction.emoji) == EMOJIS['SKULL']: +        elif await self.skull_messages.get(message.id) == "skull" and str(reaction.emoji) == EMOJIS["SKULL"]:              await self.skull_messages.delete(message.id)              if prev_record := await self.candy_records.get(user.id): @@ -124,11 +125,11 @@ class CandyCollection(commands.Cog):          """          if random.randint(1, ADD_SKULL_EXISTING_REACTION_CHANCE) == 1:              await self.skull_messages.set(message.id, "skull") -            return await message.add_reaction(EMOJIS['SKULL']) +            await message.add_reaction(EMOJIS['SKULL']) -        if random.randint(1, ADD_CANDY_EXISTING_REACTION_CHANCE) == 1: +        elif random.randint(1, ADD_CANDY_EXISTING_REACTION_CHANCE) == 1:              await self.candy_messages.set(message.id, "candy") -            return await message.add_reaction(EMOJIS['CANDY']) +            await message.add_reaction(EMOJIS["CANDY"])      @property      def hacktober_channel(self) -> discord.TextChannel: @@ -141,8 +142,10 @@ class CandyCollection(commands.Cog):      ) -> None:          """Send a spooky message."""          e = discord.Embed(colour=author.colour) -        e.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; " -                          f"I took {candies} candies and quickly took flight.") +        e.set_author( +            name="Ghosts and Ghouls and Jack o' lanterns at night; " +            f"I took {candies} candies and quickly took flight." +        )          await channel.send(embed=e)      @staticmethod @@ -173,7 +176,7 @@ class CandyCollection(commands.Cog):              return '\n'.join(                  f"{EMOJIS['MEDALS'][index]} <@{record[0]}>: {record[1]}"                  for index, record in enumerate(top_five) -            ) if top_five else 'No Candies' +            ) if top_five else "No Candies"          e = discord.Embed(colour=discord.Colour.blurple())          e.add_field( @@ -191,6 +194,6 @@ class CandyCollection(commands.Cog):          await ctx.send(embed=e) -def setup(bot: commands.Bot) -> None: -    """Candy Collection game Cog load.""" +def setup(bot: Bot) -> None: +    """Load the Candy Collection Cog."""      bot.add_cog(CandyCollection(bot)) diff --git a/bot/exts/halloween/hacktober-issue-finder.py b/bot/exts/halloween/hacktober-issue-finder.py index 9deadde9..c88e2b6f 100644 --- a/bot/exts/halloween/hacktober-issue-finder.py +++ b/bot/exts/halloween/hacktober-issue-finder.py @@ -3,10 +3,10 @@ import logging  import random  from typing import Dict, Optional -import aiohttp  import discord  from discord.ext import commands +from bot.bot import Bot  from bot.constants import Month, Tokens  from bot.utils.decorators import in_month @@ -25,7 +25,7 @@ if GITHUB_TOKEN := Tokens.github:  class HacktoberIssues(commands.Cog):      """Find a random hacktober python issue on GitHub.""" -    def __init__(self, bot: commands.Bot): +    def __init__(self, bot: Bot):          self.bot = bot          self.cache_normal = None          self.cache_timer_normal = datetime.datetime(1, 1, 1) @@ -41,7 +41,7 @@ class HacktoberIssues(commands.Cog):          If the command is run with beginner (`.hacktoberissues beginner`):          It will also narrow it down to the "first good issue" label.          """ -        with ctx.typing(): +        async with ctx.typing():              issues = await self.get_issues(ctx, option)              if issues is None:                  return @@ -59,40 +59,39 @@ class HacktoberIssues(commands.Cog):              log.debug("using cache")              return self.cache_normal -        async with aiohttp.ClientSession() as session: +        if option == "beginner": +            url = URL + '+label:"good first issue"' +            if self.cache_beginner is not None: +                page = random.randint(1, min(1000, self.cache_beginner["total_count"]) // 100) +                url += f"&page={page}" +        else: +            url = URL +            if self.cache_normal is not None: +                page = random.randint(1, min(1000, self.cache_normal["total_count"]) // 100) +                url += f"&page={page}" + +        log.debug(f"making api request to url: {url}") +        async with self.bot.http_session.get(url, headers=REQUEST_HEADERS) as response: +            if response.status != 200: +                log.error(f"expected 200 status (got {response.status}) from the GitHub api.") +                await ctx.send(f"ERROR: expected 200 status (got {response.status}) from the GitHub api.") +                await ctx.send(await response.text()) +                return None +            data = await response.json() + +            if len(data["items"]) == 0: +                log.error(f"no issues returned from GitHub api. with url: {response.url}") +                await ctx.send(f"ERROR: no issues returned from GitHub api. with url: {response.url}") +                return None +              if option == "beginner": -                url = URL + '+label:"good first issue"' -                if self.cache_beginner is not None: -                    page = random.randint(1, min(1000, self.cache_beginner["total_count"]) // 100) -                    url += f"&page={page}" +                self.cache_beginner = data +                self.cache_timer_beginner = ctx.message.created_at              else: -                url = URL -                if self.cache_normal is not None: -                    page = random.randint(1, min(1000, self.cache_normal["total_count"]) // 100) -                    url += f"&page={page}" - -            log.debug(f"making api request to url: {url}") -            async with session.get(url, headers=REQUEST_HEADERS) as response: -                if response.status != 200: -                    log.error(f"expected 200 status (got {response.status}) from the GitHub api.") -                    await ctx.send(f"ERROR: expected 200 status (got {response.status}) from the GitHub api.") -                    await ctx.send(await response.text()) -                    return None -                data = await response.json() - -                if len(data["items"]) == 0: -                    log.error(f"no issues returned from GitHub api. with url: {response.url}") -                    await ctx.send(f"ERROR: no issues returned from GitHub api. with url: {response.url}") -                    return None - -                if option == "beginner": -                    self.cache_beginner = data -                    self.cache_timer_beginner = ctx.message.created_at -                else: -                    self.cache_normal = data -                    self.cache_timer_normal = ctx.message.created_at - -                return data +                self.cache_normal = data +                self.cache_timer_normal = ctx.message.created_at + +            return data      @staticmethod      def format_embed(issue: Dict) -> discord.Embed: @@ -111,6 +110,6 @@ class HacktoberIssues(commands.Cog):          return embed -def setup(bot: commands.Bot) -> None: -    """Hacktober issue finder Cog Load.""" +def setup(bot: Bot) -> None: +    """Load the HacktoberIssue finder."""      bot.add_cog(HacktoberIssues(bot)) diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index d9fc0e8a..9695ba2a 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -5,11 +5,11 @@ from collections import Counter  from datetime import datetime, timedelta  from typing import List, Optional, Tuple, Union -import aiohttp  import discord  from async_rediscache import RedisCache  from discord.ext import commands +from bot.bot import Bot  from bot.constants import Channels, Month, NEGATIVE_REPLIES, Tokens, WHITELISTED_CHANNELS  from bot.utils.decorators import in_month, whitelist_override @@ -39,7 +39,7 @@ class HacktoberStats(commands.Cog):      # Stores mapping of user IDs and GitHub usernames      linked_accounts = RedisCache() -    def __init__(self, bot: commands.Bot): +    def __init__(self, bot: Bot):          self.bot = bot      @in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER) @@ -83,15 +83,15 @@ class HacktoberStats(commands.Cog):          if github_username:              if await self.linked_accounts.contains(author_id):                  old_username = await self.linked_accounts.get(author_id) -                logging.info(f"{author_id} has changed their github link from '{old_username}' to '{github_username}'") +                log.info(f"{author_id} has changed their github link from '{old_username}' to '{github_username}'")                  await ctx.send(f"{author_mention}, your GitHub username has been updated to: '{github_username}'")              else: -                logging.info(f"{author_id} has added a github link to '{github_username}'") +                log.info(f"{author_id} has added a github link to '{github_username}'")                  await ctx.send(f"{author_mention}, your GitHub username has been added")              await self.linked_accounts.set(author_id, github_username)          else: -            logging.info(f"{author_id} tried to link a GitHub account but didn't provide a username") +            log.info(f"{author_id} tried to link a GitHub account but didn't provide a username")              await ctx.send(f"{author_mention}, a GitHub username is required to link your account")      @in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER) @@ -157,7 +157,7 @@ class HacktoberStats(commands.Cog):          stats_embed = discord.Embed(              title=f"{github_username}'s Hacktoberfest", -            color=discord.Color(0x9c4af7), +            color=0x9c4af7,              description=(                  f"{github_username} has made {n} valid "                  f"{self._contributionator(n)} in " @@ -188,8 +188,7 @@ class HacktoberStats(commands.Cog):          logging.info(f"Hacktoberfest PR built for GitHub user '{github_username}'")          return stats_embed -    @staticmethod -    async def get_october_prs(github_username: str) -> Optional[List[dict]]: +    async def get_october_prs(self, github_username: str) -> Optional[List[dict]]:          """          Query GitHub's API for PRs created during the month of October by github_username. @@ -212,7 +211,7 @@ class HacktoberStats(commands.Cog):          Otherwise, return empty list.          None will be returned when the GitHub user was not found.          """ -        logging.info(f"Fetching Hacktoberfest Stats for GitHub user: '{github_username}'") +        log.info(f"Fetching Hacktoberfest Stats for GitHub user: '{github_username}'")          base_url = "https://api.github.com/search/issues?q="          action_type = "pr"          is_query = "public" @@ -228,24 +227,24 @@ class HacktoberStats(commands.Cog):              f"+created:{date_range}"              f"&per_page={per_page}"          ) -        logging.debug(f"GitHub query URL generated: {query_url}") +        log.logProcesses.debug(f"GitHub query URL generated: {query_url}") -        jsonresp = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) -        if "message" in jsonresp.keys(): +        jsonresp = await self._fetch_url(query_url, REQUEST_HEADERS) +        if "message" in jsonresp:              # One of the parameters is invalid, short circuit for now              api_message = jsonresp["errors"][0]["message"]              # Ignore logging non-existent users or users we do not have permission to see              if api_message == GITHUB_NONEXISTENT_USER_MESSAGE: -                logging.debug(f"No GitHub user found named '{github_username}'") +                log.debug(f"No GitHub user found named '{github_username}'")                  return              else: -                logging.error(f"GitHub API request for '{github_username}' failed with message: {api_message}") +                log.error(f"GitHub API request for '{github_username}' failed with message: {api_message}")              return []  # No October PRs were found due to error          if jsonresp["total_count"] == 0:              # Short circuit if there aren't any PRs -            logging.info(f"No October PRs found for GitHub user: '{github_username}'") +            log.info(f"No October PRs found for GitHub user: '{github_username}'")              return []          logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'") @@ -253,20 +252,20 @@ class HacktoberStats(commands.Cog):          oct3 = datetime(int(CURRENT_YEAR), 10, 3, 23, 59, 59, tzinfo=None)          hackto_topics = {}  # cache whether each repo has the appropriate topic (bool values)          for item in jsonresp["items"]: -            shortname = HacktoberStats._get_shortname(item["repository_url"]) +            shortname = self._get_shortname(item["repository_url"])              itemdict = {                  "repo_url": f"https://www.github.com/{shortname}",                  "repo_shortname": shortname,                  "created_at": datetime.strptime( -                    item["created_at"], r"%Y-%m-%dT%H:%M:%SZ" +                    item["created_at"], "%Y-%m-%dT%H:%M:%SZ"                  ),                  "number": item["number"]              }              # If the PR has 'invalid' or 'spam' labels, the PR must be              # either merged or approved for it to be included -            if HacktoberStats._has_label(item, ["invalid", "spam"]): -                if not await HacktoberStats._is_accepted(itemdict): +            if self._has_label(item, ["invalid", "spam"]): +                if not await self._is_accepted(itemdict):                      continue              # PRs before oct 3 no need to check for topics @@ -277,21 +276,20 @@ class HacktoberStats(commands.Cog):                  continue              # Checking PR's labels for "hacktoberfest-accepted" -            if HacktoberStats._has_label(item, "hacktoberfest-accepted"): +            if self._has_label(item, "hacktoberfest-accepted"):                  outlist.append(itemdict)                  continue              # No need to query GitHub if repo topics are fetched before already -            if shortname in hackto_topics.keys(): -                if hackto_topics[shortname]: -                    outlist.append(itemdict) -                    continue +            if hackto_topics.get(shortname): +                outlist.append(itemdict) +                continue              # Fetch topics for the PR's repo              topics_query_url = f"https://api.github.com/repos/{shortname}/topics" -            logging.debug(f"Fetching repo topics for {shortname} with url: {topics_query_url}") -            jsonresp2 = await HacktoberStats._fetch_url(topics_query_url, GITHUB_TOPICS_ACCEPT_HEADER) +            log.debug(f"Fetching repo topics for {shortname} with url: {topics_query_url}") +            jsonresp2 = await self._fetch_url(topics_query_url, GITHUB_TOPICS_ACCEPT_HEADER)              if jsonresp2.get("names") is None: -                logging.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}") +                log.error(f"Error fetching topics for {shortname}: {jsonresp2['message']}")                  continue  # Assume the repo doesn't have the `hacktoberfest` topic if API  request errored              # PRs after oct 3 that doesn't have 'hacktoberfest-accepted' label @@ -301,12 +299,10 @@ class HacktoberStats(commands.Cog):                  outlist.append(itemdict)          return outlist -    @staticmethod -    async def _fetch_url(url: str, headers: dict) -> dict: +    async def _fetch_url(self, url: str, headers: dict) -> dict:          """Retrieve API response from URL.""" -        async with aiohttp.ClientSession() as session: -            async with session.get(url, headers=headers) as resp: -                jsonresp = await resp.json() +        async with self.bot.http_session.get(url, headers=headers) as resp: +            jsonresp = await resp.json()          return jsonresp      @staticmethod @@ -319,40 +315,36 @@ class HacktoberStats(commands.Cog):          """          if not pr.get("labels"):  # if PR has no labels              return False -        if (isinstance(labels, str)) and (any(label["name"].casefold() == labels for label in pr["labels"])): +        if isinstance(labels, str) and any(label["name"].casefold() == labels for label in pr["labels"]):              return True          for item in labels:              if any(label["name"].casefold() == item for label in pr["labels"]):                  return True          return False -    @staticmethod -    async def _is_accepted(pr: dict) -> bool: +    async def _is_accepted(self, pr: dict) -> bool:          """Check if a PR is merged, approved, or labelled hacktoberfest-accepted."""          # checking for merge status -        query_url = f"https://api.github.com/repos/{pr['repo_shortname']}/pulls/" -        query_url += str(pr["number"]) -        jsonresp = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) - -        if "message" in jsonresp.keys(): -            logging.error( -                f"Error fetching PR stats for #{pr['number']} in repo {pr['repo_shortname']}:\n" -                f"{jsonresp['message']}" -            ) +        query_url = f"https://api.github.com/repos/{pr['repo_shortname']}/pulls/{pr['number']}" +        jsonresp = await self._fetch_url(query_url, REQUEST_HEADERS) + +        if message := jsonresp.get("message"): +            log.error(f"Error fetching PR stats for #{pr['number']} in repo {pr['repo_shortname']}:\n{message}")              return False -        if ("merged" in jsonresp.keys()) and jsonresp["merged"]: + +        if jsonresp.get("merged"):              return True          # checking for the label, using `jsonresp` which has the label information -        if HacktoberStats._has_label(jsonresp, "hacktoberfest-accepted"): +        if self._has_label(jsonresp, "hacktoberfest-accepted"):              return True          # checking approval          query_url += "/reviews" -        jsonresp2 = await HacktoberStats._fetch_url(query_url, REQUEST_HEADERS) +        jsonresp2 = await self._fetch_url(query_url, REQUEST_HEADERS)          if isinstance(jsonresp2, dict):              # if API request is unsuccessful it will be a dict with the error in 'message' -            logging.error( +            log.error(                  f"Error fetching PR reviews for #{pr['number']} in repo {pr['repo_shortname']}:\n"                  f"{jsonresp2['message']}"              ) @@ -363,9 +355,8 @@ class HacktoberStats(commands.Cog):          # loop through reviews and check for approval          for item in jsonresp2: -            if "status" in item.keys(): -                if item['status'] == "APPROVED": -                    return True +            if item.get('status') == "APPROVED": +                return True          return False      @staticmethod @@ -381,8 +372,7 @@ class HacktoberStats(commands.Cog):          exp = r"https?:\/\/api.github.com\/repos\/([/\-\_\.\w]+)"          return re.findall(exp, in_url)[0] -    @staticmethod -    async def _categorize_prs(prs: List[dict]) -> tuple: +    async def _categorize_prs(self, prs: List[dict]) -> tuple:          """          Categorize PRs into 'in_review' and 'accepted' and returns as a tuple. @@ -399,7 +389,7 @@ class HacktoberStats(commands.Cog):          for pr in prs:              if (pr['created_at'] + timedelta(REVIEW_DAYS)) > now:                  in_review.append(pr) -            elif (pr['created_at'] <= oct3) or await HacktoberStats._is_accepted(pr): +            elif (pr['created_at'] <= oct3) or await self._is_accepted(pr):                  accepted.append(pr)          return in_review, accepted @@ -438,14 +428,14 @@ class HacktoberStats(commands.Cog):              return "contributions"      @staticmethod -    def _author_mention_from_context(ctx: commands.Context) -> Tuple: +    def _author_mention_from_context(ctx: commands.Context) -> Tuple[str, str]:          """Return stringified Message author ID and mentionable string from commands.Context.""" -        author_id = str(ctx.message.author.id) -        author_mention = ctx.message.author.mention +        author_id = str(ctx.author.id) +        author_mention = ctx.author.mention          return author_id, author_mention -def setup(bot: commands.Bot) -> None: -    """Hacktoberstats Cog load.""" +def setup(bot: Bot) -> None: +    """Load the Hacktober Stats Cog."""      bot.add_cog(HacktoberStats(bot)) diff --git a/bot/exts/halloween/halloween_facts.py b/bot/exts/halloween/halloween_facts.py index 7eb6d56f..139e0810 100644 --- a/bot/exts/halloween/halloween_facts.py +++ b/bot/exts/halloween/halloween_facts.py @@ -8,6 +8,8 @@ from typing import Tuple  import discord  from discord.ext import commands +from bot.bot import Bot +  log = logging.getLogger(__name__)  SPOOKY_EMOJIS = [ @@ -20,16 +22,15 @@ SPOOKY_EMOJIS = [      "\N{SKULL AND CROSSBONES}",      "\N{SPIDER WEB}",  ] -PUMPKIN_ORANGE = discord.Color(0xFF7518) +PUMPKIN_ORANGE = 0xFF7518  INTERVAL = timedelta(hours=6).total_seconds()  class HalloweenFacts(commands.Cog):      """A Cog for displaying interesting facts about Halloween.""" -    def __init__(self, bot: commands.Bot): -        self.bot = bot -        with open(Path("bot/resources/halloween/halloween_facts.json"), "r", encoding="utf8") as file: +    def __init__(self): +        with Path("bot/resources/halloween/halloween_facts.json").open("r", encoding="utf8") as file:              self.halloween_facts = json.load(file)          self.facts = list(enumerate(self.halloween_facts))          random.shuffle(self.facts) @@ -53,6 +54,6 @@ class HalloweenFacts(commands.Cog):          return discord.Embed(title=title, description=fact, color=PUMPKIN_ORANGE) -def setup(bot: commands.Bot) -> None: -    """Halloween facts Cog load.""" -    bot.add_cog(HalloweenFacts(bot)) +def setup(bot: Bot) -> None: +    """Load the Halloween Facts Cog.""" +    bot.add_cog(HalloweenFacts()) diff --git a/bot/exts/halloween/halloweenify.py b/bot/exts/halloween/halloweenify.py index 596c6682..5a8f4ecc 100644 --- a/bot/exts/halloween/halloweenify.py +++ b/bot/exts/halloween/halloweenify.py @@ -6,7 +6,9 @@ from random import choice  import discord  from discord.errors import Forbidden  from discord.ext import commands -from discord.ext.commands.cooldowns import BucketType +from discord.ext.commands import BucketType + +from bot.bot import Bot  log = logging.getLogger(__name__) @@ -14,9 +16,6 @@ log = logging.getLogger(__name__)  class Halloweenify(commands.Cog):      """A cog to change a invokers nickname to a spooky one!""" -    def __init__(self, bot: commands.Bot): -        self.bot = bot -      @commands.cooldown(1, 300, BucketType.user)      @commands.command()      async def halloweenify(self, ctx: commands.Context) -> None: @@ -61,6 +60,6 @@ class Halloweenify(commands.Cog):          await ctx.send(embed=embed) -def setup(bot: commands.Bot) -> None: -    """Halloweenify Cog load.""" -    bot.add_cog(Halloweenify(bot)) +def setup(bot: Bot) -> None: +    """Load the Halloweenify Cog.""" +    bot.add_cog(Halloweenify()) diff --git a/bot/exts/halloween/monsterbio.py b/bot/exts/halloween/monsterbio.py index 016a66d1..f484305d 100644 --- a/bot/exts/halloween/monsterbio.py +++ b/bot/exts/halloween/monsterbio.py @@ -6,6 +6,7 @@ from pathlib import Path  import discord  from discord.ext import commands +from bot.bot import Bot  from bot.constants import Colours  log = logging.getLogger(__name__) @@ -17,9 +18,6 @@ with open(Path("bot/resources/halloween/monster.json"), "r", encoding="utf8") as  class MonsterBio(commands.Cog):      """A cog that generates a spooky monster biography.""" -    def __init__(self, bot: commands.Bot): -        self.bot = bot -      def generate_name(self, seeded_random: random.Random) -> str:          """Generates a name (for either monster species or monster name)."""          n_candidate_strings = seeded_random.randint(2, len(TEXT_OPTIONS["monster_type"])) @@ -50,6 +48,6 @@ class MonsterBio(commands.Cog):          await ctx.send(embed=embed) -def setup(bot: commands.Bot) -> None: -    """Monster bio Cog load.""" -    bot.add_cog(MonsterBio(bot)) +def setup(bot: Bot) -> None: +    """Load the Monster Bio Cog.""" +    bot.add_cog(MonsterBio()) diff --git a/bot/exts/halloween/monstersurvey.py b/bot/exts/halloween/monstersurvey.py index 80196825..0610503d 100644 --- a/bot/exts/halloween/monstersurvey.py +++ b/bot/exts/halloween/monstersurvey.py @@ -23,9 +23,8 @@ class MonsterSurvey(Cog):      Users may change their vote, but only their current vote will be counted.      """ -    def __init__(self, bot: Bot): +    def __init__(self):          """Initializes values for the bot to use within the voting commands.""" -        self.bot = bot          self.registry_location = os.path.join(os.getcwd(), 'bot', 'resources', 'halloween', 'monstersurvey.json')          with open(self.registry_location, 'r', encoding="utf8") as jason:              self.voter_registry = json.load(jason) @@ -201,4 +200,5 @@ class MonsterSurvey(Cog):  def setup(bot: Bot) -> None: -    """Monster survey Cog load.""" +    """Load the Monster Survey Cog.""" +    bot.add_cog(MonsterSurvey()) diff --git a/bot/exts/halloween/scarymovie.py b/bot/exts/halloween/scarymovie.py index 0807eca6..48c9f53d 100644 --- a/bot/exts/halloween/scarymovie.py +++ b/bot/exts/halloween/scarymovie.py @@ -2,24 +2,25 @@ import logging  import random  from os import environ -import aiohttp  from discord import Embed  from discord.ext import commands +from bot.bot import Bot +  log = logging.getLogger(__name__) -TMDB_API_KEY = environ.get('TMDB_API_KEY') -TMDB_TOKEN = environ.get('TMDB_TOKEN') +TMDB_API_KEY = environ.get("TMDB_API_KEY") +TMDB_TOKEN = environ.get("TMDB_TOKEN")  class ScaryMovie(commands.Cog):      """Selects a random scary movie and embeds info into Discord chat.""" -    def __init__(self, bot: commands.Bot): +    def __init__(self, bot: Bot):          self.bot = bot -    @commands.command(name='scarymovie', alias=['smovie']) +    @commands.command(name="scarymovie", alias=["smovie"])      async def random_movie(self, ctx: commands.Context) -> None:          """Randomly select a scary movie and display information about it."""          async with ctx.typing(): @@ -28,36 +29,34 @@ class ScaryMovie(commands.Cog):          await ctx.send(embed=movie_details) -    @staticmethod -    async def select_movie() -> dict: +    async def select_movie(self) -> dict:          """Selects a random movie and returns a JSON of movie details from TMDb.""" -        url = 'https://api.themoviedb.org/4/discover/movie' +        url = "https://api.themoviedb.org/4/discover/movie"          params = { -            'with_genres': '27', -            'vote_count.gte': '5' +            "with_genres": "27", +            "vote_count.gte": "5"          }          headers = { -            'Authorization': 'Bearer ' + TMDB_TOKEN, -            'Content-Type': 'application/json;charset=utf-8' +            "Authorization": "Bearer " + TMDB_TOKEN, +            "Content-Type": "application/json;charset=utf-8"          }          # Get total page count of horror movies -        async with aiohttp.ClientSession() as session: -            response = await session.get(url=url, params=params, headers=headers) -            total_pages = await response.json() -            total_pages = total_pages.get('total_pages') - -            # Get movie details from one random result on a random page -            params['page'] = random.randint(1, total_pages) -            response = await session.get(url=url, params=params, headers=headers) -            response = await response.json() -            selection_id = random.choice(response.get('results')).get('id') - -            # Get full details and credits -            selection = await session.get( -                url='https://api.themoviedb.org/3/movie/' + str(selection_id), -                params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'} -            ) +        async with self.bot.http_session.get(url=url, params=params, headers=headers) as response: +            data = await response.json() +            total_pages = data.get("total_pages") + +        # Get movie details from one random result on a random page +        params['page'] = random.randint(1, total_pages) +        async with self.bot.http_session.get(url=url, params=params, headers=headers) as response: +            data = await response.json() +            selection_id = random.choice(data.get("results")).get("id") + +        # Get full details and credits +        async with self.bot.http_session.get( +            url=f"https://api.themoviedb.org/3/movie/{selection_id}", +            params={"api_key": TMDB_API_KEY, "append_to_response": "credits"} +        ) as selection:              return await selection.json() @@ -67,8 +66,8 @@ class ScaryMovie(commands.Cog):          # Build the relevant URLs.          movie_id = movie.get("id")          poster_path = movie.get("poster_path") -        tmdb_url = f'https://www.themoviedb.org/movie/{movie_id}' if movie_id else None -        poster = f'https://image.tmdb.org/t/p/original{poster_path}' if poster_path else None +        tmdb_url = f"https://www.themoviedb.org/movie/{movie_id}" if movie_id else None +        poster = f"https://image.tmdb.org/t/p/original{poster_path}" if poster_path else None          # Get cast names          cast = [] @@ -82,10 +81,7 @@ class ScaryMovie(commands.Cog):          # Determine the spookiness rating          rating = '' -        rating_count = movie.get('vote_average', 0) - -        if rating_count: -            rating_count /= 2 +        rating_count = movie.get('vote_average', 0) / 2          for _ in range(int(rating_count)):              rating += ':skull:' @@ -100,7 +96,7 @@ class ScaryMovie(commands.Cog):          # Not all these attributes will always be present          movie_attributes = {              "Directed by": director, -            "Starring": ', '.join(cast), +            "Starring": ", ".join(cast),              "Running time": runtime,              "Release year": year,              "Spookiness rating": rating, @@ -108,9 +104,9 @@ class ScaryMovie(commands.Cog):          embed = Embed(              colour=0x01d277, -            title='**' + movie.get('title') + '**', +            title=f"**{movie.get('title')}**",              url=tmdb_url, -            description=movie.get('overview') +            description=movie.get("overview")          )          if poster: @@ -127,6 +123,6 @@ class ScaryMovie(commands.Cog):          return embed -def setup(bot: commands.Bot) -> None: -    """Scary movie Cog load.""" +def setup(bot: Bot) -> None: +    """Load the Scary Movie Cog."""      bot.add_cog(ScaryMovie(bot)) diff --git a/bot/exts/halloween/spookygif.py b/bot/exts/halloween/spookygif.py index f402437f..bfdf2128 100644 --- a/bot/exts/halloween/spookygif.py +++ b/bot/exts/halloween/spookygif.py @@ -1,9 +1,9 @@  import logging -import aiohttp  import discord  from discord.ext import commands +from bot.bot import Bot  from bot.constants import Tokens  log = logging.getLogger(__name__) @@ -12,27 +12,26 @@ log = logging.getLogger(__name__)  class SpookyGif(commands.Cog):      """A cog to fetch a random spooky gif from the web!""" -    def __init__(self, bot: commands.Bot): +    def __init__(self, bot: Bot):          self.bot = bot      @commands.command(name="spookygif", aliases=("sgif", "scarygif"))      async def spookygif(self, ctx: commands.Context) -> None:          """Fetches a random gif from the GIPHY API and responds with it."""          async with ctx.typing(): -            async with aiohttp.ClientSession() as session: -                params = {'api_key': Tokens.giphy, 'tag': 'halloween', 'rating': 'g'} -                # Make a GET request to the Giphy API to get a random halloween gif. -                async with session.get('http://api.giphy.com/v1/gifs/random', params=params) as resp: -                    data = await resp.json() -                url = data['data']['image_url'] +            params = {'api_key': Tokens.giphy, 'tag': 'halloween', 'rating': 'g'} +            # Make a GET request to the Giphy API to get a random halloween gif. +            async with self.bot.http_session.get('http://api.giphy.com/v1/gifs/random', params=params) as resp: +                data = await resp.json() +            url = data['data']['image_url'] -                embed = discord.Embed(colour=0x9b59b6) -                embed.title = "A spooooky gif!" -                embed.set_image(url=url) +            embed = discord.Embed(colour=0x9b59b6) +            embed.title = "A spooooky gif!" +            embed.set_image(url=url)          await ctx.send(embed=embed) -def setup(bot: commands.Bot) -> None: +def setup(bot: Bot) -> None:      """Spooky GIF Cog load."""      bot.add_cog(SpookyGif(bot)) diff --git a/bot/exts/halloween/spookynamerate.py b/bot/exts/halloween/spookynamerate.py index e2950343..9191f5f6 100644 --- a/bot/exts/halloween/spookynamerate.py +++ b/bot/exts/halloween/spookynamerate.py @@ -12,8 +12,9 @@ from async_rediscache import RedisCache  from discord import Embed, Reaction, TextChannel, User  from discord.colour import Colour  from discord.ext import tasks -from discord.ext.commands import Bot, Cog, Context, group +from discord.ext.commands import Cog, Context, group +from bot.bot import Bot  from bot.constants import Channels, Client, Colours, Month  from bot.utils.decorators import InMonthCheckFailure @@ -34,7 +35,7 @@ ADDED_MESSAGES = [  ]  PING = "<@{id}>" -EMOJI_MESSAGE = "\n".join([f"- {emoji} {val}" for emoji, val in EMOJIS_VAL.items()]) +EMOJI_MESSAGE = "\n".join(f"- {emoji} {val}" for emoji, val in EMOJIS_VAL.items())  HELP_MESSAGE_DICT = {      "title": "Spooky Name Rate",      "description": f"Help for the `{Client.prefix}spookynamerate` command", @@ -137,14 +138,12 @@ class SpookyNameRate(Cog):      async def add_name(self, ctx: Context, *, name: str) -> None:          """Use this command to add/register your spookified name."""          if self.poll: -            logger.info(f"{ctx.message.author} tried to add a name, but the poll had already started.") +            logger.info(f"{ctx.author} tried to add a name, but the poll had already started.")              await ctx.send("Sorry, the poll has started! You can try and participate in the next round though!")              return -        message = ctx.message -          for data in (json.loads(user_data) for _, user_data in await self.messages.items()): -            if data["author"] == message.author.id: +            if data["author"] == ctx.author.id:                  await ctx.send(                      "But you have already added an entry! Type "                      f"`{self.bot.command_prefix}spookynamerate " @@ -156,14 +155,14 @@ class SpookyNameRate(Cog):                  await ctx.send("TOO LATE. Someone has already added this name.")                  return -        msg = await (await self.get_channel()).send(f"{message.author.mention} added the name {name!r}!") +        msg = await (await self.get_channel()).send(f"{ctx.author.mention} added the name {name!r}!")          await self.messages.set(              msg.id,              json.dumps(                  {                      "name": name, -                    "author": message.author.id, +                    "author": ctx.author.id,                      "score": 0,                  }              ), @@ -172,7 +171,7 @@ class SpookyNameRate(Cog):          for emoji in EMOJIS_VAL:              await msg.add_reaction(emoji) -        logger.info(f"{message.author} added the name {name!r}") +        logger.info(f"{ctx.author} added the name {name!r}")      @spooky_name_rate.command(name="delete")      async def delete_name(self, ctx: Context) -> None: @@ -185,7 +184,7 @@ class SpookyNameRate(Cog):              if ctx.author.id == data["author"]:                  await self.messages.delete(message_id) -                await ctx.send(f'Name deleted successfully ({data["name"]!r})!') +                await ctx.send(f"Name deleted successfully ({data['name']!r})!")                  return          await ctx.send( @@ -397,5 +396,5 @@ class SpookyNameRate(Cog):  def setup(bot: Bot) -> None: -    """Loads the SpookyNameRate Cog.""" +    """Load the SpookyNameRate Cog."""      bot.add_cog(SpookyNameRate(bot)) diff --git a/bot/exts/halloween/spookyrating.py b/bot/exts/halloween/spookyrating.py index 6f069f8c..dc398e2e 100644 --- a/bot/exts/halloween/spookyrating.py +++ b/bot/exts/halloween/spookyrating.py @@ -7,20 +7,20 @@ from pathlib import Path  import discord  from discord.ext import commands +from bot.bot import Bot  from bot.constants import Colours  log = logging.getLogger(__name__)  with Path("bot/resources/halloween/spooky_rating.json").open(encoding="utf8") as file: -    SPOOKY_DATA = json.load(file) -    SPOOKY_DATA = sorted((int(key), value) for key, value in SPOOKY_DATA.items()) +    data = json.load(file) +    SPOOKY_DATA = sorted((int(key), value) for key, value in data.items())  class SpookyRating(commands.Cog):      """A cog for calculating one's spooky rating.""" -    def __init__(self, bot: commands.Bot): -        self.bot = bot +    def __init__(self):          self.local_random = random.Random()      @commands.command() @@ -61,6 +61,6 @@ class SpookyRating(commands.Cog):          await ctx.send(embed=embed) -def setup(bot: commands.Bot) -> None: -    """Spooky Rating Cog load.""" -    bot.add_cog(SpookyRating(bot)) +def setup(bot: Bot) -> None: +    """Load the Spooky Rating Cog.""" +    bot.add_cog(SpookyRating()) diff --git a/bot/exts/halloween/spookyreact.py b/bot/exts/halloween/spookyreact.py index b335df75..dabc3c1f 100644 --- a/bot/exts/halloween/spookyreact.py +++ b/bot/exts/halloween/spookyreact.py @@ -2,8 +2,9 @@ import logging  import re  import discord -from discord.ext.commands import Bot, Cog +from discord.ext.commands import Cog +from bot.bot import Bot  from bot.constants import Month  from bot.utils.decorators import in_month @@ -28,20 +29,20 @@ class SpookyReact(Cog):      @in_month(Month.OCTOBER)      @Cog.listener() -    async def on_message(self, ctx: discord.Message) -> None: +    async def on_message(self, message: discord.Message) -> None:          """Triggered when the bot sees a message in October.""" -        for trigger in SPOOKY_TRIGGERS.keys(): -            trigger_test = re.search(SPOOKY_TRIGGERS[trigger][0], ctx.content.lower()) +        for name, trigger in SPOOKY_TRIGGERS.items(): +            trigger_test = re.search(trigger[0], message.content.lower())              if trigger_test:                  # Check message for bot replies and/or command invocations                  # Short circuit if they're found, logging is handled in _short_circuit_check -                if await self._short_circuit_check(ctx): +                if await self._short_circuit_check(message):                      return                  else: -                    await ctx.add_reaction(SPOOKY_TRIGGERS[trigger][1]) -                    logging.info(f"Added '{trigger}' reaction to message ID: {ctx.id}") +                    await message.add_reaction(trigger[1]) +                    log.info(f"Added {name!r} reaction to message ID: {message.id}") -    async def _short_circuit_check(self, ctx: discord.Message) -> bool: +    async def _short_circuit_check(self, message: discord.Message) -> bool:          """          Short-circuit helper check. @@ -50,20 +51,20 @@ class SpookyReact(Cog):            * prefix is not None          """          # Check for self reaction -        if ctx.author == self.bot.user: -            logging.debug(f"Ignoring reactions on self message. Message ID: {ctx.id}") +        if message.author == self.bot.user: +            log.debug(f"Ignoring reactions on self message. Message ID: {message.id}")              return True          # Check for command invocation          # Because on_message doesn't give a full Context object, generate one first -        tmp_ctx = await self.bot.get_context(ctx) -        if tmp_ctx.prefix: -            logging.debug(f"Ignoring reactions on command invocation. Message ID: {ctx.id}") +        ctx = await self.bot.get_context(message) +        if ctx.prefix: +            log.debug(f"Ignoring reactions on command invocation. Message ID: {message.id}")              return True          return False  def setup(bot: Bot) -> None: -    """Spooky reaction Cog load.""" +    """Load the Spooky Reaction Cog."""      bot.add_cog(SpookyReact(bot)) diff --git a/bot/exts/halloween/timeleft.py b/bot/exts/halloween/timeleft.py index 47adb09b..f4ab9284 100644 --- a/bot/exts/halloween/timeleft.py +++ b/bot/exts/halloween/timeleft.py @@ -4,13 +4,15 @@ from typing import Tuple  from discord.ext import commands +from bot.bot import Bot +  log = logging.getLogger(__name__)  class TimeLeft(commands.Cog):      """A Cog that tells you how long left until Hacktober is over!""" -    def __init__(self, bot: commands.Bot): +    def __init__(self, bot: Bot):          self.bot = bot      def in_hacktober(self) -> bool: @@ -64,6 +66,6 @@ class TimeLeft(commands.Cog):              ) -def setup(bot: commands.Bot) -> None: -    """Cog load.""" +def setup(bot: Bot) -> None: +    """Load the Time Left Cog."""      bot.add_cog(TimeLeft(bot)) | 
