aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts
diff options
context:
space:
mode:
Diffstat (limited to 'bot/exts')
-rw-r--r--bot/exts/easter/egg_facts.py8
-rw-r--r--bot/exts/evergreen/branding.py543
-rw-r--r--bot/exts/evergreen/emoji_count.py94
-rw-r--r--bot/exts/evergreen/error_handler.py6
-rw-r--r--bot/exts/evergreen/game.py6
-rw-r--r--bot/exts/evergreen/help.py8
-rw-r--r--bot/exts/evergreen/issues.py2
-rw-r--r--bot/exts/evergreen/showprojects.py33
-rw-r--r--bot/exts/evergreen/source.py2
-rw-r--r--bot/exts/evergreen/space.py6
-rw-r--r--bot/exts/evergreen/wonder_twins.py49
-rw-r--r--bot/exts/halloween/candy_collection.py235
-rw-r--r--bot/exts/halloween/hacktoberstats.py88
-rw-r--r--bot/exts/halloween/monstersurvey.py1
-rw-r--r--bot/exts/halloween/spookyreact.py8
-rw-r--r--bot/exts/pride/pride_facts.py8
-rw-r--r--bot/exts/utils/extensions.py2
-rw-r--r--bot/exts/valentines/be_my_valentine.py2
18 files changed, 243 insertions, 858 deletions
diff --git a/bot/exts/easter/egg_facts.py b/bot/exts/easter/egg_facts.py
index 0051aa50..761e9059 100644
--- a/bot/exts/easter/egg_facts.py
+++ b/bot/exts/easter/egg_facts.py
@@ -6,7 +6,7 @@ from pathlib import Path
import discord
from discord.ext import commands
-from bot.bot import SeasonalBot
+from bot.bot import Bot
from bot.constants import Channels, Colours, Month
from bot.utils.decorators import seasonal_task
@@ -20,7 +20,7 @@ class EasterFacts(commands.Cog):
It also contains a background task which sends an easter egg fact in the event channel everyday.
"""
- def __init__(self, bot: SeasonalBot):
+ def __init__(self, bot: Bot):
self.bot = bot
self.facts = self.load_json()
@@ -38,7 +38,7 @@ class EasterFacts(commands.Cog):
"""A background task that sends an easter egg fact in the event channel everyday."""
await self.bot.wait_until_guild_available()
- channel = self.bot.get_channel(Channels.seasonalbot_commands)
+ channel = self.bot.get_channel(Channels.community_bot_commands)
await channel.send(embed=self.make_embed())
@commands.command(name='eggfact', aliases=['fact'])
@@ -56,6 +56,6 @@ class EasterFacts(commands.Cog):
)
-def setup(bot: SeasonalBot) -> None:
+def setup(bot: Bot) -> None:
"""Easter Egg facts cog load."""
bot.add_cog(EasterFacts(bot))
diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py
deleted file mode 100644
index 7e531011..00000000
--- a/bot/exts/evergreen/branding.py
+++ /dev/null
@@ -1,543 +0,0 @@
-import asyncio
-import itertools
-import json
-import logging
-import random
-import typing as t
-from datetime import datetime, time, timedelta
-from pathlib import Path
-
-import arrow
-import discord
-from discord.embeds import EmptyEmbed
-from discord.ext import commands
-
-from bot.bot import SeasonalBot
-from bot.constants import Branding, Colours, Emojis, MODERATION_ROLES, Tokens
-from bot.seasons import SeasonBase, get_all_seasons, get_current_season, get_season
-from bot.utils import human_months
-from bot.utils.decorators import with_role
-from bot.utils.exceptions import BrandingError
-from bot.utils.persist import make_persistent
-
-log = logging.getLogger(__name__)
-
-STATUS_OK = 200 # HTTP status code
-
-FILE_BANNER = "banner.png"
-FILE_AVATAR = "avatar.png"
-SERVER_ICONS = "server_icons"
-
-BRANDING_URL = "https://api.github.com/repos/python-discord/branding/contents"
-
-PARAMS = {"ref": "master"} # Target branch
-HEADERS = {"Accept": "application/vnd.github.v3+json"} # Ensure we use API v3
-
-# A GitHub token is not necessary for the cog to operate,
-# unauthorized requests are however limited to 60 per hour
-if Tokens.github:
- HEADERS["Authorization"] = f"token {Tokens.github}"
-
-
-class GitHubFile(t.NamedTuple):
- """
- Represents a remote file on GitHub.
-
- The `sha` hash is kept so that we can determine that a file has changed,
- despite its filename remaining unchanged.
- """
-
- download_url: str
- path: str
- sha: str
-
-
-def pretty_files(files: t.Iterable[GitHubFile]) -> str:
- """Provide a human-friendly representation of `files`."""
- return "\n".join(file.path for file in files)
-
-
-def time_until_midnight() -> timedelta:
- """
- Determine amount of time until the next-up UTC midnight.
-
- The exact `midnight` moment is actually delayed to 5 seconds after, in order
- to avoid potential problems due to imprecise sleep.
- """
- now = datetime.utcnow()
- tomorrow = now + timedelta(days=1)
- midnight = datetime.combine(tomorrow, time(second=5))
-
- return midnight - now
-
-
-class BrandingManager(commands.Cog):
- """
- Manages the guild's branding.
-
- The purpose of this cog is to help automate the synchronization of the branding
- repository with the guild. It is capable of discovering assets in the repository
- via GitHub's API, resolving download urls for them, and delegating
- to the `bot` instance to upload them to the guild.
-
- BrandingManager is designed to be entirely autonomous. Its `daemon` background task awakens
- once a day (see `time_until_midnight`) to detect new seasons, or to cycle icons within a single
- season. The daemon can be turned on and off via the `daemon` cmd group. The value set via
- its `start` and `stop` commands is persisted across sessions. If turned on, the daemon will
- automatically start on the next bot start-up. Otherwise, it will wait to be started manually.
-
- All supported operations, e.g. setting seasons, applying the branding, or cycling icons, can
- also be invoked manually, via the following API:
-
- branding list
- - Show all available seasons
-
- branding set <season_name>
- - Set the cog's internal state to represent `season_name`, if it exists
- - If no `season_name` is given, set chronologically current season
- - This will not automatically apply the season's branding to the guild,
- the cog's state can be detached from the guild
- - Seasons can therefore be 'previewed' using this command
-
- branding info
- - View detailed information about resolved assets for current season
-
- branding refresh
- - Refresh internal state, i.e. synchronize with branding repository
-
- branding apply
- - Apply the current internal state to the guild, i.e. upload the assets
-
- branding cycle
- - If there are multiple available icons for current season, randomly pick
- and apply the next one
-
- The daemon calls these methods autonomously as appropriate. The use of this cog
- is locked to moderation roles. As it performs media asset uploads, it is prone to
- rate-limits - the `apply` command should be used with caution. The `set` command can,
- however, be used freely to 'preview' seasonal branding and check whether paths have been
- resolved as appropriate.
-
- While the bot is in debug mode, it will 'mock' asset uploads by logging the passed
- download urls and pretending that the upload was successful. Make use of this
- to test this cog's behaviour.
- """
-
- current_season: t.Type[SeasonBase]
-
- banner: t.Optional[GitHubFile]
- avatar: t.Optional[GitHubFile]
-
- available_icons: t.List[GitHubFile]
- remaining_icons: t.List[GitHubFile]
-
- days_since_cycle: t.Iterator
-
- config_file: Path
-
- daemon: t.Optional[asyncio.Task]
-
- def __init__(self, bot: SeasonalBot) -> None:
- """
- Assign safe default values on init.
-
- At this point, we don't have information about currently available branding.
- Most of these attributes will be overwritten once the daemon connects, or once
- the `refresh` command is used.
- """
- self.bot = bot
- self.current_season = get_current_season()
-
- self.banner = None
- self.avatar = None
-
- self.available_icons = []
- self.remaining_icons = []
-
- self.days_since_cycle = itertools.cycle([None])
-
- self.config_file = make_persistent(Path("bot", "resources", "evergreen", "branding.json"))
- should_run = self._read_config()["daemon_active"]
-
- if should_run:
- self.daemon = self.bot.loop.create_task(self._daemon_func())
- else:
- self.daemon = None
-
- @property
- def _daemon_running(self) -> bool:
- """True if the daemon is currently active, False otherwise."""
- return self.daemon is not None and not self.daemon.done()
-
- def _read_config(self) -> t.Dict[str, bool]:
- """Read and return persistent config file."""
- with self.config_file.open("r", encoding="utf8") as persistent_file:
- return json.load(persistent_file)
-
- def _write_config(self, key: str, value: bool) -> None:
- """Write a `key`, `value` pair to persistent config file."""
- current_config = self._read_config()
- current_config[key] = value
-
- with self.config_file.open("w", encoding="utf8") as persistent_file:
- json.dump(current_config, persistent_file)
-
- async def _daemon_func(self) -> None:
- """
- Manage all automated behaviour of the BrandingManager cog.
-
- Once a day, the daemon will perform the following tasks:
- - Update `current_season`
- - Poll GitHub API to see if the available branding for `current_season` has changed
- - Update assets if changes are detected (banner, guild icon, bot avatar, bot nickname)
- - Check whether it's time to cycle guild icons
-
- The internal loop runs once when activated, then periodically at the time
- given by `time_until_midnight`.
-
- All method calls in the internal loop are considered safe, i.e. no errors propagate
- to the daemon's loop. The daemon itself does not perform any error handling on its own.
- """
- await self.bot.wait_until_guild_available()
-
- while True:
- self.current_season = get_current_season()
- branding_changed = await self.refresh()
-
- if branding_changed:
- await self.apply()
-
- elif next(self.days_since_cycle) == Branding.cycle_frequency:
- await self.cycle()
-
- until_midnight = time_until_midnight()
- await asyncio.sleep(until_midnight.total_seconds())
-
- async def _info_embed(self) -> discord.Embed:
- """Make an informative embed representing current season."""
- info_embed = discord.Embed(description=self.current_season.description, colour=self.current_season.colour)
-
- # If we're in a non-evergreen season, also show active months
- if self.current_season is not SeasonBase:
- title = f"{self.current_season.season_name} ({human_months(self.current_season.months)})"
- else:
- title = self.current_season.season_name
-
- # Use the author field to show the season's name and avatar if available
- info_embed.set_author(name=title, icon_url=self.avatar.download_url if self.avatar else EmptyEmbed)
-
- banner = self.banner.path if self.banner is not None else "Unavailable"
- info_embed.add_field(name="Banner", value=banner, inline=False)
-
- avatar = self.avatar.path if self.avatar is not None else "Unavailable"
- info_embed.add_field(name="Avatar", value=avatar, inline=False)
-
- icons = pretty_files(self.available_icons) or "Unavailable"
- info_embed.add_field(name="Available icons", value=icons, inline=False)
-
- # Only display cycle frequency if we're actually cycling
- if len(self.available_icons) > 1 and Branding.cycle_frequency:
- info_embed.set_footer(text=f"Icon cycle frequency: {Branding.cycle_frequency}")
-
- return info_embed
-
- async def _reset_remaining_icons(self) -> None:
- """Set `remaining_icons` to a shuffled copy of `available_icons`."""
- self.remaining_icons = random.sample(self.available_icons, k=len(self.available_icons))
-
- async def _reset_days_since_cycle(self) -> None:
- """
- Reset the `days_since_cycle` iterator based on configured frequency.
-
- If the current season only has 1 icon, or if `Branding.cycle_frequency` is falsey,
- the iterator will always yield None. This signals that the icon shouldn't be cycled.
-
- Otherwise, it will yield ints in range [1, `Branding.cycle_frequency`] indefinitely.
- When the iterator yields a value equal to `Branding.cycle_frequency`, it is time to cycle.
- """
- if len(self.available_icons) > 1 and Branding.cycle_frequency:
- sequence = range(1, Branding.cycle_frequency + 1)
- else:
- sequence = [None]
-
- self.days_since_cycle = itertools.cycle(sequence)
-
- async def _get_files(self, path: str, include_dirs: bool = False) -> t.Dict[str, GitHubFile]:
- """
- Get files at `path` in the branding repository.
-
- If `include_dirs` is False (default), only returns files at `path`.
- Otherwise, will return both files and directories. Never returns symlinks.
-
- Return dict mapping from filename to corresponding `GitHubFile` instance.
- This may return an empty dict if the response status is non-200,
- or if the target directory is empty.
- """
- url = f"{BRANDING_URL}/{path}"
- async with self.bot.http_session.get(url, headers=HEADERS, params=PARAMS) as resp:
- # Short-circuit if we get non-200 response
- if resp.status != STATUS_OK:
- log.error(f"GitHub API returned non-200 response: {resp}")
- return {}
- directory = await resp.json() # Directory at `path`
-
- allowed_types = {"file", "dir"} if include_dirs else {"file"}
- return {
- file["name"]: GitHubFile(file["download_url"], file["path"], file["sha"])
- for file in directory
- if file["type"] in allowed_types
- }
-
- async def refresh(self) -> bool:
- """
- Synchronize available assets with branding repository.
-
- If the current season is not the evergreen, and lacks at least one asset,
- we use the evergreen seasonal dir as fallback for missing assets.
-
- Finally, if neither the seasonal nor fallback branding directories contain
- an asset, it will simply be ignored.
-
- Return True if the branding has changed. This will be the case when we enter
- a new season, or when something changes in the current seasons's directory
- in the branding repository.
- """
- old_branding = (self.banner, self.avatar, self.available_icons)
- seasonal_dir = await self._get_files(self.current_season.branding_path, include_dirs=True)
-
- # Only make a call to the fallback directory if there is something to be gained
- branding_incomplete = any(
- asset not in seasonal_dir
- for asset in (FILE_BANNER, FILE_AVATAR, SERVER_ICONS)
- )
- if branding_incomplete and self.current_season is not SeasonBase:
- fallback_dir = await self._get_files(SeasonBase.branding_path, include_dirs=True)
- else:
- fallback_dir = {}
-
- # Resolve assets in this directory, None is a safe value
- self.banner = seasonal_dir.get(FILE_BANNER) or fallback_dir.get(FILE_BANNER)
- self.avatar = seasonal_dir.get(FILE_AVATAR) or fallback_dir.get(FILE_AVATAR)
-
- # Now resolve server icons by making a call to the proper sub-directory
- if SERVER_ICONS in seasonal_dir:
- icons_dir = await self._get_files(f"{self.current_season.branding_path}/{SERVER_ICONS}")
- self.available_icons = list(icons_dir.values())
-
- elif SERVER_ICONS in fallback_dir:
- icons_dir = await self._get_files(f"{SeasonBase.branding_path}/{SERVER_ICONS}")
- self.available_icons = list(icons_dir.values())
-
- else:
- self.available_icons = [] # This should never be the case, but an empty list is a safe value
-
- # GitHubFile instances carry a `sha` attr so this will pick up if a file changes
- branding_changed = old_branding != (self.banner, self.avatar, self.available_icons)
-
- if branding_changed:
- log.info(f"New branding detected (season: {self.current_season.season_name})")
- await self._reset_remaining_icons()
- await self._reset_days_since_cycle()
-
- return branding_changed
-
- async def cycle(self) -> bool:
- """
- Apply the next-up server icon.
-
- Returns True if an icon is available and successfully gets applied, False otherwise.
- """
- if not self.available_icons:
- log.info("Cannot cycle: no icons for this season")
- return False
-
- if not self.remaining_icons:
- log.info("Reset & shuffle remaining icons")
- await self._reset_remaining_icons()
-
- next_up = self.remaining_icons.pop(0)
- success = await self.bot.set_icon(next_up.download_url)
-
- return success
-
- async def apply(self) -> t.List[str]:
- """
- Apply current branding to the guild and bot.
-
- This delegates to the bot instance to do all the work. We only provide download urls
- for available assets. Assets unavailable in the branding repo will be ignored.
-
- Returns a list of names of all failed assets. An asset is considered failed
- if it isn't found in the branding repo, or if something goes wrong while the
- bot is trying to apply it.
-
- An empty list denotes that all assets have been applied successfully.
- """
- report = {asset: False for asset in ("banner", "avatar", "nickname", "icon")}
-
- if self.banner is not None:
- report["banner"] = await self.bot.set_banner(self.banner.download_url)
-
- if self.avatar is not None:
- report["avatar"] = await self.bot.set_avatar(self.avatar.download_url)
-
- if self.current_season.bot_name:
- report["nickname"] = await self.bot.set_nickname(self.current_season.bot_name)
-
- report["icon"] = await self.cycle()
-
- failed_assets = [asset for asset, succeeded in report.items() if not succeeded]
- return failed_assets
-
- @with_role(*MODERATION_ROLES)
- @commands.group(name="branding")
- async def branding_cmds(self, ctx: commands.Context) -> None:
- """Manual branding control."""
- if not ctx.invoked_subcommand:
- await ctx.send_help(ctx.command)
-
- @branding_cmds.command(name="list", aliases=["ls"])
- async def branding_list(self, ctx: commands.Context) -> None:
- """List all available seasons and branding sources."""
- embed = discord.Embed(title="Available seasons", colour=Colours.soft_green)
-
- for season in get_all_seasons():
- if season is SeasonBase:
- active_when = "always"
- else:
- active_when = f"in {human_months(season.months)}"
-
- description = (
- f"Active {active_when}\n"
- f"Branding: {season.branding_path}"
- )
- embed.add_field(name=season.season_name, value=description, inline=False)
-
- await ctx.send(embed=embed)
-
- @branding_cmds.command(name="set")
- async def branding_set(self, ctx: commands.Context, *, season_name: t.Optional[str] = None) -> None:
- """
- Manually set season, or reset to current if none given.
-
- Season search is a case-less comparison against both seasonal class name,
- and its `season_name` attr.
-
- This only pre-loads the cog's internal state to the chosen season, but does not
- automatically apply the branding. As that is an expensive operation, the `apply`
- command must be called explicitly after this command finishes.
-
- This means that this command can be used to 'preview' a season gathering info
- about its available assets, without applying them to the guild.
-
- If the daemon is running, it will automatically reset the season to current when
- it wakes up. The season set via this command can therefore remain 'detached' from
- what it should be - the daemon will make sure that it's set back properly.
- """
- if season_name is None:
- new_season = get_current_season()
- else:
- new_season = get_season(season_name)
- if new_season is None:
- raise BrandingError("No such season exists")
-
- if self.current_season is new_season:
- raise BrandingError(f"Season {self.current_season.season_name} already active")
-
- self.current_season = new_season
- await self.branding_refresh(ctx)
-
- @branding_cmds.command(name="info", aliases=["status"])
- async def branding_info(self, ctx: commands.Context) -> None:
- """
- Show available assets for current season.
-
- This can be used to confirm that assets have been resolved properly.
- When `apply` is used, it attempts to upload exactly the assets listed here.
- """
- await ctx.send(embed=await self._info_embed())
-
- @branding_cmds.command(name="refresh")
- async def branding_refresh(self, ctx: commands.Context) -> None:
- """Sync currently available assets with branding repository."""
- async with ctx.typing():
- await self.refresh()
- await self.branding_info(ctx)
-
- @branding_cmds.command(name="apply")
- async def branding_apply(self, ctx: commands.Context) -> None:
- """
- Apply current season's branding to the guild.
-
- Use `info` to check which assets will be applied. Shows which assets have
- failed to be applied, if any.
- """
- async with ctx.typing():
- failed_assets = await self.apply()
- if failed_assets:
- raise BrandingError(f"Failed to apply following assets: {', '.join(failed_assets)}")
-
- response = discord.Embed(description=f"All assets applied {Emojis.ok_hand}", colour=Colours.soft_green)
- await ctx.send(embed=response)
-
- @branding_cmds.command(name="cycle")
- async def branding_cycle(self, ctx: commands.Context) -> None:
- """
- Apply the next-up guild icon, if multiple are available.
-
- The order is random.
- """
- async with ctx.typing():
- success = await self.cycle()
- if not success:
- raise BrandingError("Failed to cycle icon")
-
- response = discord.Embed(description=f"Success {Emojis.ok_hand}", colour=Colours.soft_green)
- await ctx.send(embed=response)
-
- @branding_cmds.group(name="daemon", aliases=["d", "task"])
- async def daemon_group(self, ctx: commands.Context) -> None:
- """Control the background daemon."""
- if not ctx.invoked_subcommand:
- await ctx.send_help(ctx.command)
-
- @daemon_group.command(name="status")
- async def daemon_status(self, ctx: commands.Context) -> None:
- """Check whether daemon is currently active."""
- if self._daemon_running:
- remaining_time = (arrow.utcnow() + time_until_midnight()).humanize()
- response = discord.Embed(description=f"Daemon running {Emojis.ok_hand}", colour=Colours.soft_green)
- response.set_footer(text=f"Next refresh {remaining_time}")
- else:
- response = discord.Embed(description="Daemon not running", colour=Colours.soft_red)
-
- await ctx.send(embed=response)
-
- @daemon_group.command(name="start")
- async def daemon_start(self, ctx: commands.Context) -> None:
- """If the daemon isn't running, start it."""
- if self._daemon_running:
- raise BrandingError("Daemon already running!")
-
- self.daemon = self.bot.loop.create_task(self._daemon_func())
- self._write_config("daemon_active", True)
-
- response = discord.Embed(description=f"Daemon started {Emojis.ok_hand}", colour=Colours.soft_green)
- await ctx.send(embed=response)
-
- @daemon_group.command(name="stop")
- async def daemon_stop(self, ctx: commands.Context) -> None:
- """If the daemon is running, stop it."""
- if not self._daemon_running:
- raise BrandingError("Daemon not running!")
-
- self.daemon.cancel()
- self._write_config("daemon_active", False)
-
- response = discord.Embed(description=f"Daemon stopped {Emojis.ok_hand}", colour=Colours.soft_green)
- await ctx.send(embed=response)
-
-
-def setup(bot: SeasonalBot) -> None:
- """Load BrandingManager cog."""
- bot.add_cog(BrandingManager(bot))
diff --git a/bot/exts/evergreen/emoji_count.py b/bot/exts/evergreen/emoji_count.py
index ef900199..cc43e9ab 100644
--- a/bot/exts/evergreen/emoji_count.py
+++ b/bot/exts/evergreen/emoji_count.py
@@ -1,12 +1,14 @@
import datetime
import logging
import random
-from typing import Dict, Optional
+from collections import defaultdict
+from typing import List, Tuple
import discord
from discord.ext import commands
from bot.constants import Colours, ERROR_REPLIES
+from bot.utils.pagination import LinePaginator
log = logging.getLogger(__name__)
@@ -17,73 +19,77 @@ class EmojiCount(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
- def embed_builder(self, emoji: dict) -> discord.Embed:
+ @staticmethod
+ def embed_builder(emoji: dict) -> Tuple[discord.Embed, List[str]]:
"""Generates an embed with the emoji names and count."""
embed = discord.Embed(
color=Colours.orange,
title="Emoji Count",
timestamp=datetime.datetime.utcnow()
)
+ msg = []
if len(emoji) == 1:
- for key, value in emoji.items():
- embed.description = f"There are **{len(value)}** emojis in the **{key}** category"
- embed.set_thumbnail(url=random.choice(value).url)
+ for category_name, category_emojis in emoji.items():
+ if len(category_emojis) == 1:
+ msg.append(f"There is **{len(category_emojis)}** emoji in **{category_name}** category")
+ else:
+ msg.append(f"There are **{len(category_emojis)}** emojis in **{category_name}** category")
+ embed.set_thumbnail(url=random.choice(category_emojis).url)
+
else:
- msg = ''
- for key, value in emoji.items():
- emoji_choice = random.choice(value)
- emoji_info = f'There are **{len(value)}** emojis in the **{key}** category\n'
- msg += f'<:{emoji_choice.name}:{emoji_choice.id}> {emoji_info}'
- embed.description = msg
- return embed
+ for category_name, category_emojis in emoji.items():
+ emoji_choice = random.choice(category_emojis)
+ if len(category_emojis) > 1:
+ emoji_info = f"There are **{len(category_emojis)}** emojis in **{category_name}** category"
+ else:
+ emoji_info = f"There is **{len(category_emojis)}** emoji in **{category_name}** category"
+ if emoji_choice.animated:
+ msg.append(f'<a:{emoji_choice.name}:{emoji_choice.id}> {emoji_info}')
+ else:
+ msg.append(f'<:{emoji_choice.name}:{emoji_choice.id}> {emoji_info}')
+ return embed, msg
@staticmethod
- def generate_invalid_embed(ctx: commands.Context) -> discord.Embed:
- """Genrates error embed."""
+ def generate_invalid_embed(emojis: list) -> Tuple[discord.Embed, List[str]]:
+ """Generates error embed."""
embed = discord.Embed(
color=Colours.soft_red,
title=random.choice(ERROR_REPLIES)
)
+ msg = []
- emoji_dict = {}
- for emoji in ctx.guild.emojis:
- emoji_dict[emoji.name.split("_")[0]] = []
+ emoji_dict = defaultdict(list)
+ for emoji in emojis:
+ emoji_dict[emoji.name.split("_")[0]].append(emoji)
- error_comp = ', '.join(key for key in emoji_dict.keys())
- embed.description = f"These are the valid categories\n```{error_comp}```"
- return embed
+ error_comp = ', '.join(emoji_dict)
+ msg.append(f"These are the valid categories\n```{error_comp}```")
+ return embed, msg
- def emoji_list(self, ctx: commands.Context, categories: dict) -> Dict:
- """Generates an embed with the emoji names and count."""
- out = {category: [] for category in categories}
+ @commands.command(name="emojicount", aliases=["ec", "emojis"])
+ async def emoji_count(self, ctx: commands.Context, *, category_query: str = None) -> None:
+ """Returns embed with emoji category and info given by the user."""
+ emoji_dict = defaultdict(list)
+ if not ctx.guild.emojis:
+ await ctx.send("No emojis found.")
+ return
+ log.trace(f"Emoji Category {'' if category_query else 'not '}provided by the user")
for emoji in ctx.guild.emojis:
- category = emoji.name.split('_')[0]
- if category in out:
- out[category].append(emoji)
- return out
-
- @commands.command(name="emoji_count", aliases=["ec"])
- async def ec(self, ctx: commands.Context, *, emoji: str = None) -> Optional[str]:
- """Returns embed with emoji category and info given by the user."""
- emoji_dict = {}
+ emoji_category = emoji.name.split("_")[0]
- for a in ctx.guild.emojis:
- if emoji is None:
- log.trace("Emoji Category not provided by the user")
- emoji_dict.update({a.name.split("_")[0]: []})
- elif a.name.split("_")[0] in emoji:
- log.trace("Emoji Category provided by the user")
- emoji_dict.update({a.name.split("_")[0]: []})
+ if category_query is not None and emoji_category not in category_query:
+ continue
- emoji_dict = self.emoji_list(ctx, emoji_dict)
+ emoji_dict[emoji_category].append(emoji)
- if len(emoji_dict) == 0:
- embed = self.generate_invalid_embed(ctx)
+ if not emoji_dict:
+ log.trace("Invalid name provided by the user")
+ embed, msg = self.generate_invalid_embed(ctx.guild.emojis)
else:
- embed = self.embed_builder(emoji_dict)
- await ctx.send(embed=embed)
+ embed, msg = self.embed_builder(emoji_dict)
+ await LinePaginator.paginate(lines=msg, ctx=ctx, embed=embed)
def setup(bot: commands.Bot) -> None:
diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py
index 459a2b2d..6e518435 100644
--- a/bot/exts/evergreen/error_handler.py
+++ b/bot/exts/evergreen/error_handler.py
@@ -9,7 +9,7 @@ from sentry_sdk import push_scope
from bot.constants import Colours, ERROR_REPLIES, NEGATIVE_REPLIES
from bot.utils.decorators import InChannelCheckFailure, InMonthCheckFailure
-from bot.utils.exceptions import BrandingError, UserNotPlayingError
+from bot.utils.exceptions import UserNotPlayingError
log = logging.getLogger(__name__)
@@ -57,10 +57,6 @@ class CommandErrorHandler(commands.Cog):
if isinstance(error, commands.CommandNotFound):
return
- if isinstance(error, BrandingError):
- await ctx.send(embed=self.error_embed(str(error)))
- return
-
if isinstance(error, (InChannelCheckFailure, InMonthCheckFailure)):
await ctx.send(embed=self.error_embed(str(error), NEGATIVE_REPLIES), delete_after=7.5)
return
diff --git a/bot/exts/evergreen/game.py b/bot/exts/evergreen/game.py
index 3c8b2725..d0fd7a40 100644
--- a/bot/exts/evergreen/game.py
+++ b/bot/exts/evergreen/game.py
@@ -11,7 +11,7 @@ from discord import Embed
from discord.ext import tasks
from discord.ext.commands import Cog, Context, group
-from bot.bot import SeasonalBot
+from bot.bot import Bot
from bot.constants import STAFF_ROLES, Tokens
from bot.utils.decorators import with_role
from bot.utils.pagination import ImagePaginator, LinePaginator
@@ -130,7 +130,7 @@ class AgeRatings(IntEnum):
class Games(Cog):
"""Games Cog contains commands that collect data from IGDB."""
- def __init__(self, bot: SeasonalBot):
+ def __init__(self, bot: Bot):
self.bot = bot
self.http_session: ClientSession = bot.http_session
@@ -415,7 +415,7 @@ class Games(Cog):
return sorted((item for item in results if item[0] >= 0.60), reverse=True)[:4]
-def setup(bot: SeasonalBot) -> None:
+def setup(bot: Bot) -> None:
"""Add/Load Games cog."""
# Check does IGDB API key exist, if not, log warning and don't load cog
if not Tokens.igdb:
diff --git a/bot/exts/evergreen/help.py b/bot/exts/evergreen/help.py
index ccd76d76..91147243 100644
--- a/bot/exts/evergreen/help.py
+++ b/bot/exts/evergreen/help.py
@@ -12,7 +12,7 @@ from discord.ext.commands import CheckFailure, Cog as DiscordCog, Command, Conte
from fuzzywuzzy import fuzz, process
from bot import constants
-from bot.bot import SeasonalBot
+from bot.bot import Bot
from bot.constants import Emojis
from bot.utils.pagination import (
FIRST_EMOJI, LAST_EMOJI,
@@ -511,7 +511,7 @@ class Help(DiscordCog):
await ctx.send(embed=embed)
-def unload(bot: SeasonalBot) -> None:
+def unload(bot: Bot) -> None:
"""
Reinstates the original help command.
@@ -521,7 +521,7 @@ def unload(bot: SeasonalBot) -> None:
bot.add_command(bot._old_help)
-def setup(bot: SeasonalBot) -> None:
+def setup(bot: Bot) -> None:
"""
The setup for the help extension.
@@ -542,7 +542,7 @@ def setup(bot: SeasonalBot) -> None:
raise
-def teardown(bot: SeasonalBot) -> None:
+def teardown(bot: Bot) -> None:
"""
The teardown for the help extension.
diff --git a/bot/exts/evergreen/issues.py b/bot/exts/evergreen/issues.py
index 97ee6a12..e419a6f5 100644
--- a/bot/exts/evergreen/issues.py
+++ b/bot/exts/evergreen/issues.py
@@ -33,7 +33,7 @@ class Issues(commands.Cog):
self,
ctx: commands.Context,
numbers: commands.Greedy[int],
- repository: str = "seasonalbot",
+ repository: str = "sir-lancebot",
user: str = "python-discord"
) -> None:
"""Command to retrieve issue(s) from a GitHub repository."""
diff --git a/bot/exts/evergreen/showprojects.py b/bot/exts/evergreen/showprojects.py
deleted file mode 100644
index 328a7aa5..00000000
--- a/bot/exts/evergreen/showprojects.py
+++ /dev/null
@@ -1,33 +0,0 @@
-import logging
-
-from discord import Message
-from discord.ext import commands
-
-from bot.constants import Channels
-
-log = logging.getLogger(__name__)
-
-
-class ShowProjects(commands.Cog):
- """Cog that reacts to posts in the #show-your-projects."""
-
- def __init__(self, bot: commands.Bot):
- self.bot = bot
- self.lastPoster = 0 # Given 0 as the default last poster ID as no user can actually have 0 assigned to them
-
- @commands.Cog.listener()
- async def on_message(self, message: Message) -> None:
- """Adds reactions to posts in #show-your-projects."""
- reactions = ["\U0001f44d", "\U00002764", "\U0001f440", "\U0001f389", "\U0001f680", "\U00002b50", "\U0001f6a9"]
- if (message.channel.id == Channels.show_your_projects
- and message.author.bot is False
- and message.author.id != self.lastPoster):
- for reaction in reactions:
- await message.add_reaction(reaction)
-
- self.lastPoster = message.author.id
-
-
-def setup(bot: commands.Bot) -> None:
- """Show Projects Reaction Cog."""
- bot.add_cog(ShowProjects(bot))
diff --git a/bot/exts/evergreen/source.py b/bot/exts/evergreen/source.py
index 0725714f..cdfe54ec 100644
--- a/bot/exts/evergreen/source.py
+++ b/bot/exts/evergreen/source.py
@@ -38,7 +38,7 @@ class BotSource(commands.Cog):
async def source_command(self, ctx: commands.Context, *, source_item: SourceConverter = None) -> None:
"""Display information and a GitHub link to the source code of a command, tag, or cog."""
if not source_item:
- embed = Embed(title="Seasonal Bot's GitHub Repository")
+ embed = Embed(title="Sir Lancebot's GitHub Repository")
embed.add_field(name="Repository", value=f"[Go to GitHub]({Source.github})")
embed.set_thumbnail(url=Source.github_avatar_url)
await ctx.send(embed=embed)
diff --git a/bot/exts/evergreen/space.py b/bot/exts/evergreen/space.py
index 3587fc00..bc8e3118 100644
--- a/bot/exts/evergreen/space.py
+++ b/bot/exts/evergreen/space.py
@@ -8,7 +8,7 @@ from discord import Embed
from discord.ext import tasks
from discord.ext.commands import BadArgument, Cog, Context, Converter, group
-from bot.bot import SeasonalBot
+from bot.bot import Bot
from bot.constants import Tokens
logger = logging.getLogger(__name__)
@@ -37,7 +37,7 @@ class DateConverter(Converter):
class Space(Cog):
"""Space Cog contains commands, that show images, facts or other information about space."""
- def __init__(self, bot: SeasonalBot):
+ def __init__(self, bot: Bot):
self.bot = bot
self.http_session = bot.http_session
@@ -240,7 +240,7 @@ class Space(Cog):
).set_image(url=image).set_footer(text="Powered by NASA API" + footer)
-def setup(bot: SeasonalBot) -> None:
+def setup(bot: Bot) -> None:
"""Load Space Cog."""
if not Tokens.nasa:
logger.warning("Can't find NASA API key. Not loading Space Cog.")
diff --git a/bot/exts/evergreen/wonder_twins.py b/bot/exts/evergreen/wonder_twins.py
new file mode 100644
index 00000000..afc5346e
--- /dev/null
+++ b/bot/exts/evergreen/wonder_twins.py
@@ -0,0 +1,49 @@
+import random
+from pathlib import Path
+
+import yaml
+from discord.ext.commands import Bot, Cog, Context, command
+
+
+class WonderTwins(Cog):
+ """Cog for a Wonder Twins inspired command."""
+
+ def __init__(self, bot: Bot):
+ self.bot = bot
+
+ with open(Path.cwd() / "bot" / "resources" / "evergreen" / "wonder_twins.yaml", "r", encoding="utf-8") as f:
+ info = yaml.load(f, Loader=yaml.FullLoader)
+ self.water_types = info["water_types"]
+ self.objects = info["objects"]
+ self.adjectives = info["adjectives"]
+
+ @staticmethod
+ def append_onto(phrase: str, insert_word: str) -> str:
+ """Appends one word onto the end of another phrase in order to format with the proper determiner."""
+ if insert_word.endswith("s"):
+ phrase = phrase.split()
+ del phrase[0]
+ phrase = " ".join(phrase)
+
+ insert_word = insert_word.split()[-1]
+ return " ".join([phrase, insert_word])
+
+ def format_phrase(self) -> str:
+ """Creates a transformation phrase from available words."""
+ adjective = random.choice((None, random.choice(self.adjectives)))
+ object_name = random.choice(self.objects)
+ water_type = random.choice(self.water_types)
+
+ if adjective:
+ object_name = self.append_onto(adjective, object_name)
+ return f"{object_name} of {water_type}"
+
+ @command(name="formof", aliases=["wondertwins", "wondertwin", "fo"])
+ async def form_of(self, ctx: Context) -> None:
+ """Command to send a Wonder Twins inspired phrase to the user invoking the command."""
+ await ctx.send(f"Form of {self.format_phrase()}!")
+
+
+def setup(bot: Bot) -> None:
+ """Load the WonderTwins cog."""
+ bot.add_cog(WonderTwins(bot))
diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py
index caf0df11..0cb37ecd 100644
--- a/bot/exts/halloween/candy_collection.py
+++ b/bot/exts/halloween/candy_collection.py
@@ -1,11 +1,9 @@
-import functools
-import json
import logging
-import os
import random
-from typing import List, Union
+from typing import Union
import discord
+from async_rediscache import RedisCache
from discord.ext import commands
from bot.constants import Channels, Month
@@ -13,27 +11,37 @@ from bot.utils.decorators import in_month
log = logging.getLogger(__name__)
-json_location = os.path.join("bot", "resources", "halloween", "candy_collection.json")
-
# chance is 1 in x range, so 1 in 20 range would give 5% chance (for add candy)
ADD_CANDY_REACTION_CHANCE = 20 # 5%
ADD_CANDY_EXISTING_REACTION_CHANCE = 10 # 10%
ADD_SKULL_REACTION_CHANCE = 50 # 2%
ADD_SKULL_EXISTING_REACTION_CHANCE = 20 # 5%
+EMOJIS = dict(
+ CANDY="\N{CANDY}",
+ SKULL="\N{SKULL}",
+ MEDALS=(
+ '\N{FIRST PLACE MEDAL}',
+ '\N{SECOND PLACE MEDAL}',
+ '\N{THIRD PLACE MEDAL}',
+ '\N{SPORTS MEDAL}',
+ '\N{SPORTS MEDAL}',
+ ),
+)
+
class CandyCollection(commands.Cog):
"""Candy collection game Cog."""
+ # User candy amount records
+ candy_records = RedisCache()
+
+ # Candy and skull messages mapping
+ candy_messages = RedisCache()
+ skull_messages = RedisCache()
+
def __init__(self, bot: commands.Bot):
self.bot = bot
- with open(json_location, encoding="utf8") as candy:
- self.candy_json = json.load(candy)
- self.msg_reacted = self.candy_json['msg_reacted']
- self.get_candyinfo = dict()
- for userinfo in self.candy_json['records']:
- userid = userinfo['userid']
- self.get_candyinfo[userid] = userinfo
@in_month(Month.OCTOBER)
@commands.Cog.listener()
@@ -43,19 +51,17 @@ class CandyCollection(commands.Cog):
if message.author.bot:
return
# ensure it's hacktober channel
- if message.channel.id != Channels.seasonalbot_commands:
+ if message.channel.id != Channels.community_bot_commands:
return
# do random check for skull first as it has the lower chance
if random.randint(1, ADD_SKULL_REACTION_CHANCE) == 1:
- d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False}
- self.msg_reacted.append(d)
- return await message.add_reaction('\N{SKULL}')
+ await self.skull_messages.set(message.id, "skull")
+ return await message.add_reaction(EMOJIS['SKULL'])
# check for the candy chance next
if random.randint(1, ADD_CANDY_REACTION_CHANCE) == 1:
- d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False}
- self.msg_reacted.append(d)
- return await message.add_reaction('\N{CANDY}')
+ await self.candy_messages.set(message.id, "candy")
+ return await message.add_reaction(EMOJIS['CANDY'])
@in_month(Month.OCTOBER)
@commands.Cog.listener()
@@ -67,43 +73,44 @@ class CandyCollection(commands.Cog):
return
# check to ensure it is in correct channel
- if message.channel.id != Channels.seasonalbot_commands:
+ if message.channel.id != Channels.community_bot_commands:
return
# 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 ('\N{SKULL}', '\N{CANDY}'):
- if message.id in await self.ten_recent_msg():
+ 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()
+ )
+ if message.id in recent_message_ids:
await self.reacted_msg_chance(message)
return
- for react in self.msg_reacted:
- # check to see if the message id of a message we added a
- # reaction to is in json file, and if nobody has won/claimed it yet
- if react['msg_id'] == message.id and react['won'] is False:
- react['user_reacted'] = user.id
- react['won'] = True
- try:
- # if they have record/candies in json already it will do this
- user_records = self.get_candyinfo[user.id]
- if str(reaction.emoji) == '\N{CANDY}':
- user_records['record'] += 1
- if str(reaction.emoji) == '\N{SKULL}':
- if user_records['record'] <= 3:
- user_records['record'] = 0
- lost = 'all of your'
- else:
- lost = random.randint(1, 3)
- user_records['record'] -= lost
- await self.send_spook_msg(message.author, message.channel, lost)
-
- except KeyError:
- # otherwise it will raise KeyError so we need to add them to file
- if str(reaction.emoji) == '\N{CANDY}':
- print('ok')
- d = {"userid": user.id, "record": 1}
- self.candy_json['records'].append(d)
- await self.remove_reactions(reaction)
+ 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']:
+ await self.skull_messages.delete(message.id)
+
+ if prev_record := await self.candy_records.get(user.id):
+ lost = min(random.randint(1, 3), prev_record)
+ await self.candy_records.decrement(user.id, lost)
+
+ if lost == prev_record:
+ await CandyCollection.send_spook_msg(user, message.channel, 'all of your')
+ else:
+ await CandyCollection.send_spook_msg(user, message.channel, lost)
+ else:
+ await CandyCollection.send_no_candy_spook_message(user, message.channel)
+ else:
+ return # Skip saving
+
+ await reaction.clear()
async def reacted_msg_chance(self, message: discord.Message) -> None:
"""
@@ -113,109 +120,71 @@ class CandyCollection(commands.Cog):
existing reaction.
"""
if random.randint(1, ADD_SKULL_EXISTING_REACTION_CHANCE) == 1:
- d = {"reaction": '\N{SKULL}', "msg_id": message.id, "won": False}
- self.msg_reacted.append(d)
- return await message.add_reaction('\N{SKULL}')
+ await self.skull_messages.set(message.id, "skull")
+ return await message.add_reaction(EMOJIS['SKULL'])
if random.randint(1, ADD_CANDY_EXISTING_REACTION_CHANCE) == 1:
- d = {"reaction": '\N{CANDY}', "msg_id": message.id, "won": False}
- self.msg_reacted.append(d)
- return await message.add_reaction('\N{CANDY}')
-
- async def ten_recent_msg(self) -> List[int]:
- """Get the last 10 messages sent in the channel."""
- ten_recent = []
- recent_msg_id = max(
- message.id for message in self.bot._connection._messages
- if message.channel.id == Channels.seasonalbot_commands
- )
-
- channel = await self.hacktober_channel()
- ten_recent.append(recent_msg_id)
-
- for i in range(9):
- o = discord.Object(id=recent_msg_id + i)
- msg = await next(channel.history(limit=1, before=o))
- ten_recent.append(msg.id)
+ await self.candy_messages.set(message.id, "candy")
+ return await message.add_reaction(EMOJIS['CANDY'])
- return ten_recent
-
- async def get_message(self, msg_id: int) -> Union[discord.Message, None]:
- """Get the message from its ID."""
- try:
- o = discord.Object(id=msg_id + 1)
- # Use history rather than get_message due to
- # poor ratelimit (50/1s vs 1/1s)
- msg = await next(self.hacktober_channel.history(limit=1, before=o))
-
- if msg.id != msg_id:
- return None
-
- return msg
-
- except Exception:
- return None
-
- async def hacktober_channel(self) -> discord.TextChannel:
+ @property
+ def hacktober_channel(self) -> discord.TextChannel:
"""Get #hacktoberbot channel from its ID."""
- return self.bot.get_channel(id=Channels.seasonalbot_commands)
-
- async def remove_reactions(self, reaction: discord.Reaction) -> None:
- """Remove all candy/skull reactions."""
- try:
- async for user in reaction.users():
- await reaction.message.remove_reaction(reaction.emoji, user)
+ return self.bot.get_channel(id=Channels.community_bot_commands)
- except discord.HTTPException:
- pass
-
- async def send_spook_msg(self, author: discord.Member, channel: discord.TextChannel, candies: int) -> None:
+ @staticmethod
+ async def send_spook_msg(
+ author: discord.Member, channel: discord.TextChannel, candies: Union[str, int]
+ ) -> 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.")
await channel.send(embed=e)
- def save_to_json(self) -> None:
- """Save JSON to a local file."""
- with open(json_location, 'w', encoding="utf8") as outfile:
- json.dump(self.candy_json, outfile)
+ @staticmethod
+ async def send_no_candy_spook_message(
+ author: discord.Member,
+ channel: discord.TextChannel
+ ) -> None:
+ """An alternative spooky message sent when user has no candies in the collection."""
+ embed = discord.Embed(color=author.color)
+ embed.set_author(name="Ghosts and Ghouls and Jack o' lanterns at night; "
+ "I tried to take your candies but you had none to begin with!")
+ await channel.send(embed=embed)
@in_month(Month.OCTOBER)
@commands.command()
async def candy(self, ctx: commands.Context) -> None:
"""Get the candy leaderboard and save to JSON."""
- # Use run_in_executor to prevent blocking
- thing = functools.partial(self.save_to_json)
- await self.bot.loop.run_in_executor(None, thing)
-
- emoji = (
- '\N{FIRST PLACE MEDAL}',
- '\N{SECOND PLACE MEDAL}',
- '\N{THIRD PLACE MEDAL}',
- '\N{SPORTS MEDAL}',
- '\N{SPORTS MEDAL}'
- )
-
- top_sorted = sorted(self.candy_json['records'], key=lambda k: k.get('record', 0), reverse=True)
- top_five = top_sorted[:5]
+ records = await self.candy_records.items()
- usersid = []
- records = []
- for record in top_five:
- usersid.append(record['userid'])
- records.append(record['record'])
+ def generate_leaderboard() -> str:
+ top_sorted = sorted(
+ ((user_id, score) for user_id, score in records if score > 0),
+ key=lambda x: x[1],
+ reverse=True
+ )
+ top_five = top_sorted[:5]
- value = '\n'.join(f'{emoji[index]} <@{usersid[index]}>: {records[index]}'
- for index in range(0, len(usersid))) or 'No Candies'
+ return '\n'.join(
+ f"{EMOJIS['MEDALS'][index]} <@{record[0]}>: {record[1]}"
+ for index, record in enumerate(top_five)
+ ) if top_five else 'No Candies'
e = discord.Embed(colour=discord.Colour.blurple())
- e.add_field(name="Top Candy Records", value=value, inline=False)
- e.add_field(name='\u200b',
- value="Candies will randomly appear on messages sent. "
- "\nHit the candy when it appears as fast as possible to get the candy! "
- "\nBut beware the ghosts...",
- inline=False)
+ e.add_field(
+ name="Top Candy Records",
+ value=generate_leaderboard(),
+ inline=False
+ )
+ e.add_field(
+ name='\u200b',
+ value="Candies will randomly appear on messages sent. "
+ "\nHit the candy when it appears as fast as possible to get the candy! "
+ "\nBut beware the ghosts...",
+ inline=False
+ )
await ctx.send(embed=e)
diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py
index 701ef0b3..ae949f53 100644
--- a/bot/exts/halloween/hacktoberstats.py
+++ b/bot/exts/halloween/hacktoberstats.py
@@ -1,4 +1,3 @@
-import json
import logging
import random
import re
@@ -9,11 +8,11 @@ from typing import List, Optional, Tuple, Union
import aiohttp
import discord
+from async_rediscache import RedisCache
from discord.ext import commands
from bot.constants import Channels, Month, NEGATIVE_REPLIES, Tokens, WHITELISTED_CHANNELS
from bot.utils.decorators import in_month, override_in_channel
-from bot.utils.persist import make_persistent
log = logging.getLogger(__name__)
@@ -38,10 +37,11 @@ GITHUB_NONEXISTENT_USER_MESSAGE = (
class HacktoberStats(commands.Cog):
"""Hacktoberfest statistics Cog."""
+ # Stores mapping of user IDs and GitHub usernames
+ linked_accounts = RedisCache()
+
def __init__(self, bot: commands.Bot):
self.bot = bot
- self.link_json = make_persistent(Path("bot", "resources", "halloween", "github_links.json"))
- self.linked_accounts = self.load_linked_users()
@in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER)
@commands.group(name="hacktoberstats", aliases=("hackstats",), invoke_without_command=True)
@@ -57,8 +57,8 @@ class HacktoberStats(commands.Cog):
if not github_username:
author_id, author_mention = self._author_mention_from_context(ctx)
- if str(author_id) in self.linked_accounts.keys():
- github_username = self.linked_accounts[author_id]["github_username"]
+ if await self.linked_accounts.contains(author_id):
+ github_username = await self.linked_accounts.get(author_id)
logging.info(f"Getting stats for {author_id} linked GitHub account '{github_username}'")
else:
msg = (
@@ -78,30 +78,19 @@ class HacktoberStats(commands.Cog):
"""
Link the invoking user's Github github_username to their Discord ID.
- Linked users are stored as a nested dict:
- {
- Discord_ID: {
- "github_username": str
- "date_added": datetime
- }
- }
+ Linked users are stored in Redis: User ID => GitHub Username.
"""
author_id, author_mention = self._author_mention_from_context(ctx)
if github_username:
- if str(author_id) in self.linked_accounts.keys():
- old_username = self.linked_accounts[author_id]["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}'")
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}'")
await ctx.send(f"{author_mention}, your GitHub username has been added")
- self.linked_accounts[author_id] = {
- "github_username": github_username,
- "date_added": datetime.now()
- }
-
- self.save_linked_users()
+ 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")
await ctx.send(f"{author_mention}, a GitHub username is required to link your account")
@@ -113,7 +102,7 @@ class HacktoberStats(commands.Cog):
"""Remove the invoking user's account link from the log."""
author_id, author_mention = self._author_mention_from_context(ctx)
- stored_user = self.linked_accounts.pop(author_id, None)
+ stored_user = await self.linked_accounts.pop(author_id, None)
if stored_user:
await ctx.send(f"{author_mention}, your GitHub profile has been unlinked")
logging.info(f"{author_id} has unlinked their GitHub account")
@@ -121,48 +110,6 @@ class HacktoberStats(commands.Cog):
await ctx.send(f"{author_mention}, you do not currently have a linked GitHub account")
logging.info(f"{author_id} tried to unlink their GitHub account but no account was linked")
- self.save_linked_users()
-
- def load_linked_users(self) -> dict:
- """
- Load list of linked users from local JSON file.
-
- Linked users are stored as a nested dict:
- {
- Discord_ID: {
- "github_username": str
- "date_added": datetime
- }
- }
- """
- if self.link_json.exists():
- logging.info(f"Loading linked GitHub accounts from '{self.link_json}'")
- with open(self.link_json, 'r', encoding="utf8") as file:
- linked_accounts = json.load(file)
-
- logging.info(f"Loaded {len(linked_accounts)} linked GitHub accounts from '{self.link_json}'")
- return linked_accounts
- else:
- logging.info(f"Linked account log: '{self.link_json}' does not exist")
- return {}
-
- def save_linked_users(self) -> None:
- """
- Save list of linked users to local JSON file.
-
- Linked users are stored as a nested dict:
- {
- Discord_ID: {
- "github_username": str
- "date_added": datetime
- }
- }
- """
- logging.info(f"Saving linked_accounts to '{self.link_json}'")
- with open(self.link_json, 'w', encoding="utf8") as file:
- json.dump(self.linked_accounts, file, default=str)
- logging.info(f"linked_accounts saved to '{self.link_json}'")
-
async def get_stats(self, ctx: commands.Context, github_username: str) -> None:
"""
Query GitHub's API for PRs created by a GitHub user during the month of October.
@@ -254,7 +201,7 @@ class HacktoberStats(commands.Cog):
For each PR:
{
"repo_url": str
- "repo_shortname": str (e.g. "python-discord/seasonalbot")
+ "repo_shortname": str (e.g. "python-discord/sir-lancebot")
"created_at": datetime.datetime
"number": int
}
@@ -267,7 +214,7 @@ class HacktoberStats(commands.Cog):
action_type = "pr"
is_query = "public"
not_query = "draft"
- date_range = f"{CURRENT_YEAR}-10-01T00:00:00%2B14:00..{CURRENT_YEAR}-11-01T00:00:00-11:00"
+ date_range = f"{CURRENT_YEAR}-09-30T10:00Z..{CURRENT_YEAR}-11-01T12:00Z"
per_page = "300"
query_url = (
f"{base_url}"
@@ -300,7 +247,7 @@ class HacktoberStats(commands.Cog):
logging.info(f"Found {len(jsonresp['items'])} Hacktoberfest PRs for GitHub user: '{github_username}'")
outlist = [] # list of pr information dicts that will get returned
- oct3 = datetime(int(CURRENT_YEAR), 10, 3, 0, 0, 0)
+ 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"])
@@ -423,10 +370,10 @@ class HacktoberStats(commands.Cog):
"""
Extract shortname from https://api.github.com/repos/* URL.
- e.g. "https://api.github.com/repos/python-discord/seasonalbot"
+ e.g. "https://api.github.com/repos/python-discord/sir-lancebot"
|
V
- "python-discord/seasonalbot"
+ "python-discord/sir-lancebot"
"""
exp = r"https?:\/\/api.github.com\/repos\/([/\-\_\.\w]+)"
return re.findall(exp, in_url)[0]
@@ -443,12 +390,13 @@ class HacktoberStats(commands.Cog):
'hacktoberfest-accepted.
"""
now = datetime.now()
+ oct3 = datetime(CURRENT_YEAR, 10, 3, 23, 59, 59, tzinfo=None)
in_review = []
accepted = []
for pr in prs:
if (pr['created_at'] + timedelta(REVIEW_DAYS)) > now:
in_review.append(pr)
- elif await HacktoberStats._is_accepted(pr):
+ elif (pr['created_at'] <= oct3) or await HacktoberStats._is_accepted(pr):
accepted.append(pr)
return in_review, accepted
diff --git a/bot/exts/halloween/monstersurvey.py b/bot/exts/halloween/monstersurvey.py
index 7b1a1e84..80196825 100644
--- a/bot/exts/halloween/monstersurvey.py
+++ b/bot/exts/halloween/monstersurvey.py
@@ -202,4 +202,3 @@ class MonsterSurvey(Cog):
def setup(bot: Bot) -> None:
"""Monster survey Cog load."""
- bot.add_cog(MonsterSurvey(bot))
diff --git a/bot/exts/halloween/spookyreact.py b/bot/exts/halloween/spookyreact.py
index e5945aea..b335df75 100644
--- a/bot/exts/halloween/spookyreact.py
+++ b/bot/exts/halloween/spookyreact.py
@@ -29,13 +29,7 @@ class SpookyReact(Cog):
@in_month(Month.OCTOBER)
@Cog.listener()
async def on_message(self, ctx: discord.Message) -> None:
- """
- A command to send the seasonalbot github project.
-
- Lines that begin with the bot's command prefix are ignored
-
- Seasonalbot's own messages are ignored
- """
+ """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())
if trigger_test:
diff --git a/bot/exts/pride/pride_facts.py b/bot/exts/pride/pride_facts.py
index 9ff4c9e0..5bd5d0ce 100644
--- a/bot/exts/pride/pride_facts.py
+++ b/bot/exts/pride/pride_facts.py
@@ -9,7 +9,7 @@ import dateutil.parser
import discord
from discord.ext import commands
-from bot.bot import SeasonalBot
+from bot.bot import Bot
from bot.constants import Channels, Colours, Month
from bot.utils.decorators import seasonal_task
@@ -21,7 +21,7 @@ Sendable = Union[commands.Context, discord.TextChannel]
class PrideFacts(commands.Cog):
"""Provides a new fact every day during the Pride season!"""
- def __init__(self, bot: SeasonalBot):
+ def __init__(self, bot: Bot):
self.bot = bot
self.facts = self.load_facts()
@@ -38,7 +38,7 @@ class PrideFacts(commands.Cog):
"""Background task to post the daily pride fact every day."""
await self.bot.wait_until_guild_available()
- channel = self.bot.get_channel(Channels.seasonalbot_commands)
+ channel = self.bot.get_channel(Channels.community_bot_commands)
await self.send_select_fact(channel, datetime.utcnow())
async def send_random_fact(self, ctx: commands.Context) -> None:
@@ -102,6 +102,6 @@ class PrideFacts(commands.Cog):
)
-def setup(bot: SeasonalBot) -> None:
+def setup(bot: Bot) -> None:
"""Cog loader for pride facts."""
bot.add_cog(PrideFacts(bot))
diff --git a/bot/exts/utils/extensions.py b/bot/exts/utils/extensions.py
index 102a0416..bb22c353 100644
--- a/bot/exts/utils/extensions.py
+++ b/bot/exts/utils/extensions.py
@@ -8,7 +8,7 @@ from discord.ext import commands
from discord.ext.commands import Context, group
from bot import exts
-from bot.bot import SeasonalBot as Bot
+from bot.bot import Bot
from bot.constants import Client, Emojis, MODERATION_ROLES, Roles
from bot.utils.checks import with_role_check
from bot.utils.extensions import EXTENSIONS, unqualify
diff --git a/bot/exts/valentines/be_my_valentine.py b/bot/exts/valentines/be_my_valentine.py
index b1258307..4db4d191 100644
--- a/bot/exts/valentines/be_my_valentine.py
+++ b/bot/exts/valentines/be_my_valentine.py
@@ -99,7 +99,7 @@ class BeMyValentine(commands.Cog):
emoji_1, emoji_2 = self.random_emoji()
lovefest_role = discord.utils.get(ctx.guild.roles, id=Lovefest.role_id)
- channel = self.bot.get_channel(Channels.seasonalbot_commands)
+ channel = self.bot.get_channel(Channels.community_bot_commands)
valentine, title = self.valentine_check(valentine_type)
if user is None: