Skip to content

Commit

Permalink
⚡ Convert all cogs to use _base.py directory, and isort everything
Browse files Browse the repository at this point in the history
  • Loading branch information
tookender committed May 16, 2024
1 parent 770aa02 commit 1951ead
Show file tree
Hide file tree
Showing 37 changed files with 137 additions and 343 deletions.
11 changes: 6 additions & 5 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import logging
import pathlib
from typing import List

import discord
import mystbin as mystbin_library
import aiohttp
import asyncpg
import discord
import mystbin as mystbin_library
from discord.ext import commands

import config
from typing import List


class Korii(commands.AutoShardedBot):
pool: asyncpg.Pool | None
pool: asyncpg.Pool
user: discord.ClientUser
owner_ids: List[int]

Expand Down Expand Up @@ -102,7 +103,7 @@ async def load_extensions(self) -> None:
return self.ext_logger.info(f"Loaded {success} out of {success + failed} extensions")

async def setup_hook(self) -> None:
self.pool = await asyncpg.create_pool(config.DATABASE)
self.pool = await asyncpg.create_pool(config.DATABASE) # type: ignore

if not self.pool:
raise RuntimeError("Failed to connect with the database.")
Expand Down
4 changes: 2 additions & 2 deletions extensions/events/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .guild import GuildCog
from .ping import PingCog
from .errors import ErrorsCog
from .ghost import GhostCog
from .guild import GuildCog
from .ping import PingCog


class Events(PingCog, GuildCog, ErrorsCog, GhostCog): # PingCog needs to be first, it adds attributes
Expand Down
8 changes: 8 additions & 0 deletions extensions/events/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from discord.ext import commands

from bot import Korii


