Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
- rename SpecialCog to ExtendedCog
- rename SpecialGroupCog to ExtendedGroupCog
- merge cog_property.py, misc_command.py and special_cog.py to extended_commands.py
- move modules out of core
- rename modules to libraries
- clean imports by using relative inside core
  • Loading branch information
AiroPi committed Dec 15, 2023
1 parent 96699eb commit 2db7d8a
Show file tree
Hide file tree
Showing 42 changed files with 139 additions and 175 deletions.
4 changes: 2 additions & 2 deletions src/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from discord import app_commands

from core import SpecialCog, config
from core import ExtendedCog, config

if TYPE_CHECKING:
from discord import Interaction
Expand All @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)


class Admin(SpecialCog["MyBot"]): # TODO(airo.pi_): add checkers
class Admin(ExtendedCog): # TODO(airo.pi_): add checkers
@app_commands.command()
@app_commands.guilds(config.SUPPORT_GUILD_ID)
async def reload_extension(self, inter: Interaction, extension: str):
Expand Down
8 changes: 4 additions & 4 deletions src/cogs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from aiohttp import hdrs, web
from psutil import Process

from core import SpecialCog, config
from core import ExtendedCog, config

if TYPE_CHECKING:
from mybot import MyBot
Expand All @@ -17,7 +17,7 @@
logger = logging.getLogger(__name__)

P = ParamSpec("P")
S = TypeVar("S", bound="SpecialCog[MyBot]")
S = TypeVar("S", bound="ExtendedCog")


