-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cs
More file actions
433 lines (367 loc) · 18.9 KB
/
Copy pathBot.cs
File metadata and controls
433 lines (367 loc) · 18.9 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using Discord;
using Discord.WebSocket;
namespace WisBot;
public class Bot(Terminal terminal) {
private string token = null!;
private DiscordSocketClient client = null!;
private VoiceRecorder voiceRecorder = new(terminal);
private WelcomeService welcomeService = new(terminal);
private UserVoiceActivityTracker voiceActivityTracker = new(terminal);
private VoiceStatsService voiceStatsService = new(terminal);
private WisLlmService wisLlmService = new(terminal);
private UploadService uploadService = new(terminal);
private ReminderService? reminderService;
private VoiceNotificationService? voiceNotifyService;
private StatusService? statusService;
private WebService? webService;
private Dictionary<string, Func<SocketSlashCommand, Task>> commands = [];
public async Task StartBot() {
var config = new DiscordSocketConfig {
GatewayIntents =
GatewayIntents.AllUnprivileged
| GatewayIntents.MessageContent
| GatewayIntents.GuildMessages
| GatewayIntents.GuildMembers,
};
client = new DiscordSocketClient(config);
reminderService = new ReminderService(terminal, client);
voiceNotifyService = new VoiceNotificationService(terminal, client);
statusService = new StatusService(terminal, client);
webService = new WebService(terminal, client, uploadService);
client.Log += OnLog;
client.MessageUpdated += OnMessageUpdated;
client.MessageReceived += OnMessageReceived;
client.Ready += OnReady;
client.UserJoined += welcomeService.OnUserJoined;
client.UserVoiceStateUpdated += voiceNotifyService.OnVoiceStateUpdated;
client.UserVoiceStateUpdated += voiceActivityTracker.OnVoiceStateUpdated;
client.UserIsTyping += OnUserIsTyping;
client.SlashCommandExecuted += OnSlashCommandExecuted;
await InitBot();
await Database.Initialize(); // before the web host — /u/{id} routes query the uploads table
await webService.Start(); // up immediately (503 until connected), so orchestration can poll
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
}
/// Graceful shutdown: stop background loops, the web host, and the Discord connection.
public async Task StopBot() {
await Log("Stopping bot...");
reminderService?.Stop();
uploadService.StopRetention();
voiceRecorder.StopRetention();
wisLlmService.StopRetention();
voiceActivityTracker.StopRetention();
if (webService is not null) await webService.Stop();
if (client is not null) {
await client.StopAsync();
await client.LogoutAsync();
}
await Log("Bot stopped");
}
private async Task InitBot() {
Config.Load();
if (string.IsNullOrWhiteSpace(Config.DiscordToken))
throw new InvalidOperationException(
"Discord token not found. Add it to discord.key or set DISCORD_TOKEN_WISBOT in .env");
if (Config.GuildId == 0)
throw new InvalidOperationException(
"WISBOT_GUILD_ID not set. Set the Discord guild the bot serves via environment or .env");
token = Config.DiscordToken;
await terminal.AddLine("[Bot] Config loaded");
}
private async Task OnUserIsTyping(Cacheable<IUser, ulong> cachedUser, Cacheable<IMessageChannel, ulong> cachedChannel) {
var user = await cachedUser.GetOrDownloadAsync();
var channel = await cachedChannel.GetOrDownloadAsync();
await Log($"{user} is typing in {channel}...");
}
private async Task OnMessageReceived(SocketMessage message) {
if (message.Author.IsBot) {
return;
}
// Log metadata only — never raw message content. Content lands in stdout /
// the container log pipeline (persisting beyond Discord's controls) and the bot
// holds the privileged MessageContent intent, so this would exfiltrate all guild
// conversation. (security audit M-1)
await Log($"[{message.Channel.Name}] {message.Author}: <{message.Content.Length} chars>");
if (message.Content.StartsWith("!")) {
if (message.Content.StartsWith("!eatdeeznuts")) {
if (Random.Shared.NextDouble() < 0.1) {
_ = message.Channel.SendMessageAsync("Sure, I could go for some nuts.");
return;
}
_ = message.Channel.SendMessageAsync("No thanks, I'm good.");
return;
}
_ = message.Channel.SendMessageAsync("Command detected, but commands are not implemented yet.");
return;
}
}
private async Task OnReady() {
await Log("Bot is Ready!!!");
await reminderService!.Start();
uploadService.StartRetention();
voiceRecorder.StartRetention();
wisLlmService.StartRetention();
voiceActivityTracker.StartRetention();
await AddCommandsIfNotExist();
commands = new Dictionary<string, Func<SocketSlashCommand, Task>> {
["remind"] = cmd => reminderService!.HandleRemindCommand(cmd),
["status"] = cmd => statusService!.HandleCommand(cmd),
["voicestats"] = cmd => voiceStatsService.HandleCommand(cmd),
["notify"] = cmd => voiceNotifyService!.HandleNotifyCommand(cmd),
["recording"] = cmd => voiceRecorder.HandleRecordingCommand(cmd),
["upload"] = cmd => uploadService.HandleUploadCommand(cmd),
["wisllm"] = HandleWisLlmCommand,
};
}
public async Task AddCommandsIfNotExist() {
await Log("Registering slash commands");
var guild = client.GetGuild(Config.GuildId);
if (guild == null)
throw new InvalidOperationException(
$"Configured guild '{Config.GuildId}' was not found. Ensure the bot is a member of that guild.");
var existingGuildCommands = await guild.GetApplicationCommandsAsync();
// Uhstray guild commands
if (!existingGuildCommands.Any(cmd => cmd.Name == "recording")) {
var command = new SlashCommandBuilder()
.WithName("recording")
.WithDescription("Control voice channel recording")
// Records other people's audio — gate to members who can move/manage
// voice (server-side rechecked in the handler; this is the UI hint).
.WithDefaultMemberPermissions(GuildPermission.MoveMembers)
.AddOption(new SlashCommandOptionBuilder()
.WithName("action")
.WithDescription("Start or stop recording")
.WithRequired(true)
.WithType(ApplicationCommandOptionType.String)
.AddChoice("Start", "start")
.AddChoice("Stop", "stop")
)
.AddOption(new SlashCommandOptionBuilder()
.WithName("sendfile")
.WithDescription("Send files directly in Discord chat (default: false)")
.WithRequired(false)
.WithType(ApplicationCommandOptionType.Boolean)
)
.AddOption(new SlashCommandOptionBuilder()
.WithName("mergeaudio")
.WithDescription("Merge all audio sources into one file (default: false)")
.WithRequired(false)
.WithType(ApplicationCommandOptionType.Boolean)
)
.Build();
await guild.CreateApplicationCommandAsync(command);
await Log($"Registered guild slash command: /{command.Name} in '{guild.Name}'");
}
if (!existingGuildCommands.Any(cmd => cmd.Name == "remind")) {
var command = new SlashCommandBuilder()
.WithName("remind")
.WithDescription("Set a reminder — bot will DM you when time is up")
.AddOption(new SlashCommandOptionBuilder()
.WithName("when")
.WithDescription("How long from now, e.g. 30m, 2h, 1d, 1h30m")
.WithRequired(true)
.WithType(ApplicationCommandOptionType.String)
)
.AddOption(new SlashCommandOptionBuilder()
.WithName("message")
.WithDescription("What to remind you about")
.WithRequired(true)
.WithType(ApplicationCommandOptionType.String)
)
.Build();
await guild.CreateApplicationCommandAsync(command);
await Log($"Registered guild slash command: /{command.Name} in '{guild.Name}'");
}
if (!existingGuildCommands.Any(cmd => cmd.Name == "status")) {
var command = new SlashCommandBuilder()
.WithName("status")
.WithDescription("Show bot health: uptime, latency, memory, CPU, and more")
.Build();
await guild.CreateApplicationCommandAsync(command);
await Log($"Registered guild slash command: /{command.Name} in '{guild.Name}'");
}
if (!existingGuildCommands.Any(cmd => cmd.Name == "voicestats")) {
var command = new SlashCommandBuilder()
.WithName("voicestats")
.WithDescription("Show voice channel stats for a user")
.AddOption(new SlashCommandOptionBuilder()
.WithName("user")
.WithDescription("The user to look up")
.WithRequired(true)
.WithType(ApplicationCommandOptionType.User)
)
.Build();
await guild.CreateApplicationCommandAsync(command);
await Log($"Registered guild slash command: /{command.Name} in '{guild.Name}'");
}
if (!existingGuildCommands.Any(cmd => cmd.Name == "notify")) {
var command = new SlashCommandBuilder()
.WithName("notify")
.WithDescription("DM me once when a user joins a voice channel")
.AddOption(new SlashCommandOptionBuilder()
.WithName("user")
.WithDescription("The user to watch for")
.WithRequired(true)
.WithType(ApplicationCommandOptionType.User)
)
.Build();
await guild.CreateApplicationCommandAsync(command);
await Log($"Registered guild slash command: /{command.Name} in '{guild.Name}'");
}
if (Config.UploadEnabled && !existingGuildCommands.Any(cmd => cmd.Name == "upload")) {
var command = new SlashCommandBuilder()
.WithName("upload")
.WithDescription("Get a private link to upload a large file (bypasses Discord's size limit)")
.Build();
await guild.CreateApplicationCommandAsync(command);
await Log($"Registered guild slash command: /{command.Name} in '{guild.Name}'");
}
// Global commands
var existingGlobalCommands = await client.GetGlobalApplicationCommandsAsync();
// Replace old single-option wisllm with subcommand version if needed
var existingWisllm = existingGlobalCommands.FirstOrDefault(cmd => cmd.Name == "wisllm");
bool wisllmNeedsUpdate = existingWisllm == null ||
!existingWisllm.Options.Any(o => o.Type == ApplicationCommandOptionType.SubCommand);
if (wisllmNeedsUpdate) {
if (existingWisllm != null) {
await existingWisllm.DeleteAsync();
await Log("Deleted outdated /wisllm global command — replacing with subcommand version");
}
var command = new SlashCommandBuilder()
.WithName("wisllm")
.WithDescription("WisLLM — AI assistant provided by Uhstray.io")
.AddOption(new SlashCommandOptionBuilder()
.WithName("ask")
.WithDescription("Ask WisLLM a question")
.WithType(ApplicationCommandOptionType.SubCommand)
.AddOption("prompt", ApplicationCommandOptionType.String, "Your question", isRequired: true)
.AddOption("model", ApplicationCommandOptionType.String, "Model to use (default from config)", isRequired: false)
)
.AddOption(new SlashCommandOptionBuilder()
.WithName("clear")
.WithDescription("Start a fresh conversation session")
.WithType(ApplicationCommandOptionType.SubCommand)
)
.AddOption(new SlashCommandOptionBuilder()
.WithName("compact")
.WithDescription("Summarise the current session into a single context and continue from it")
.WithType(ApplicationCommandOptionType.SubCommand)
)
.Build();
await client.CreateGlobalApplicationCommandAsync(command);
await Log($"Registered global slash command: /{command.Name} (ask / clear / compact)");
}
}
private async Task HandleWisLlmCommand(SocketSlashCommand command) {
var sub = command.Data.Options.First().Name;
switch (sub) {
case "ask": await wisLlmService.HandleAskCommand(command); break;
case "clear": await wisLlmService.HandleClearCommand(command); break;
case "compact": await wisLlmService.HandleCompactCommand(command); break;
}
}
// Central per-command authorization (audit L-9). Commands not listed require no
// special permission. Administrator always passes. This is the systemic gate at the
// router; high-risk handlers (e.g. recording) ALSO recheck server-side as a backstop.
private static readonly Dictionary<string, GuildPermission> commandPermissions = new() {
["recording"] = GuildPermission.MoveMembers,
};
private async Task OnSlashCommandExecuted(SocketSlashCommand command) {
await terminal.AddLine($"[Bot] /{command.CommandName} by {command.User.Username}");
if (commandPermissions.TryGetValue(command.CommandName, out var required) && !Authorized(command.User, required)) {
await command.RespondAsync($"You don't have permission to use /{command.CommandName}.", ephemeral: true);
await terminal.AddLine($"[Bot] Denied /{command.CommandName} for {command.User.Username} (needs {required})", LogLevel.Warn);
return;
}
if (commands.TryGetValue(command.CommandName, out var handler))
await handler(command);
else
await terminal.AddLine($"[Bot] Unknown command: {command.CommandName}", LogLevel.Warn);
}
/// True if the user holds the required guild permission (Administrator implies all).
/// A non-guild context (DM) can't satisfy a guild-permission requirement.
private static bool Authorized(SocketUser user, GuildPermission required) =>
user is SocketGuildUser member
&& (member.GuildPermissions.Administrator || member.GuildPermissions.Has(required));
public async Task RemoveAllCommands() {
await Log("Removing all existing commands...");
try {
// Remove global commands
await Log("Removing global commands");
var globalCommands = await client.GetGlobalApplicationCommandsAsync();
foreach (var command in globalCommands) {
await command.DeleteAsync();
await Log($" Deleted global command: {command.Name}");
await Task.Delay(100);
}
// Remove guild-specific commands for the configured guild
await Log($"Removing guild commands for guild {Config.GuildId}");
var guild = client.GetGuild(Config.GuildId);
if (guild != null) {
var guildCommands = await guild.GetApplicationCommandsAsync();
foreach (var command in guildCommands) {
await command.DeleteAsync();
await Log($" Deleted guild command: {command.Name}");
await Task.Delay(100);
}
}
// Optionally: Remove commands from all guilds the bot is in
await Log("Removing commands from all guilds");
foreach (var g in client.Guilds) {
var guildCmds = await g.GetApplicationCommandsAsync();
foreach (var command in guildCmds) {
await command.DeleteAsync();
await Log($" Deleted command '{command.Name}' from guild {g.Name}");
await Task.Delay(100);
}
}
await Log("Successfully removed all commands!");
} catch (Exception ex) {
await Log($"Error removing commands: {ex.Message}", LogLevel.Error);
}
}
/// Terminal command: joins a hardcoded voice channel, records for 15 seconds, then stops and saves.
public async Task TestRecord() {
ulong testGuildId = Config.TestGuildId;
ulong testChannelId = Config.TestVoiceChannelId;
if (testChannelId == 0) {
await Log("❌ TestRecord: WISBOT_TEST_VOICE_CHANNEL_ID is not set.");
return;
}
var guild = client.GetGuild(testGuildId);
if (guild == null) {
await Log("❌ TestRecord: Could not find guild");
return;
}
var channel = guild.GetVoiceChannel(testChannelId);
if (channel == null) {
await Log("❌ TestRecord: Could not find voice channel");
return;
}
await Log($"🎙️ TestRecord: Joining {channel.Name} in {guild.Name}...");
var joinResult = await voiceRecorder.JoinAndRecordChannel(channel);
await Log($"🎙️ TestRecord: {joinResult}");
await Log("🎙️ TestRecord: Recording for 15 seconds...");
await Task.Delay(TimeSpan.FromSeconds(15));
await Log("🎙️ TestRecord: Stopping recording...");
var files = await voiceRecorder.StopRecordingAndSave();
if (files.Count > 0) {
await Log($"🎙️ TestRecord: Complete! Saved {files.Count} file(s):");
foreach (var file in files) {
await Log($" 📁 {file}");
}
} else {
await Log("🎙️ TestRecord: Complete, but no audio was captured.");
}
}
private async Task OnMessageUpdated(Cacheable<IMessage, ulong> before, SocketMessage after, ISocketMessageChannel channel) {
// Metadata only — see OnMessageReceived (M-1). Don't log before/after content.
await Log($"Message from {after.Author.Username} edited in channel {channel.Name}");
}
private async Task OnLog(LogMessage msg) {
await Log(msg.ToString());
}
private async Task Log(string msg, LogLevel level = LogLevel.Info)
=> await terminal.AddLine($"[Discord] {msg}", level);
}