class EventsBase(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
6 changes: 4 additions & 2 deletions extensions/events/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from discord.ext import commands

from utils import Cog, BlacklistedError, Embed
from utils import BlacklistedError, Embed

from ._base import EventsBase

errors: dict[type[Exception], str] = {
BlacklistedError: "bro got blacklisted 💀💀 point and laugh at this user {self.bot.E['pepepoint']}",
Expand All @@ -9,7 +11,7 @@
}


class ErrorsCog(Cog):
class ErrorsCog(EventsBase):
@commands.Cog.listener()
async def on_command_error(self, ctx, error: Exception):
if isinstance(error, commands.CommandNotFound):
Expand Down
6 changes: 3 additions & 3 deletions extensions/events/ghost.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import discord
from discord.ext import commands

from utils import Cog
from ._base import EventsBase


class GhostCog(Cog):
class GhostCog(EventsBase):
@commands.Cog.listener("on_message")
async def ghost(self, message: discord.Message):
if message.author.bot:
Expand All @@ -14,4 +14,4 @@ async def ghost(self, message: discord.Message):
return await message.reply("G-HOOOOOOST <:ghoost:1228090615510732850>", silent=True)

if message.content.lower() == "👻":
return await message.reply("G-HOOOOOOST 👻", silent=True)
return await message.reply("G-HOOOOOOST 👻", silent=True)
11 changes: 6 additions & 5 deletions extensions/events/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import discord
from discord.ext import commands

from utils import Embed, Cog
from bot import Korii
from utils import Embed

from ._base import EventsBase

class GuildCog(Cog):

class GuildCog(EventsBase):
@commands.Cog.listener("on_guild_join")
async def smash_or_pass(self, guild: discord.Guild):
if not guild.owner:
Expand All @@ -32,8 +33,8 @@ async def smash_or_pass(self, guild: discord.Guild):
return await guild.leave()

embed = Embed(
title=f"💖 Thanks for choosing Korii",
description="Thank you for choosing **Korii**. We promise to not let you down.\n" "For more information, use the `/help` command.",
title="💖 Thanks for choosing Korii",
description="Thank you for choosing **Korii**. For more information, use the `/help` command.",
)

try:
Expand Down
6 changes: 4 additions & 2 deletions extensions/events/ping.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import random

import discord
from discord.ext import commands

from utils import Cog
from ._base import EventsBase


class PingCog(Cog):
class PingCog(EventsBase):
@commands.Cog.listener("on_message")
async def ping(self, message: discord.Message):
if message.reference and message.reference.resolved:
Expand Down
2 changes: 1 addition & 1 deletion extensions/fun/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .dog import DogCog
from .actions import ActionsCog
from .dog import DogCog
from .random import RandomCog


Expand Down
8 changes: 8 additions & 0 deletions extensions/fun/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from discord.ext import commands

from bot import Korii


class FunBase(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
6 changes: 4 additions & 2 deletions extensions/fun/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import discord
from discord import app_commands

from utils import Interaction, Cog
from utils import Interaction

from ._base import FunBase

class ActionsCog(Cog):

class ActionsCog(FunBase):
@app_commands.command(description="Do a funny lil' prank on someone to show how grateful you are!")
@app_commands.describe(user="The user you want to prank.")
@app_commands.guild_only()
Expand Down
7 changes: 5 additions & 2 deletions extensions/fun/dog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from discord import app_commands
from utils import Embed, Interaction, Cog

from utils import Embed, Interaction

class DogCog(Cog):
from ._base import FunBase


class DogCog(FunBase):
@app_commands.command(description="Sends a random image of my dog :D")
@app_commands.guild_only()
@app_commands.describe(ephemeral="If the message should be private or not.")
Expand Down
8 changes: 4 additions & 4 deletions extensions/fun/random.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

import asyncio
import random
from typing import Optional

import discord
from discord import app_commands
from discord.ext import commands

from utils import Embed, Interaction, Cog
from utils import Embed, Interaction

from ._base import FunBase


class RandomCog(Cog):
class RandomCog(FunBase):
fun = app_commands.Group(name="fun", description="General entertainment commands.")

async def wait_and_send(self, interaction: Interaction, content: str, wait: int = 2):
Expand Down
5 changes: 3 additions & 2 deletions extensions/ipc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from aiohttp import web
from aiohttp.web import Request, Response, json_response
from aiohttp.web_routedef import route

from bot import Korii
from utils import Cog
from aiohttp import web
from aiohttp.web import Response, json_response, Request


class IPC(Cog):
Expand Down
8 changes: 8 additions & 0 deletions extensions/levelling/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from discord.ext import commands

from bot import Korii


class LevellingBase(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
7 changes: 4 additions & 3 deletions extensions/levelling/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import discord
from discord import app_commands
from discord.ext import commands

from utils import Embed, Interaction, Cog
from utils import Embed, Interaction

from ._base import LevellingBase

class CommandsCog(Cog):

class CommandsCog(LevellingBase):
@app_commands.command(description="View the level of the specified member.")
@app_commands.describe(user="The user you want to view the level of.")
@app_commands.guild_only()
Expand Down
6 changes: 4 additions & 2 deletions extensions/levelling/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import discord
from discord.ext import commands

from utils import Embed, Cog
from utils import Cog, Embed

from ._base import LevellingBase

class EventsCog(Cog):

class EventsCog(LevellingBase):
@commands.Cog.listener("on_message")
async def levelling(self, message: discord.Message):
if not message.guild or message.author.bot or isinstance(message.author, discord.User):
Expand Down
8 changes: 8 additions & 0 deletions extensions/moderation/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from discord.ext import commands

from bot import Korii


class ModerationBase(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
7 changes: 4 additions & 3 deletions extensions/moderation/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import discord
from discord import app_commands
from discord.app_commands import Choice
from discord.ext import commands

from utils import Embed, Interaction, utils, Cog
from utils import Embed, Interaction, utils

from ._base import ModerationBase

class BasicCog(Cog):

class BasicCog(ModerationBase):
@app_commands.command(description="Kicks the specified member from the server.")
@app_commands.guild_only()
@app_commands.default_permissions(kick_members=True)
Expand Down
4 changes: 2 additions & 2 deletions extensions/utility/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from .config import ConfigCog
from .embed import EmbedCog
from .info import InfoCog
from .neofetch import NeofetchCog
from .ping import PingCog
from .source import SourceCog
from .embed import EmbedCog
from .translate import TranslateCog
from .spotify import SpotifyCog
from .translate import TranslateCog


class Utility(ConfigCog, InfoCog, NeofetchCog, PingCog, SourceCog, EmbedCog, TranslateCog, SpotifyCog):
Expand Down
8 changes: 8 additions & 0 deletions extensions/utility/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from discord.ext import commands

from bot import Korii


class UtilityBase(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
6 changes: 4 additions & 2 deletions extensions/utility/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import discord
from discord import app_commands

from utils import Embed, Interaction, Cog, Invalid
from utils import Embed, Interaction, Invalid

from ._base import UtilityBase


class Modal(discord.ui.Modal):
Expand Down Expand Up @@ -177,7 +179,7 @@ async def update_message(interaction: Interaction, edit: Optional[bool] = True):
return await interaction.response.send_message(f"An error occurred: {traceback.format_exception(e)}", ephemeral=True)


class ConfigCog(Cog):
class ConfigCog(UtilityBase):
group = app_commands.Group(name="config", description="Configure your guild's bot configuration.")

@group.command(description="Configure your guild's levelling system.")
Expand Down
8 changes: 4 additions & 4 deletions extensions/utility/embed.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import discord
from discord import app_commands
from discord.ext import commands

from utils import Interaction, Cog
from utils import Interaction
from views.embed import EmbedView

from ._base import UtilityBase

class EmbedCog(Cog):

class EmbedCog(UtilityBase):
@app_commands.command(description="Create a custom embed.")
@app_commands.describe(ephemeral="If the message should be sent ephemerally or not.")
async def embed(self, interaction: Interaction, ephemeral: bool = False):
Expand Down
6 changes: 4 additions & 2 deletions extensions/utility/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from discord import app_commands
from discord.ext import commands

from utils import utils, Embed, Interaction, Cog, EMOJIS
from utils import EMOJIS, Embed, Interaction, utils

from ._base import UtilityBase


class UserInfoView(discord.ui.View):
Expand Down Expand Up @@ -70,7 +72,7 @@ async def delete(self, interaction: Interaction, _):
return await interaction.response.send_message("hehe oki", ephemeral=True)


class InfoCog(Cog):
class InfoCog(UtilityBase):
async def format_permissions(self, permissions: discord.Permissions):
staff_permissions = dict({x for x in permissions if x[1] is True} - set(discord.Permissions(521942715969)))

Expand Down
6 changes: 4 additions & 2 deletions extensions/utility/neofetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from discord import app_commands
from discord.ext import commands

from utils import Interaction, Cog
from utils import Interaction

from ._base import UtilityBase

class NeofetchCog(Cog):

class NeofetchCog(UtilityBase):
@app_commands.command(description="Neofetch but in Discord.")
@app_commands.checks.cooldown(1, 5)
async def neofetch(self, interaction: Interaction):
Expand Down
7 changes: 4 additions & 3 deletions extensions/utility/ping.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from discord import app_commands
from discord.ext import commands

from bot import Korii
from utils import Interaction, utils, Cog
from utils import Interaction, utils

from ._base import UtilityBase

class PingCog(Cog):

class PingCog(UtilityBase):
def spaces(self, ping: float) -> str:
return " " * (9 - len(str(round(ping, 2))))

Expand Down
Loading

0 comments on commit 1951ead

Please sign in to comment.