Skip to content

Commit e070195

Browse files
committed
Fixed it all
1 parent 2068de0 commit e070195

17 files changed

+115
-64
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ COPY poetry.lock pyproject.toml ./
4747
RUN poetry install --without=dev
4848

4949
COPY . /app/
50-
ENTRYPOINT poetry run python bot.py
50+
ENTRYPOINT poetry run python -O bot.py

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ This will allow for public evals. To disable the functionality entirely, ensure
1818

1919
In the future we'll provide a systemd unit, pm2 config and maybe a Dockerfile for running the bot, but for now you can use your ideal terminal and:-
2020
```shell
21-
python launcher.py
21+
python -O launcher.py
2222
```
2323

24+
(we run with `python -O` because we use `assert` for type checking purposes).
25+
2426
### Docker
2527

2628
We also provide both `Dockerfile` and `docker-compose.yml` files for running the bot in Docker.

config.template.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dsn = 'postgres://pythonistabot:pythonistabot@database:5432/pythonistabot' # ass
1313
# 20 = INFO
1414
# 10 = DEBUG
1515
[LOGGING]
16-
webhook_url = ""
16+
webhook_url = "" # optional
1717
level = 20
1818

1919
[SNEKBOX] # optional

core/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
__version__ = "0.0.1a"
2424

2525
from . import utils as utils
26-
from .bot import Bot
26+
from .bot import Bot as Bot
27+
from .context import *
2728
from .converters import *
2829
from .core import *
29-
from .context import *
3030
from .errors import *

core/bot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@
3232
import discord
3333
from discord.ext import commands
3434

35-
from .core import CONFIG
3635
from .context import Context
36+
from .core import CONFIG
3737
from .utils import LogHandler
3838

3939

4040
if TYPE_CHECKING:
41-
import mystbin
42-
from queue import Queue
41+
from asyncio import Queue
4342
from logging import LogRecord
4443

44+
import mystbin
45+
4546

4647
class Bot(commands.Bot):
4748
session: aiohttp.ClientSession

core/converters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""MIT License
22
3-
Copyright (c) 2021 - Present PythonistaGuild
3+
Copyright (c) 2021-Present PythonistaGuild
44
55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -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/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""MIT License
22
3-
Copyright (c) 2021 - Present PythonistaGuild
3+
Copyright (c) 2021-Present PythonistaGuild
44
55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

core/utils/__init__.py

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

