Skip to content

Commit 7c4770a

Browse files
authored
Changes to new dpy modlog. (#65)
* Changes to new dpy modlog. * Update lock?
1 parent c43a2e6 commit 7c4770a

File tree

9 files changed

+223
-243
lines changed

9 files changed

+223
-243
lines changed

constants/constants.py

-33
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
"Colours",
2929
"Channels",
3030
"ForumTags",
31-
"PAPIWebsocketSubscriptions",
32-
"PAPIWebsocketCloseCodes",
33-
"PAPIWebsocketNotificationTypes",
34-
"PAPIWebsocketOPCodes",
3531
)
3632

3733

@@ -76,32 +72,3 @@ class ForumTags(CONSTANTS):
7672
DISCORDPY = 1006716972802789457
7773
OTHER = 1006717008613740596
7874
RESOLVED = 1006769269201195059
79-
80-
81-
class PAPIWebsocketCloseCodes(CONSTANTS):
82-
NORMAL: int = 1000
83-
ABNORMAL: int = 1006
84-
85-
86-
class PAPIWebsocketOPCodes(CONSTANTS):
87-
# Received from Pythonista API...
88-
HELLO: int = 0
89-
EVENT: int = 1
90-
NOTIFICATION: int = 2
91-
92-
# Sent to Pythonista API...
93-
SUBSCRIBE: str = "subscribe"
94-
UNSUBSCRIBE: str = "unsubscribe"
95-
96-
97-
class PAPIWebsocketSubscriptions(CONSTANTS):
98-
DPY_MODLOG: str = "dpy_modlog"
99-
100-
101-
class PAPIWebsocketNotificationTypes(CONSTANTS):
102-
# Subscriptions...
103-
SUBSCRIPTION_ADDED: str = "subscription_added"
104-
SUBSCRIPTION_REMOVED: str = "subscription_removed"
105-
106-
# Failures...
107-
UNKNOWN_OP: str = "unknown_op"

core/utils/logging.py

-9
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ def emit(self, record: logging.LogRecord) -> None:
2424
self.bot.logging_queue.put_nowait(record)
2525

2626

27-
class PAPILoggingFilter(logging.Filter):
28-
def __init__(self) -> None:
29-
super().__init__(name="modules.api")
30-
31-
def filter(self, record: logging.LogRecord) -> bool:
32-
return not ("Received HELLO" in record.msg or "added our subscription" in record.msg)
33-
34-
3527
class LogHandler:
3628
def __init__(self, *, bot: Bot, stream: bool = True) -> None:
3729
self.log: logging.Logger = logging.getLogger()
@@ -54,7 +46,6 @@ def __enter__(self: Self) -> Self:
5446
logging.getLogger("discord.http").setLevel(logging.INFO)
5547
logging.getLogger("discord.state").setLevel(logging.WARNING)
5648
logging.getLogger("discord.gateway").setLevel(logging.WARNING)
57-
logging.getLogger("modules.api").addFilter(PAPILoggingFilter())
5849

5950
self.log.setLevel(logging.INFO)
6051
handler = RotatingFileHandler(

launcher.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
import aiohttp
2727
import asyncpg
2828
import mystbin
29+
import uvicorn
2930

3031
import core
3132
from core.utils import LogHandler
3233
from modules import EXTENSIONS
34+
from server.application import Application
35+
36+
tasks: set[asyncio.Task[None]] = set()
3337

3438

3539
async def main() -> None:
@@ -59,8 +63,12 @@ async def main() -> None:
5963
extension.name,
6064
)
6165

62-
await bot.start(core.CONFIG["TOKENS"]["bot"])
66+
app: Application = Application(bot=bot)
67+
config: uvicorn.Config = uvicorn.Config(app, port=2332)
68+
server: uvicorn.Server = uvicorn.Server(config)
6369

70+
tasks.add(asyncio.create_task(bot.start(core.CONFIG["TOKENS"]["bot"])))
71+
await server.serve()
6472

6573
try:
6674
asyncio.run(main())

modules/api.py

-179
This file was deleted.

modules/moderation.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import logging
3131
import re
3232
from textwrap import shorten
33-
from typing import TYPE_CHECKING, Any, Self, TypeAlias
33+
from typing import TYPE_CHECKING, Any, Self
3434

3535
import discord
3636
import mystbin
@@ -43,12 +43,12 @@
4343

4444
if TYPE_CHECKING:
4545
from core.context import Interaction
46-
from types_.papi import ModLogPayload, PythonistaAPIWebsocketPayload
46+
from types_.papi import ModLogPayload
4747

48-
ModLogType: TypeAlias = PythonistaAPIWebsocketPayload[ModLogPayload]
4948

5049
logger = logging.getLogger(__name__)
5150

51+
5252
BASE_BADBIN_RE = r"https://(?P<site>{domains})/(?P<slug>[a-zA-Z0-9]+)[.]?(?P<ext>[a-z]{{1,8}})?"
5353
TOKEN_RE = re.compile(r"[a-zA-Z0-9_-]{23,28}\.[a-zA-Z0-9_-]{6,7}\.[a-zA-Z0-9_-]{27}")
5454
PROSE_LOOKUP = {
@@ -264,21 +264,20 @@ async def find_badbins(self, message: discord.Message) -> None:
264264
await message.reply(msg, mention_author=False)
265265

266266
@commands.Cog.listener()
267-
async def on_papi_dpy_modlog(self, payload: ModLogType, /) -> None:
268-
moderation_payload = payload["payload"]
269-
moderation_event = core.DiscordPyModerationEvent(moderation_payload["moderation_event_type"])
267+
async def on_papi_dpy_modlog(self, payload: ModLogPayload, /) -> None:
268+
moderation_event = core.DiscordPyModerationEvent(payload["moderation_event_type"])
270269

271270
embed = discord.Embed(
272271
title=f"Discord.py Moderation Event: {moderation_event.name.title()}",
273272
colour=random_pastel_colour(),
274273
)
275274

276-
target_id = moderation_payload["target_id"]
275+
target_id = payload["target_id"]
277276
target = await self.bot.get_or_fetch_user(target_id)
278277

279-
moderation_reason = moderation_payload["reason"]
278+
moderation_reason = payload["reason"]
280279

281-
moderator_id = moderation_payload["author_id"]
280+
moderator_id = payload["author_id"]
282281
moderator = self.dpy_mod_cache.get(moderator_id) or await self.bot.get_or_fetch_user(
283282
moderator_id, cache=self.dpy_mod_cache
284283
)
@@ -301,7 +300,7 @@ async def on_papi_dpy_modlog(self, payload: ModLogType, /) -> None:
301300

302301
embed.description = moderator_format + target_format
303302

304-
when = datetime.datetime.fromisoformat(moderation_payload["event_time"])
303+
when = datetime.datetime.fromisoformat(payload["event_time"])
305304
embed.timestamp = when
306305

307306
guild = self.bot.get_guild(490948346773635102)

0 commit comments

Comments
 (0)