Skip to content

Commit 1ae6579

Browse files
committed
fix: code cleanup, better typing, and minor refactors
1 parent 90999ea commit 1ae6579

6 files changed

Lines changed: 148 additions & 193 deletions

File tree

bot.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
__version__ = "5.0.0-alpha.1"
22

3-
43
import asyncio
54
import copy
65
import hashlib
@@ -10,12 +9,12 @@
109
import sys
1110
import typing
1211
from datetime import datetime, timedelta, timezone
13-
from subprocess import PIPE
1412
from types import SimpleNamespace
13+
from typing import Optional
1514

1615
import discord
1716
import isodate
18-
from aiohttp import ClientResponseError, ClientSession
17+
from aiohttp import ClientSession
1918
from dateutil import parser
2019
from discord.ext import commands, tasks
2120
from discord.ext.commands.view import StringView
@@ -33,21 +32,19 @@
3332
pass
3433

3534
from core import checks
36-
from core.changelog import Changelog
37-
from core.clients import ApiClient, MongoDBClient, PluginDatabaseClient
35+
from core.clients import MongoDBClient, PluginDatabaseClient
3836
from core.config import ConfigManager
3937
from core.models import (
4038
DMDisabled,
4139
HostingMethod,
42-
InvalidConfigError,
4340
PermissionLevel,
4441
SafeFormatter,
4542
configure_logging,
4643
getLogger,
4744
)
48-
from core.thread import ThreadManager
45+
from core.thread import Thread, ThreadManager
4946
from core.time import human_timedelta
50-
from core.utils import human_join, normalize_alias, parse_alias, truncate, tryint, extract_forwarded_content
47+
from core.utils import extract_forwarded_content, human_join, normalize_alias, parse_alias, truncate, tryint
5148

5249
logger = getLogger(__name__)
5350

@@ -61,7 +58,8 @@
6158
except AttributeError:
6259
logger.error("Failed to use WindowsProactorEventLoopPolicy.", exc_info=True)
6360

64-
61+
class ModmailCommandContext(commands.Context["ModmailBot"]):
62+
thread: Optional[Thread]
6563
class ModmailBot(commands.Bot):
6664
def __init__(self):
6765
self.config = ConfigManager(self)
@@ -1416,19 +1414,20 @@ async def trigger_auto_triggers(self, message, channel, *, cls=commands.Context)
14161414
ctx.command.checks = old_checks
14171415
continue
14181416

1419-
async def get_context(self, message, *, cls=commands.Context):
1417+
async def get_context(self, message, *, cls=ModmailCommandContext):
14201418
"""
14211419
Returns the invocation context from the message.
14221420
Supports getting the prefix from database.
14231421
"""
14241422

14251423
view = StringView(message.content)
1426-
ctx = cls(prefix=self.prefix, view=view, bot=self, message=message)
1424+
ctx: ModmailCommandContext = cls(prefix=self.prefix, view=view, bot=self, message=message)
14271425

14281426
if message.author.id == self.user.id:
14291427
return ctx
14301428

1431-
ctx.thread = await self.threads.find(channel=ctx.channel)
1429+
if isinstance(ctx.channel, discord.TextChannel):
1430+
ctx.thread = await self.threads.find(channel=ctx.channel)
14321431

14331432
prefixes = await self.get_prefix()
14341433

0 commit comments

Comments
 (0)