def route(method: str, path: str):
Expand All @@ -28,9 +28,9 @@ def wrap(func: Callable[Concatenate[S, web.Request, P], Awaitable[web.Response]]
return wrap


class API(SpecialCog["MyBot"]):
class API(ExtendedCog):
def __init__(self, bot: MyBot):
self.bot: MyBot = bot
super().__init__(bot)
self.app = web.Application()
self.runner = web.AppRunner(self.app)
self.routes = web.RouteTableDef()
Expand Down
4 changes: 2 additions & 2 deletions src/cogs/calculator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from discord import ButtonStyle, app_commands, ui
from discord.app_commands import locale_str as __

from core import SpecialCog
from core import ExtendedCog
from core.i18n import _

from .calcul import Calcul, UnclosedParentheses
Expand All @@ -36,7 +36,7 @@ def display_calcul(calcul: Calcul) -> str:
return display


class Calculator(SpecialCog["MyBot"]):
class Calculator(ExtendedCog):
@app_commands.command(
name=__("calculator"),
description=__("Show a calculator you can use."),
Expand Down
7 changes: 3 additions & 4 deletions src/cogs/clear/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import discord
from discord import app_commands, ui
from discord.app_commands import Transform, locale_str as __
from discord.ext.commands import Cog # pyright: ignore[reportMissingTypeStubs]
from typing_extensions import Self

from core import Menu, MessageDisplay, ResponseType, response_constructor
from core import ExtendedCog, Menu, MessageDisplay, ResponseType, response_constructor
from core.checkers import MaxConcurrency
from core.errors import BadArgument, MaxConcurrencyReached, NonSpecificError, UnexpectedError
from core.i18n import _
Expand Down Expand Up @@ -44,9 +43,9 @@ def channel_bucket(inter: discord.Interaction):
return inter.channel_id


class Clear(Cog):
class Clear(ExtendedCog):
def __init__(self, bot: MyBot):
self.bot: MyBot = bot
super().__init__(bot)

self.clear_max_concurrency = MaxConcurrency(1, key=channel_bucket, wait=False)

Expand Down
4 changes: 2 additions & 2 deletions src/cogs/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from discord import app_commands
from discord.app_commands import locale_str as __

from core import SpecialGroupCog, cog_property
from core import ExtendedGroupCog, cog_property

if TYPE_CHECKING:
from discord import Interaction
Expand All @@ -27,7 +27,7 @@
@app_commands.guild_only()
@app_commands.default_permissions(administrator=True)
class Config(
SpecialGroupCog["MyBot"],
ExtendedGroupCog,
group_name=__("config"),
group_description=__("Set configurations."),
group_extras={"beta": True},
Expand Down
6 changes: 2 additions & 4 deletions src/cogs/config/config_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
import logging
from typing import TYPE_CHECKING

from core import ResponseType, SpecialCog, response_constructor
from core import ExtendedCog, ResponseType, response_constructor
from core.errors import UnexpectedError
from core.i18n import _

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)


class ConfigBot(SpecialCog["MyBot"], name="config_bot"):
class ConfigBot(ExtendedCog, name="config_bot"):
async def public_translation(self, inter: Interaction, value: bool) -> None:
if inter.guild_id is None:
raise UnexpectedError()
Expand Down
6 changes: 2 additions & 4 deletions src/cogs/config/config_guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import logging
from typing import TYPE_CHECKING

from core import SpecialCog
from core import ExtendedCog

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)


class ConfigGuild(SpecialCog["MyBot"], name="config_guild"):
class ConfigGuild(ExtendedCog, name="config_guild"):
async def emote(self, inter: Interaction) -> None:
raise NotImplementedError()
7 changes: 2 additions & 5 deletions src/cogs/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import discord
from discord import ButtonStyle, Color, Embed, Message, TextStyle, app_commands, ui
from discord.ext import commands
from discord.ext.commands import Cog # pyright: ignore[reportMissingTypeStubs]
from discord.interactions import Interaction

from core import ExtendedCog
from core._config import config
from core.checkers.app import is_me
from core.checkers.base import is_me_bool
Expand All @@ -30,10 +30,7 @@
logger = logging.getLogger(__name__)


class Eval(Cog):
def __init__(self, bot: MyBot):
self.bot: MyBot = bot

class Eval(ExtendedCog):
@commands.command(name="+eval")
@commands.check(lambda ctx: is_me_bool(ctx.author.id))
async def add_eval(self, ctx: commands.Context[MyBot]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/cogs/game/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from discord import app_commands
from discord.app_commands import locale_str as __

from core import SpecialGroupCog, cog_property
from core import ExtendedGroupCog, cog_property

from .connect4 import GameConnect4
from .game_2084 import Two048Cog
Expand All @@ -29,7 +29,7 @@


class Game(
SpecialGroupCog["MyBot"],
ExtendedGroupCog,
group_name=__("game"),
group_description=__("Play some games."),
group_extras={"soon": True},
Expand Down
6 changes: 2 additions & 4 deletions src/cogs/game/connect4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import logging
from typing import TYPE_CHECKING

from core import SpecialCog
from core import ExtendedCog

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)


class GameConnect4(SpecialCog["MyBot"], name="game_connect4"):
class GameConnect4(ExtendedCog, name="game_connect4"):
async def connect4(self, inter: Interaction) -> None:
raise NotImplementedError()
6 changes: 2 additions & 4 deletions src/cogs/game/game_2084.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
from discord import ui
from two048 import Direction, Tile, Two048

from core import ResponseType, SpecialCog, response_constructor
from core import ExtendedCog, ResponseType, response_constructor
from core.constants import Emojis

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,7 +44,7 @@ class ResponseT(TypedDict):
}


class Two048Cog(SpecialCog["MyBot"], name="game_2048"):
class Two048Cog(ExtendedCog, name="game_2048"):
async def two048(self, inter: Interaction) -> None:
await inter.response.send_message(**Two048View.init_game(inter.user))

Expand Down
6 changes: 2 additions & 4 deletions src/cogs/game/minesweeper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import discord
from discord import ui

from core import ResponseType, SpecialCog, response_constructor
from core import ExtendedCog, ResponseType, response_constructor

from .minesweeper_game import Minesweeper, MinesweeperConfig, PlayType

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,7 +55,7 @@ def get_char(row: int, column: int) -> str:
return description


class MinesweeperCog(SpecialCog["MyBot"], name="minesweeper"):
class MinesweeperCog(ExtendedCog, name="minesweeper"):
async def minesweeper(self, inter: Interaction) -> None:
embed = response_constructor(ResponseType.info, "Minesweeper").embed
game = Minesweeper(board_size, 0) # only used for the view, will be regenerated within the first play
Expand Down
6 changes: 2 additions & 4 deletions src/cogs/game/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import logging
from typing import TYPE_CHECKING

from core import SpecialCog
from core import ExtendedCog

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)


