-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBot.cs
67 lines (60 loc) · 2.14 KB
/
Bot.cs
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
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Hosting;
using QuickEdit.Commands;
using QuickEdit.Logger;
using Serilog;
namespace QuickEdit;
internal sealed class Bot(DiscordSocketClient client, Config.DiscordConfig discordConfig, IHostApplicationLifetime appLifetime) : IHostedService
{
private readonly DiscordSocketClient _client = client;
private readonly Config.DiscordConfig _discordConfig = discordConfig;
private readonly IHostApplicationLifetime _appLifetime = appLifetime;
public async Task StartAsync(CancellationToken cancellationToken)
{
_client.Log += AutoLog.LogMessage;
// Try-catch ValidateToken since LoginAsync doesn't throw exceptions, they just catch them
// So there's no way to know if the token is invalid without checking it first (or is there?)
// Also this is separate from the other try-catch to make sure it's the token that's invalid
try
{
TokenUtils.ValidateToken(TokenType.Bot, _discordConfig.Token);
}
catch (ArgumentException e)
{
Log.Fatal("{e}", e.Message);
Environment.ExitCode = 1;
_appLifetime.StopApplication();
return; // The app would normally continue for a short amount of time before stopping
}
// Most of the exceptions are caught by the library :( [maybe not idk i'm writing this at 2am]
// This means that the program will log stuff in an ugly way and NOT stop the program on fatal errors
try
{
// The token is already validated, so there's no need to validate it again
await _client.LoginAsync(TokenType.Bot, _discordConfig.Token, validateToken: false);
await _client.StartAsync();
}
catch (Exception e)
{
Log.Fatal("Failed to start the bot: {e}", e);
Environment.ExitCode = 1;
_appLifetime.StopApplication();
return;
}
// Custom activities use a different method
if (_discordConfig.StatusType == ActivityType.CustomStatus)
{
await _client.SetCustomStatusAsync(_discordConfig.Status);
}
else
{
await _client.SetGameAsync(_discordConfig.Status, null, _discordConfig.StatusType);
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _client.LogoutAsync();
await _client.StopAsync();
}
}