core/utils/paginator.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""MIT License
22
3-
Copyright (c) 2021 - Present PythonistaGuild
3+
Copyright (c) 2021-Present PythonistaGuild
44
55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -20,15 +20,23 @@
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
2222
"""
23+
from __future__ import annotations
24+
2325
import asyncio
26+
from abc import abstractmethod
2427
from typing import TYPE_CHECKING, Any
2528

2629
import discord
2730
from discord import ui # shortcut because I'm lazy
28-
from discord.ext.commands import CommandError, Paginator as _Paginator
31+
from discord.ext.commands import CommandError, Paginator as _Paginator # type: ignore # why does this need a stub file?
2932
from discord.utils import MISSING
33+
34+
35+
if TYPE_CHECKING:
36+
from typing_extensions import Self
37+
3038
from core import Context
31-
from typing_extensions import Self
39+
3240

3341
__all__ = ("CannotPaginate", "Pager", "KVPager", "TextPager")
3442

@@ -86,9 +94,10 @@ def __init__(
8694
if stop:
8795
self.reaction_emojis.append(("\N{BLACK SQUARE FOR STOP}", self.stop_pages)) # type: ignore
8896

89-
if ctx.guild is not None:
97+
if ctx.guild:
9098
self.permissions = self.channel.permissions_for(ctx.guild.me)
9199
else:
100+
assert isinstance(self.channel, discord.abc.PrivateChannel)
92101
self.permissions = self.channel.permissions_for(ctx.me)
93102

94103
if not self.permissions.embed_links:
@@ -97,7 +106,7 @@ def __init__(
97106
if not self.permissions.send_messages:
98107
raise CannotPaginate("Bot cannot send messages.")
99108

100-
def setup_buttons(self):
109+
def setup_buttons(self) -> None:
101110
self.clear_items()
102111
for emoji, button in self.reaction_emojis:
103112
btn = ui.Button[Self](emoji=emoji)
@@ -108,14 +117,15 @@ def get_page(self, page: int) -> list[Any]:
108117
base = (page - 1) * self.per_page
109118
return self.entries[base : base + self.per_page]
110119

120+
@abstractmethod
111121
def get_content(self, entry: Any, page: int, *, first: bool = False) -> str:
112-
return None
122+
raise NotImplementedError
113123

114-
def get_embed(self, entries: list[Any], page: int, *, first: bool = False):
124+
def get_embed(self, entries: list[Any], page: int, *, first: bool = False) -> discord.Embed:
115125
self.prepare_embed(entries, page, first=first)
116126
return self.embed
117127

118-
def prepare_embed(self, entries: list[Any], page: int, *, first: bool = False):
128+
def prepare_embed(self, entries: list[Any], page: int, *, first: bool = False) -> None:
119129
p: list[Any] = []
120130
for index, entry in enumerate(entries, 1 + ((page - 1) * self.per_page)):
121131
if self.nocount:
@@ -162,28 +172,28 @@ async def checked_show_page(self, page: int):
162172
if page != 0 and page <= self.maximum_pages:
163173
await self.show_page(page)
164174

165-
async def first_page(self, inter: discord.Interaction):
175+
async def first_page(self, inter: discord.Interaction) -> None:
166176
await inter.response.defer()
167177
await self.show_page(1)
168178

169-
async def last_page(self, inter: discord.Interaction):
179+
async def last_page(self, inter: discord.Interaction) -> None:
170180
await inter.response.defer()
171181
await self.show_page(self.maximum_pages)
172182

173-
async def next_page(self, inter: discord.Interaction):
183+
async def next_page(self, inter: discord.Interaction) -> None:
174184
await inter.response.defer()
175185
await self.checked_show_page(self.current_page + 1)
176186

177-
async def previous_page(self, inter: discord.Interaction):
187+
async def previous_page(self, inter: discord.Interaction) -> None:
178188
await inter.response.defer()
179189
await self.checked_show_page(self.current_page - 1)
180190

181-
async def show_current_page(self, inter: discord.Interaction):
191+
async def show_current_page(self, inter: discord.Interaction) -> None:
182192
await inter.response.defer()
183193
if self.paginating:
184194
await self.show_page(self.current_page)
185195

186-
async def numbered_page(self, inter: discord.Interaction):
196+
async def numbered_page(self, inter: discord.Interaction) -> None:
187197
await inter.response.defer()
188198
to_delete = [await self.channel.send("What page do you want to go to?")]
189199

@@ -205,8 +215,8 @@ def message_check(m: discord.Message) -> bool:
205215
await asyncio.sleep(5)
206216

207217
try:
208-
await self.channel.delete_messages(to_delete)
209-
except Exception:
218+
await self.channel.delete_messages(to_delete) # type: ignore # we handle the attribute error or http since exception handling is free
219+
except (AttributeError, discord.HTTPException):
210220
pass
211221

212222
async def stop_pages(self, interaction: discord.Interaction | None = None):
@@ -302,7 +312,7 @@ def get_page(self, page: int) -> Any:
302312
return self.entries[page - 1]
303313

304314
def get_embed(self, entries: list[str], page: int, *, first: bool = False) -> discord.Embed:
305-
return None # type: ignore
315+
return None # type: ignore
306316

307317
def get_content(self, entry: str, page: int, *, first: bool = False) -> str:
308318
if self.maximum_pages > 1:

launcher.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
SOFTWARE.
2222
"""
2323
import asyncio
24-
from typing import TYPE_CHECKING
2524

2625
import aiohttp
2726
import asyncpg
@@ -36,7 +35,6 @@ async def main() -> None:
3635
async with core.Bot() as bot, aiohttp.ClientSession() as session, asyncpg.create_pool(
3736
dsn=core.CONFIG["DATABASE"]["dsn"]
3837
) as pool, LogHandler(bot=bot) as handler:
39-
4038
bot.logging_queue = asyncio.Queue()
4139
bot.session = session
4240
bot.pool = pool
@@ -60,4 +58,4 @@ async def main() -> None:
6058
try:
6159
asyncio.run(main())
6260
except KeyboardInterrupt:
63-
print('Fix this later, but you killed bot with KeyboardInterrupt...')
61+
print("Fix this later, but you killed bot with KeyboardInterrupt...")

0 commit comments

Comments
 (0)