class GameRPC(SpecialCog["MyBot"], name="game_rpc"):
class GameRPC(ExtendedCog, name="game_rpc"):
async def rpc(self, inter: Interaction) -> None:
raise NotImplementedError()
6 changes: 2 additions & 4 deletions src/cogs/game/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import logging
from typing import TYPE_CHECKING

from core import SpecialCog
from core import ExtendedCog

if TYPE_CHECKING:
from discord import Interaction

from mybot import MyBot


logger = logging.getLogger(__name__)


class GameTictactoe(SpecialCog["MyBot"], name="game_tictactoe"):
class GameTictactoe(ExtendedCog, name="game_tictactoe"):
async def tictactoe(self, inter: Interaction) -> None:
raise NotImplementedError()
8 changes: 2 additions & 6 deletions src/cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import discord
from discord import app_commands, ui
from discord.app_commands import Choice, locale_str as __
from discord.ext.commands import Cog # pyright: ignore[reportMissingTypeStubs]
from discord.utils import get

from commands_exporter import ContextCommand, FeatureType, Misc, MiscCommandsType, SlashCommand
from core import ResponseType, response_constructor
from core import ExtendedCog, ResponseType, response_constructor
from core.constants import Emojis
from core.i18n import _
from core.utils import splitter
Expand All @@ -33,10 +32,7 @@
}


class Help(Cog):
def __init__(self, bot: MyBot) -> None:
self.bot = bot

class Help(ExtendedCog):
@app_commands.command(name=__("help"), description=__("Get help about the bot."), extras={"beta": True})
@app_commands.rename(feature_identifier=__("feature"))
async def _help(self, inter: Interaction, feature_identifier: str | None = None):
Expand Down
8 changes: 3 additions & 5 deletions src/cogs/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from discord import app_commands
from discord.app_commands import locale_str as __
from discord.ext.commands import Cog # pyright: ignore[reportMissingTypeStubs]

from core import ExtendedCog

if TYPE_CHECKING:
from discord import Interaction
Expand All @@ -20,10 +21,7 @@
logger = logging.getLogger(__name__)


class Ping(Cog):
def __init__(self, bot: MyBot):
self.bot: MyBot = bot

class Ping(ExtendedCog):
@app_commands.command(
name=__("ping"),
description=__("Get the bot latency."),
Expand Down
4 changes: 2 additions & 2 deletions src/cogs/poll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from discord.app_commands import locale_str as __
from sqlalchemy.orm import selectinload

from core import SpecialGroupCog, db
from core import ExtendedGroupCog, db
from core.checkers.app import bot_required_permissions
from core.errors import NonSpecificError
from core.i18n import _
Expand All @@ -32,7 +32,7 @@

@app_commands.default_permissions(administrator=True)
@app_commands.guild_only()
class PollCog(SpecialGroupCog["MyBot"], group_name=__("poll"), group_description=__("Create a new poll")):
class PollCog(ExtendedGroupCog, group_name=__("poll"), group_description=__("Create a new poll")):
def __init__(self, bot: MyBot):
super().__init__(bot)

Expand Down
4 changes: 2 additions & 2 deletions src/cogs/restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from typing import TYPE_CHECKING

from core import SpecialCog, misc_command
from core import ExtendedCog, misc_command
from core.checkers.misc import bot_required_permissions, is_activated, is_user_authorized, misc_check

if TYPE_CHECKING:
Expand All @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)


class Restore(SpecialCog["MyBot"]):
class Restore(ExtendedCog):
@misc_command("restore", description="Send a message back in chat if a link is send.", extras={"soon": True})
@bot_required_permissions(manage_webhooks=True)
@misc_check(is_activated)
Expand Down
Loading

0 comments on commit 2db7d8a

Please sign in to comment.