From 745cd1d6d3d6227d2a1e82cf25611d76221c40cd Mon Sep 17 00:00:00 2001 From: decorator-factory <42166884+decorator-factory@users.noreply.github.com> Date: Sat, 7 Aug 2021 05:23:03 +0300 Subject: Fix type annotations --- bot/exts/evergreen/fun.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot/exts/evergreen/fun.py') diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 3b266e1b..1783d7b4 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -2,8 +2,9 @@ import functools import json import logging import random +from collections.abc import Iterable from pathlib import Path -from typing import Callable, Iterable, Tuple, Union +from typing import Callable, Optional, Union from discord import Embed, Message from discord.ext import commands @@ -182,7 +183,7 @@ class Fun(Cog): await self._caesar_cipher(ctx, offset, msg, left_shift=True) @staticmethod - async def _get_text_and_embed(ctx: Context, text: str) -> Tuple[str, Union[Embed, None]]: + async def _get_text_and_embed(ctx: Context, text: str) -> tuple[str, Optional[Embed]]: """ Attempts to extract the text and embed from a possible link to a discord Message. -- cgit v1.2.3 From 6683af4399330218892b0235fcc9f3dbd2288c44 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Wed, 1 Sep 2021 11:53:27 +0100 Subject: Use permissions_for over permissions_in `discord.Member.permissions_in()` was removed in d.py 2.0 in favour of using `discord.Channel.permissions_for()` everywhere. --- bot/exts/evergreen/bookmark.py | 2 +- bot/exts/evergreen/fun.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'bot/exts/evergreen/fun.py') diff --git a/bot/exts/evergreen/bookmark.py b/bot/exts/evergreen/bookmark.py index 98b0f2bf..c0619e86 100644 --- a/bot/exts/evergreen/bookmark.py +++ b/bot/exts/evergreen/bookmark.py @@ -103,7 +103,7 @@ class Bookmark(commands.Cog): target_message = ctx.message.reference.resolved # Prevent users from bookmarking a message in a channel they don't have access to - permissions = ctx.author.permissions_in(target_message.channel) + permissions = target_message.channel.permissions_for(ctx.author) if not permissions.read_messages: log.info(f"{ctx.author} tried to bookmark a message in #{target_message.channel} but has no permissions.") embed = discord.Embed( diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 3b266e1b..63450025 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -197,11 +197,13 @@ class Fun(Cog): msg = await Fun._get_discord_message(ctx, text) # Ensure the user has read permissions for the channel the message is in - if isinstance(msg, Message) and ctx.author.permissions_in(msg.channel).read_messages: - text = msg.clean_content - # Take first embed because we can't send multiple embeds - if msg.embeds: - embed = msg.embeds[0] + if isinstance(msg, Message): + permissions = msg.channel.permissions_for(ctx.author) + if permissions.read_messages: + text = msg.clean_content + # Take first embed because we can't send multiple embeds + if msg.embeds: + embed = msg.embeds[0] return (text, embed) -- cgit v1.2.3 From f4e5e3850c9a030e0125a908dc5a8dbbcd6659dd Mon Sep 17 00:00:00 2001 From: Xithrius Date: Wed, 1 Sep 2021 18:32:55 -0700 Subject: Union item with None to Optional with item. --- bot/exts/easter/egg_decorating.py | 6 +++--- bot/exts/evergreen/fun.py | 2 +- bot/exts/halloween/spookynamerate.py | 4 ++-- bot/utils/decorators.py | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'bot/exts/evergreen/fun.py') diff --git a/bot/exts/easter/egg_decorating.py b/bot/exts/easter/egg_decorating.py index 6201d424..fb5701c4 100644 --- a/bot/exts/easter/egg_decorating.py +++ b/bot/exts/easter/egg_decorating.py @@ -4,7 +4,7 @@ import random from contextlib import suppress from io import BytesIO from pathlib import Path -from typing import Union +from typing import Optional, Union import discord from PIL import Image @@ -33,7 +33,7 @@ class EggDecorating(commands.Cog): """Decorate some easter eggs!""" @staticmethod - def replace_invalid(colour: str) -> Union[int, None]: + def replace_invalid(colour: str) -> Optional[int]: """Attempts to match with HTML or XKCD colour names, returning the int value.""" with suppress(KeyError): return int(HTML_COLOURS[colour], 16) @@ -44,7 +44,7 @@ class EggDecorating(commands.Cog): @commands.command(aliases=("decorateegg",)) async def eggdecorate( self, ctx: commands.Context, *colours: Union[discord.Colour, str] - ) -> Union[Image.Image, None]: + ) -> Optional[Image.Image]: """ Picks a random egg design and decorates it using the given colours. diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 1783d7b4..fd17a691 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -192,7 +192,7 @@ class Fun(Cog): Returns a tuple of: str: If `text` is a valid discord Message, the contents of the message, else `text`. - Union[Embed, None]: The embed if found in the valid Message, else None + Optional[Embed]: The embed if found in the valid Message, else None """ embed = None diff --git a/bot/exts/halloween/spookynamerate.py b/bot/exts/halloween/spookynamerate.py index 3d6d95fa..bab79e56 100644 --- a/bot/exts/halloween/spookynamerate.py +++ b/bot/exts/halloween/spookynamerate.py @@ -6,7 +6,7 @@ from datetime import datetime, timedelta from logging import getLogger from os import getenv from pathlib import Path -from typing import Union +from typing import Optional from async_rediscache import RedisCache from discord import Embed, Reaction, TextChannel, User @@ -355,7 +355,7 @@ class SpookyNameRate(Cog): return embed - async def get_channel(self) -> Union[TextChannel, None]: + async def get_channel(self) -> Optional[TextChannel]: """Gets the sir-lancebot-channel after waiting until ready.""" await self.bot.wait_until_ready() channel = self.bot.get_channel( diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 2612ff81..132aaa87 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -5,7 +5,7 @@ import random from asyncio import Lock from collections.abc import Container from functools import wraps -from typing import Callable, Union +from typing import Callable, Optional, Union from weakref import WeakValueDictionary from discord import Colour, Embed @@ -297,7 +297,7 @@ def whitelist_override(bypass_defaults: bool = False, **kwargs: Container[int]) return inner -def locked() -> Union[Callable, None]: +def locked() -> Optional[Callable]: """ Allows the user to only run one instance of the decorated command at a time. @@ -305,11 +305,11 @@ def locked() -> Union[Callable, None]: This decorator has to go before (below) the `command` decorator. """ - def wrap(func: Callable) -> Union[Callable, None]: + def wrap(func: Callable) -> Optional[Callable]: func.__locks = WeakValueDictionary() @wraps(func) - async def inner(self: Callable, ctx: Context, *args, **kwargs) -> Union[Callable, None]: + async def inner(self: Callable, ctx: Context, *args, **kwargs) -> Optional[Callable]: lock = func.__locks.setdefault(ctx.author.id, Lock()) if lock.locked(): embed = Embed() -- cgit v1.2.3 From aecc1e6f6f1df012add3ce394496b01e776bbfdf Mon Sep 17 00:00:00 2001 From: Xithrius Date: Wed, 1 Sep 2021 18:44:24 -0700 Subject: Removed None return annotation for any __init__ --- bot/exts/christmas/advent_of_code/_cog.py | 2 +- bot/exts/easter/egghead_quiz.py | 2 +- bot/exts/evergreen/avatar_modification/avatar_modify.py | 2 +- bot/exts/evergreen/battleship.py | 2 +- bot/exts/evergreen/connect_four.py | 4 ++-- bot/exts/evergreen/duck_game.py | 2 +- bot/exts/evergreen/error_handler.py | 2 +- bot/exts/evergreen/fun.py | 2 +- bot/exts/evergreen/minesweeper.py | 2 +- bot/exts/evergreen/recommend_game.py | 2 +- bot/exts/evergreen/trivia_quiz.py | 2 +- bot/exts/evergreen/xkcd.py | 2 +- bot/exts/halloween/spookynamerate.py | 2 +- bot/exts/internal_eval/_helpers.py | 6 +++--- bot/utils/checks.py | 2 +- bot/utils/randomization.py | 2 +- 16 files changed, 19 insertions(+), 19 deletions(-) (limited to 'bot/exts/evergreen/fun.py') diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 3d61753b..bc2ccc04 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -29,7 +29,7 @@ AOC_WHITELIST = AOC_WHITELIST_RESTRICTED + (Channels.advent_of_code,) class AdventOfCode(commands.Cog): """Advent of Code festivities! Ho Ho Ho!""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self._base_url = f"https://adventofcode.com/{AocConfig.year}" diff --git a/bot/exts/easter/egghead_quiz.py b/bot/exts/easter/egghead_quiz.py index 7c4960cd..ad550567 100644 --- a/bot/exts/easter/egghead_quiz.py +++ b/bot/exts/easter/egghead_quiz.py @@ -31,7 +31,7 @@ TIMELIMIT = 30 class EggheadQuiz(commands.Cog): """This cog contains the command for the Easter quiz!""" - def __init__(self) -> None: + def __init__(self): self.quiz_messages = {} @commands.command(aliases=("eggheadquiz", "easterquiz")) diff --git a/bot/exts/evergreen/avatar_modification/avatar_modify.py b/bot/exts/evergreen/avatar_modification/avatar_modify.py index 2be09be2..18202902 100644 --- a/bot/exts/evergreen/avatar_modification/avatar_modify.py +++ b/bot/exts/evergreen/avatar_modification/avatar_modify.py @@ -62,7 +62,7 @@ def file_safe_name(effect: str, display_name: str) -> str: class AvatarModify(commands.Cog): """Various commands for users to apply affects to their own avatars.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot async def _fetch_user(self, user_id: int) -> Optional[discord.User]: diff --git a/bot/exts/evergreen/battleship.py b/bot/exts/evergreen/battleship.py index 46694f6a..70d22a56 100644 --- a/bot/exts/evergreen/battleship.py +++ b/bot/exts/evergreen/battleship.py @@ -322,7 +322,7 @@ class Game: class Battleship(commands.Cog): """Play the classic game Battleship!""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.games: list[Game] = [] self.waiting: list[discord.Member] = [] diff --git a/bot/exts/evergreen/connect_four.py b/bot/exts/evergreen/connect_four.py index 0f83bf8a..d4d80154 100644 --- a/bot/exts/evergreen/connect_four.py +++ b/bot/exts/evergreen/connect_four.py @@ -181,7 +181,7 @@ class Game: class AI: """The Computer Player for Single-Player games.""" - def __init__(self, bot: Bot, game: Game) -> None: + def __init__(self, bot: Bot, game: Game): self.game = game self.mention = bot.user.mention @@ -256,7 +256,7 @@ class AI: class ConnectFour(commands.Cog): """Connect Four. The Classic Vertical Four-in-a-row Game!""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.games: list[Game] = [] self.waiting: list[discord.Member] = [] diff --git a/bot/exts/evergreen/duck_game.py b/bot/exts/evergreen/duck_game.py index 51e7a98a..614a234b 100644 --- a/bot/exts/evergreen/duck_game.py +++ b/bot/exts/evergreen/duck_game.py @@ -172,7 +172,7 @@ class DuckGame: class DuckGamesDirector(commands.Cog): """A cog for running Duck Duck Duck Goose games.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.current_games = {} diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index 3fa8be39..fa9caffb 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -23,7 +23,7 @@ QUESTION_MARK_ICON = "https://cdn.discordapp.com/emojis/512367613339369475.png" class CommandErrorHandler(commands.Cog): """A error handler for the PythonDiscord server.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot @staticmethod diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index fd17a691..40743e7b 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -54,7 +54,7 @@ def caesar_cipher(text: str, offset: int) -> Iterable[str]: class Fun(Cog): """A collection of general commands for fun.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self._caesar_cipher_embed = json.loads(Path("bot/resources/evergreen/caesar_info.json").read_text("UTF-8")) diff --git a/bot/exts/evergreen/minesweeper.py b/bot/exts/evergreen/minesweeper.py index 75d13d88..a48b5051 100644 --- a/bot/exts/evergreen/minesweeper.py +++ b/bot/exts/evergreen/minesweeper.py @@ -51,7 +51,7 @@ class Game: class Minesweeper(commands.Cog): """Play a game of Minesweeper.""" - def __init__(self) -> None: + def __init__(self): self.games: dict[int, Game] = {} @commands.group(name="minesweeper", aliases=("ms",), invoke_without_command=True) diff --git a/bot/exts/evergreen/recommend_game.py b/bot/exts/evergreen/recommend_game.py index 66597b59..bdd3acb1 100644 --- a/bot/exts/evergreen/recommend_game.py +++ b/bot/exts/evergreen/recommend_game.py @@ -21,7 +21,7 @@ shuffle(game_recs) class RecommendGame(commands.Cog): """Commands related to recommending games.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.index = 0 diff --git a/bot/exts/evergreen/trivia_quiz.py b/bot/exts/evergreen/trivia_quiz.py index d754cf02..aa4020d6 100644 --- a/bot/exts/evergreen/trivia_quiz.py +++ b/bot/exts/evergreen/trivia_quiz.py @@ -193,7 +193,7 @@ DYNAMIC_QUESTIONS_FORMAT_FUNCS = { class TriviaQuiz(commands.Cog): """A cog for all quiz commands.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.game_status = {} # A variable to store the game status: either running or not running. diff --git a/bot/exts/evergreen/xkcd.py b/bot/exts/evergreen/xkcd.py index 7c4fc30e..b56c53d9 100644 --- a/bot/exts/evergreen/xkcd.py +++ b/bot/exts/evergreen/xkcd.py @@ -19,7 +19,7 @@ BASE_URL = "https://xkcd.com" class XKCD(Cog): """Retrieving XKCD comics.""" - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.latest_comic_info: dict[str, Union[str, int]] = {} self.get_latest_comic_info.start() diff --git a/bot/exts/halloween/spookynamerate.py b/bot/exts/halloween/spookynamerate.py index bab79e56..5c21ead7 100644 --- a/bot/exts/halloween/spookynamerate.py +++ b/bot/exts/halloween/spookynamerate.py @@ -91,7 +91,7 @@ class SpookyNameRate(Cog): # will automatically start the scoring and announcing the result (without waiting for 12, so do not expect it to.). # Also, it won't wait for the two hours (when the poll closes). - def __init__(self, bot: Bot) -> None: + def __init__(self, bot: Bot): self.bot = bot self.name = None diff --git a/bot/exts/internal_eval/_helpers.py b/bot/exts/internal_eval/_helpers.py index 6fb07f61..5b2f8f5d 100644 --- a/bot/exts/internal_eval/_helpers.py +++ b/bot/exts/internal_eval/_helpers.py @@ -80,7 +80,7 @@ class EvalContext: clear the context, use the `.internal clear` command. """ - def __init__(self, context_vars: Namespace, local_vars: Namespace) -> None: + def __init__(self, context_vars: Namespace, local_vars: Namespace): self._locals = dict(local_vars) self.context_vars = dict(context_vars) @@ -182,7 +182,7 @@ class EvalContext: class WrapEvalCodeTree(ast.NodeTransformer): """Wraps the AST of eval code with the wrapper function.""" - def __init__(self, eval_code_tree: ast.AST, *args, **kwargs) -> None: + def __init__(self, eval_code_tree: ast.AST, *args, **kwargs): super().__init__(*args, **kwargs) self.eval_code_tree = eval_code_tree @@ -207,7 +207,7 @@ class WrapEvalCodeTree(ast.NodeTransformer): class CaptureLastExpression(ast.NodeTransformer): """Captures the return value from a loose expression.""" - def __init__(self, tree: ast.AST, *args, **kwargs) -> None: + def __init__(self, tree: ast.AST, *args, **kwargs): super().__init__(*args, **kwargs) self.tree = tree self.last_node = list(ast.iter_child_nodes(tree))[-1] diff --git a/bot/utils/checks.py b/bot/utils/checks.py index d5c06728..612d1ed6 100644 --- a/bot/utils/checks.py +++ b/bot/utils/checks.py @@ -22,7 +22,7 @@ log = logging.getLogger(__name__) class InWhitelistCheckFailure(CheckFailure): """Raised when the `in_whitelist` check fails.""" - def __init__(self, redirect_channel: Optional[int]) -> None: + def __init__(self, redirect_channel: Optional[int]): self.redirect_channel = redirect_channel if redirect_channel: diff --git a/bot/utils/randomization.py b/bot/utils/randomization.py index 3360ef44..c9eabbd2 100644 --- a/bot/utils/randomization.py +++ b/bot/utils/randomization.py @@ -11,7 +11,7 @@ class RandomCycle: The iterable is reshuffled after each full cycle. """ - def __init__(self, iterable: Iterable) -> None: + def __init__(self, iterable: Iterable): self.iterable = list(iterable) self.index = itertools.cycle(range(len(iterable))) -- cgit v1.2.3