Skip to content

Commit 28e7e54

Browse files
committed
Various changes/cleanups.
Remove the need for TYPE_CHECKING in most cases with a siple import of core.
1 parent f50de13 commit 28e7e54

20 files changed

+202
-189
lines changed

constants/_meta.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"""
2323
from __future__ import annotations
2424

25-
from typing import TYPE_CHECKING, Any, NoReturn, cast
25+
from typing import TYPE_CHECKING, Any, NoReturn, cast, Union
2626

27-
from core import CONFIG
27+
import toml
2828

2929

3030
if TYPE_CHECKING:
@@ -33,13 +33,16 @@
3333
from types_.config import Database, Logging, Tokens
3434

3535

36+
_config = toml.load("config.toml")
37+
38+
3639
class ConstantsMeta(type):
3740
def __new__(mcs, name: str, bases: tuple[type, ...], attrs: dict[str, Any]) -> Self:
3841
if name == "CONSTANTS":
3942
return super().__new__(mcs, name, bases, attrs)
4043

4144
try:
42-
section = cast(Tokens | Database | Logging, CONFIG[name.upper()])
45+
section = cast(Union["Tokens", "Database", "Logging"], _config[name.upper()])
4346
except KeyError:
4447
return super().__new__(mcs, name, bases, attrs)
4548

core/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"""
2323
__version__ = "0.0.1a"
2424

25-
from . import errors as errors, utils as utils
25+
from . import utils as utils
26+
from .bot import Bot
2627
from .converters import *
2728
from .core import *
2829
from .context import *
30+
from .errors import *

bot.py renamed to core/bot.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@
2525
import asyncio
2626
import json
2727
import pathlib
28-
from asyncio import Queue
2928
from collections import deque
3029
from typing import TYPE_CHECKING, Any
3130

3231
import aiohttp
3332
import asyncpg
3433
import discord
35-
import mystbin
3634
from discord.ext import commands
3735

38-
from core import CONFIG
39-
from core.context import Context
40-
from core.utils.logging import LogHandler
41-
from modules import EXTENSIONS
36+
from .core import CONFIG
37+
from .context import Context
38+
from .utils import LogHandler
4239

4340

4441
if TYPE_CHECKING:
@@ -85,6 +82,7 @@ async def start(self, token: str, *, reconnect: bool = True) -> None:
8582
await super().start(token=token, reconnect=reconnect)
8683
finally:
8784
path = pathlib.Path("logs/prev_events.log")
85+
8886
with path.open("w+", encoding="utf-8") as f:
8987
for event in self._previous_websocket_events:
9088
try:
@@ -98,29 +96,3 @@ async def close(self) -> None:
9896
"""Closes the Bot. It will also close the internal :class:`aiohttp.ClientSession`."""
9997
await self.session.close()
10098
await super().close()
101-
102-
103-
async def main() -> None:
104-
async with Bot() as bot, aiohttp.ClientSession() as session, asyncpg.create_pool(
105-
dsn=CONFIG["DATABASE"]["dsn"]
106-
) as pool, LogHandler(bot=bot) as handler:
107-
bot.logging_queue = Queue()
108-
bot.session = session
109-
bot.pool = pool
110-
bot.log_handler = handler
111-
112-
bot.mb_client = mystbin.Client(token=CONFIG["TOKENS"]["mystbin"], session=session)
113-
114-
await bot.load_extension("jishaku")
115-
for extension in EXTENSIONS:
116-
await bot.load_extension(extension.name)
117-
bot.log_handler.log.info(
118-
"Loaded %sextension: %s",
119-
"module " if extension.ispkg else "",
120-
extension.name,
121-
)
122-
123-
await bot.start(CONFIG["TOKENS"]["bot"])
124-
125-
126-
asyncio.run(main())

core/config.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

core/context.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@
99

1010

1111
if TYPE_CHECKING:
12-
from bot import Bot
12+
from .bot import Bot
13+
1314

1415
__all__ = (
1516
"Context",
1617
"GuildContext",
1718
"Interaction",
1819
)
1920

21+
2022
Interaction: TypeAlias = discord.Interaction["Bot"]
2123

2224

2325
class Context(commands.Context["Bot"]):
26+
2427
def author_is_mod(self) -> bool:
2528
member: discord.Member
29+
2630
if self.guild is None: # dms
2731
guild = self.bot.get_guild(GUILD_ID)
32+
2833
if not guild:
2934
return False
3035

core/converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ async def convert(self, ctx: Context, argument: str) -> Codeblock: # type: igno
8282
if not code and not language:
8383
code[:] = previous_characters
8484

85-
return Codeblock("".join(language), "".join(code[len(language) : -backticks]))
85+
return Codeblock("".join(language), "".join(code[len(language): -backticks]))

core/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import toml
2828
from discord.ext import commands
2929

30+
3031
if TYPE_CHECKING:
3132
from types_.config import Config
3233

core/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
SOFTWARE.
2222
"""
2323
from .formatters import *
24+
from .logging import LogHandler

core/utils/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if TYPE_CHECKING:
1212
from typing_extensions import Self
1313

14-
from bot import Bot
14+
from core import Bot
1515

1616

1717
class QueueEmitHandler(logging.Handler):

core/utils/paginator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from discord.ext import commands
3131
from discord.utils import MISSING
3232

33-
from core.context import Context
33+
from core import Context
3434

3535

3636
if TYPE_CHECKING:

0 commit comments

Comments
 (0)