-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSponsorBoi.cs
137 lines (117 loc) · 4.3 KB
/
SponsorBoi.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
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
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Enums;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using Microsoft.Extensions.Logging;
using SponsorBoi.Commands;
namespace SponsorBoi
{
static class SponsorBoi
{
public const string APPLICATION_NAME = "SponsorBoi";
// Sets up a dummy client to use for logging
public static DiscordClient discordClient = new DiscordClient(new DiscordConfiguration { Token = "DUMMY_TOKEN", TokenType = TokenType.Bot, MinimumLogLevel = LogLevel.Debug });
private static SlashCommandsExtension commands = null;
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
public static string GetVersion()
{
Version version = Assembly.GetEntryAssembly()?.GetName().Version;
return version?.Major + "." + version?.Minor + "." + version?.Build + (version?.Revision == 0 ? "" : "-" + (char)(64 + version?.Revision ?? 0));
}
private static async Task MainAsync()
{
Logger.Log("Starting " + APPLICATION_NAME + " version " + GetVersion() + "...");
try
{
Initialize();
// Block this task until the program is closed.
await Task.Delay(-1);
}
catch (Exception e)
{
Logger.Fatal("Fatal error:\n" + e);
Console.ReadLine();
}
}
public static async void Initialize()
{
Logger.Log("Loading config \"" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "config.yml\"");
Config.LoadConfig();
// Check if bot token is unset
if (Config.botToken == "<add-token-here>" || string.IsNullOrWhiteSpace(Config.botToken))
{
Logger.Fatal("You need to set your bot token in the config and start the bot again.");
throw new ArgumentException("Invalid Discord bot token");
}
// Check if github token is unset
if (Config.githubToken == "<add-token-here>" || string.IsNullOrWhiteSpace(Config.githubToken))
{
Logger.Fatal("You need to set your Github personal access token in the config and start the bot again.");
throw new ArgumentException("Invalid Github personal access token");
}
// Database connection and setup
try
{
Logger.Log("Connecting to database...");
Database.Initialize();
}
catch (Exception e)
{
Logger.Fatal("Could not set up database tables, please confirm connection settings, status of the server and permissions of MySQL user. Error: " + e);
throw;
}
Logger.Log("Setting up Github API client...");
Github.Initialize();
Logger.Log("Setting up Discord client...");
// Checking log level
if (!Enum.TryParse(Config.logLevel, true, out LogLevel logLevel))
{
Logger.Log("Log level " + Config.logLevel + " invalid, using 'Information' instead.");
logLevel = LogLevel.Information;
}
// Setting up client configuration
DiscordConfiguration cfg = new DiscordConfiguration
{
Token = Config.botToken,
TokenType = TokenType.Bot,
MinimumLogLevel = logLevel,
AutoReconnect = true,
Intents = DiscordIntents.All
};
discordClient = new DiscordClient(cfg);
discordClient.UseInteractivity(new InteractivityConfiguration
{
AckPaginationButtons = true,
PaginationBehaviour = PaginationBehaviour.Ignore,
PaginationDeletion = PaginationDeletion.DeleteMessage,
Timeout = TimeSpan.FromMinutes(15)
});
Logger.Log("Registering commands...");
commands = discordClient.UseSlashCommands();
commands.RegisterCommands<AdminLinkCommand>();
commands.RegisterCommands<AdminUnlinkCommand>();
commands.RegisterCommands<LinkCommand>();
commands.RegisterCommands<RecheckCommand>();
commands.RegisterCommands<UnlinkCommand>();
commands.RegisterCommands<CreateSyncButtonCommand>();
Logger.Log("Hooking events...");
discordClient.Ready += EventHandler.OnReady;
discordClient.GuildAvailable += EventHandler.OnGuildAvailable;
discordClient.ClientErrored += EventHandler.OnClientError;
discordClient.GuildMemberAdded += EventHandler.OnGuildMemberAdded;
commands.SlashCommandErrored += EventHandler.OnCommandError;
discordClient.ComponentInteractionCreated += EventHandler.OnComponentInteractionCreated;
Logger.Log("Connecting to Discord...");
await discordClient.ConnectAsync();
RoleChecker.RunPeriodically();
}
}
}