Skip to content

Commit bcb88c0

Browse files
authored
Display dev in logging webhook (#43)
* lints -_- * Add config key for logging in dev mode * use get_or_fetch_user * fix: nitpicks
1 parent 1a9720c commit bcb88c0

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

config.template.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dsn = 'postgres://pythonistabot:pythonistabot@database:5432/pythonistabot' # ass
1818
[LOGGING]
1919
webhook_url = "" # optional
2020
webhook_avatar_url = "" # optional
21+
runner = 123456789 # optional: sets the webhook avatar url (will be overridden by the webhook_avatar_url attribute)
2122
level = 20
2223

2324
[SNEKBOX] # optional

modules/logging.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727

2828
import discord
2929
from discord.ext import commands, tasks
30-
from discord.utils import format_dt
30+
from discord.utils import MISSING, format_dt
3131

3232
import core
3333

3434

3535
class Logging(commands.Cog):
3636
def __init__(self, bot: core.Bot) -> None:
3737
self.bot = bot
38+
39+
self.user: discord.User | None = MISSING # none is for a failed fetch
40+
3841
if url := core.CONFIG["LOGGING"].get("webhook_url"):
3942
self.webhook = discord.Webhook.from_url(url, session=bot.session, client=bot)
4043
else:
@@ -64,9 +67,23 @@ async def logging_loop(self) -> None:
6467

6568
embed = to_log.__dict__.get("embed") or discord.utils.MISSING
6669

70+
avatar_url: str | None = core.CONFIG["LOGGING"].get("webhook_avatar_url")
71+
actor_name = "PythonistaBot Logging"
72+
73+
if not avatar_url and self.user is not None and "runner" in core.CONFIG["LOGGING"]:
74+
runner_id: int = core.CONFIG["LOGGING"]["runner"]
75+
try:
76+
user = self.user or self.bot.get_or_fetch_user(runner_id)
77+
except:
78+
self.user = user = None # This will tell us not to attempt again.
79+
80+
if user:
81+
avatar_url = user.display_avatar.url
82+
actor_name = f"Logging: Dev: {user.display_name}"
83+
6784
await self.webhook.send(
6885
message,
69-
username="PythonistaBot Logging",
86+
username=actor_name,
7087
avatar_url=core.CONFIG["LOGGING"].get("webhook_avatar_url"),
7188
embed=embed,
7289
)

types_/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Logging(TypedDict):
2020
webhook_url: NotRequired[str]
2121
webhook_avatar_url: NotRequired[str]
2222
level: int
23+
runner: NotRequired[int]
2324

2425

2526
class Snekbox(TypedDict):

0 commit comments

Comments
 (0)