-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cs
More file actions
76 lines (63 loc) · 2.02 KB
/
Bot.cs
File metadata and controls
76 lines (63 loc) · 2.02 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
using Discord;
using Discord.WebSocket;
using GeneralPurposeLib;
using SerbleBot.Commands;
using SerbleBot.EventHandlers;
namespace SerbleBot;
public class Bot {
private static DiscordSocketClient? _client;
public static bool IsMe(SocketUser user) {
return user.Id == _client!.CurrentUser.Id;
}
public async Task Run() {
_client = new DiscordSocketClient();
// Events
_client.Log += Log;
_client.Ready += ClientReady;
_client.SlashCommandExecuted += SlashCommandHandler;
_client.MessageReceived += MessageHandler.OnMessage;
await _client.LoginAsync(TokenType.Bot, Program.Config!["token"]);
await _client.StartAsync();
// Block this task until the program is closed.
await Task.Delay(-1);
Logger.Warn("Bot is shutting down");
}
private Task SlashCommandHandler(SocketSlashCommand command) {
try {
CommandManager.Invoke(command, _client!);
}
catch (Exception e) {
Logger.Error(e);
}
return Task.CompletedTask;
}
private async Task ClientReady() {
Logger.Debug("Client ready");
await _client!.SetGameAsync("serble.net");
}
private static Task Log(LogMessage msg) {
switch (msg.Severity) {
case LogSeverity.Critical:
Logger.Error(msg);
break;
case LogSeverity.Error:
Logger.Error(msg);
break;
case LogSeverity.Warning:
Logger.Warn(msg);
break;
case LogSeverity.Info:
Logger.Info(msg);
break;
case LogSeverity.Verbose:
Logger.Debug(msg);
break;
case LogSeverity.Debug:
Logger.Debug(msg);
break;
default:
throw new ArgumentOutOfRangeException(nameof(msg));
}
return Task.CompletedTask;
}
}