-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry.py
More file actions
187 lines (159 loc) · 5.13 KB
/
Copy pathtelemetry.py
File metadata and controls
187 lines (159 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import traceback
from datetime import datetime, timezone
from typing import Iterable
import discord
MASTER_GUILD_ID = 1322854959686877185
MASTER_CHANNEL_ID = 1503394648763138088
MODULE_LOG_FIELD_MAP = {
"pomodoro": "pomodoro_logs_channel_id",
"afk": "afk_logs_channel_id",
"utilities": "utilities_logs_channel_id",
"music": "music_logs_channel_id",
}
def _fmt_server(guild: discord.Guild | None) -> str:
if not guild:
return "Unknown Server"
return f"{guild.name} ({guild.id})"
def _fmt_user(user: discord.abc.User | None) -> str:
if not user:
return "Unknown User"
return f"{user} ({user.id})"
async def get_master_log_channel(bot: discord.Client) -> discord.TextChannel | None:
channel = bot.get_channel(MASTER_CHANNEL_ID)
if isinstance(channel, discord.TextChannel):
return channel
guild = bot.get_guild(MASTER_GUILD_ID)
if guild:
fetched = guild.get_channel(MASTER_CHANNEL_ID)
if isinstance(fetched, discord.TextChannel):
return fetched
return None
async def send_master_log(
bot: discord.Client,
title: str,
description: str,
*,
color: int = 0x5865F2,
fields: Iterable[tuple[str, str, bool]] | None = None,
) -> None:
channel = await get_master_log_channel(bot)
if not channel:
return
embed = discord.Embed(
title=title,
description=description,
color=color,
timestamp=datetime.now(timezone.utc),
)
if fields:
for name, value, inline in fields:
embed.add_field(name=name, value=(value or "-")[:1024], inline=inline)
try:
await channel.send(embed=embed)
except Exception:
pass
async def send_activity_log(
bot: discord.Client,
*,
activity_type: str,
details: str,
module: str,
guild: discord.Guild | None = None,
user: discord.abc.User | None = None,
jump_url: str | None = None,
fields: Iterable[tuple[str, str, bool]] | None = None,
color: int = 0x5865F2,
) -> None:
ts = int(datetime.now(timezone.utc).timestamp())
base_fields: list[tuple[str, str, bool]] = [
("Server", _fmt_server(guild), False),
("User", _fmt_user(user), False),
("Activity Type", activity_type, True),
("Timestamp", f"<t:{ts}:F>", True),
]
if jump_url:
base_fields.append(("Reference", jump_url, False))
if fields:
base_fields.extend(list(fields))
await send_master_log(
bot,
title=f"{module} • {activity_type}",
description=details,
color=color,
fields=base_fields,
)
async def send_guild_module_log(
bot: discord.Client,
*,
guild: discord.Guild | None,
module: str,
title: str,
description: str,
fields: Iterable[tuple[str, str, bool]] | None = None,
color: int = 0x5865F2,
) -> None:
if not guild:
return
setting_key = MODULE_LOG_FIELD_MAP.get(module.lower())
if not setting_key:
return
try:
import database as db
settings = await db.get_guild_settings(guild.id)
channel_id = settings.get(setting_key)
if not channel_id:
return
channel = guild.get_channel(int(channel_id))
if not isinstance(channel, discord.TextChannel):
return
embed = discord.Embed(
title=title,
description=description,
color=color,
timestamp=datetime.now(timezone.utc),
)
if fields:
for name, value, inline in fields:
embed.add_field(name=name, value=(value or "-")[:1024], inline=inline)
await channel.send(embed=embed)
except Exception:
pass
async def send_game_telemetry(
bot: discord.Client,
*,
guild: discord.Guild | None,
game_name: str,
result: str,
players: Iterable[tuple[str, int, str]],
) -> None:
guild_name = guild.name if guild else "Unknown Server"
guild_id = guild.id if guild else 0
player_lines = [f"• {name} ({user_id})" for name, user_id, _ in players]
point_lines = [f"• {name}: {delta}" for name, _, delta in players]
await send_master_log(
bot,
f"Game Result • {game_name}",
"Game activity recorded.",
fields=[
("Game", game_name, True),
("Result", result, True),
("Server", f"{guild_name} ({guild_id})", False),
("Players", "\n".join(player_lines) or "-", False),
("Point Changes", "\n".join(point_lines) or "-", False),
],
)
async def log_exception(
bot: discord.Client,
*,
title: str,
error: Exception,
context: str,
fields: Iterable[tuple[str, str, bool]] | None = None,
) -> None:
trace = "".join(traceback.format_exception(type(error), error, error.__traceback__))
if len(trace) > 3500:
trace = trace[:3500] + "\n... (truncated)"
extra_fields = list(fields or [])
extra_fields.append(("Context", context, False))
extra_fields.append(("Traceback", f"```py\n{trace}\n```", False))
await send_master_log(bot, title, f"{type(error).__name__}: {error}", color=0xED4245, fields=extra_fields)