Updated a file

This commit is contained in:
EmotionChild 2022-04-18 22:59:52 +12:00
parent 8408ddce01
commit 94324f4d4a
No known key found for this signature in database
GPG key ID: 23DC06AC32786520

View file

@ -9,155 +9,155 @@ using SupportChild.Commands;
namespace SupportChild namespace SupportChild
{ {
internal class SupportChild internal class SupportChild
{ {
internal static SupportChild instance; internal static SupportChild instance;
private DiscordClient discordClient = null; private DiscordClient discordClient = null;
private CommandsNextExtension commands = null; private CommandsNextExtension commands = null;
private EventHandler eventHandler; private EventHandler eventHandler;
static void Main(string[] args) static void Main(string[] args)
{ {
new SupportChild().MainAsync().GetAwaiter().GetResult(); new SupportChild().MainAsync().GetAwaiter().GetResult();
} }
private async Task MainAsync() private async Task MainAsync()
{ {
instance = this; instance = this;
Console.WriteLine("Starting SupportChild version " + GetVersion() + "..."); Console.WriteLine("Starting SupportChild version " + GetVersion() + "...");
try try
{ {
Reload(); this.Reload();
// Block this task until the program is closed. // Block this task until the program is closed.
await Task.Delay(-1); await Task.Delay(-1);
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Fatal error:"); Console.WriteLine("Fatal error:");
Console.WriteLine(e); Console.WriteLine(e);
Console.ReadLine(); Console.ReadLine();
} }
} }
public static DiscordClient GetClient() public static DiscordClient GetClient()
{ {
return instance.discordClient; return instance.discordClient;
} }
public static string GetVersion() public static string GetVersion()
{ {
Version version = Assembly.GetEntryAssembly()?.GetName().Version; Version version = Assembly.GetEntryAssembly()?.GetName().Version;
return version?.Major + "." + version?.Minor + "." + version?.Build + (version?.Revision == 0 ? "" : "-" + (char)(64 + version?.Revision ?? 0)); return version?.Major + "." + version?.Minor + "." + version?.Build + (version?.Revision == 0 ? "" : "-" + (char)(64 + version?.Revision ?? 0));
} }
public async void Reload() public async void Reload()
{ {
if (discordClient != null) if (this.discordClient != null)
{ {
await discordClient.DisconnectAsync(); await this.discordClient.DisconnectAsync();
discordClient.Dispose(); this.discordClient.Dispose();
Console.WriteLine("Discord client disconnected."); Console.WriteLine("Discord client disconnected.");
} }
Console.WriteLine("Loading config \"" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "config.yml\""); Console.WriteLine("Loading config \"" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "config.yml\"");
Config.LoadConfig(); Config.LoadConfig();
// Check if token is unset // Check if token is unset
if (Config.token == "<add-token-here>" || Config.token == "") if (Config.token == "<add-token-here>" || Config.token == "")
{ {
Console.WriteLine("You need to set your bot token in the config and start the bot again."); Console.WriteLine("You need to set your bot token in the config and start the bot again.");
throw new ArgumentException("Invalid Discord bot token"); throw new ArgumentException("Invalid Discord bot token");
} }
// Database connection and setup // Database connection and setup
try try
{ {
Console.WriteLine("Connecting to database... (" + Config.hostName + ":" + Config.port + ")"); Console.WriteLine("Connecting to database... (" + Config.hostName + ":" + Config.port + ")");
Database.SetConnectionString(Config.hostName, Config.port, Config.database, Config.username, Config.password); Database.SetConnectionString(Config.hostName, Config.port, Config.database, Config.username, Config.password);
Database.SetupTables(); Database.SetupTables();
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("Could not set up database tables, please confirm connection settings, status of the server and permissions of MySQL user. Error: " + e); Console.WriteLine("Could not set up database tables, please confirm connection settings, status of the server and permissions of MySQL user. Error: " + e);
throw; throw;
} }
Console.WriteLine("Setting up Discord client..."); Console.WriteLine("Setting up Discord client...");
// Checking log level // Checking log level
if (!Enum.TryParse(Config.logLevel, true, out LogLevel logLevel)) if (!Enum.TryParse(Config.logLevel, true, out LogLevel logLevel))
{ {
Console.WriteLine("Log level '" + Config.logLevel + "' invalid, using 'Information' instead."); Console.WriteLine("Log level '" + Config.logLevel + "' invalid, using 'Information' instead.");
logLevel = LogLevel.Information; logLevel = LogLevel.Information;
} }
// Setting up client configuration // Setting up client configuration
DiscordConfiguration cfg = new DiscordConfiguration DiscordConfiguration cfg = new DiscordConfiguration
{ {
Token = Config.token, Token = Config.token,
TokenType = TokenType.Bot, TokenType = TokenType.Bot,
MinimumLogLevel = logLevel, MinimumLogLevel = logLevel,
AutoReconnect = true, AutoReconnect = true,
Intents = DiscordIntents.All Intents = DiscordIntents.All
}; };
discordClient = new DiscordClient(cfg); this.discordClient = new DiscordClient(cfg);
eventHandler = new EventHandler(discordClient); this.eventHandler = new EventHandler(this.discordClient);
Console.WriteLine("Hooking events..."); Console.WriteLine("Hooking events...");
discordClient.Ready += eventHandler.OnReady; this.discordClient.Ready += this.eventHandler.OnReady;
discordClient.GuildAvailable += eventHandler.OnGuildAvailable; this.discordClient.GuildAvailable += this.eventHandler.OnGuildAvailable;
discordClient.ClientErrored += eventHandler.OnClientError; this.discordClient.ClientErrored += this.eventHandler.OnClientError;
discordClient.MessageCreated += eventHandler.OnMessageCreated; this.discordClient.MessageCreated += this.eventHandler.OnMessageCreated;
discordClient.GuildMemberAdded += eventHandler.OnMemberAdded; this.discordClient.GuildMemberAdded += this.eventHandler.OnMemberAdded;
discordClient.GuildMemberRemoved += eventHandler.OnMemberRemoved; this.discordClient.GuildMemberRemoved += this.eventHandler.OnMemberRemoved;
if (Config.reactionMessage != 0) if (Config.reactionMessage != 0)
{ {
discordClient.MessageReactionAdded += eventHandler.OnReactionAdded; this.discordClient.MessageReactionAdded += this.eventHandler.OnReactionAdded;
} }
Console.WriteLine("Registering commands..."); Console.WriteLine("Registering commands...");
commands = discordClient.UseCommandsNext(new CommandsNextConfiguration commands = discordClient.UseCommandsNext(new CommandsNextConfiguration
{ {
StringPrefixes = new[] { Config.prefix } StringPrefixes = new[] { Config.prefix }
}); });
this.commands.RegisterCommands<AddCommand>(); this.commands.RegisterCommands<AddCommand>();
this.commands.RegisterCommands<AddMessageCommand>(); this.commands.RegisterCommands<AddMessageCommand>();
this.commands.RegisterCommands<AddStaffCommand>(); this.commands.RegisterCommands<AddStaffCommand>();
this.commands.RegisterCommands<AssignCommand>(); this.commands.RegisterCommands<AssignCommand>();
this.commands.RegisterCommands<BlacklistCommand>(); this.commands.RegisterCommands<BlacklistCommand>();
this.commands.RegisterCommands<CloseCommand>(); this.commands.RegisterCommands<CloseCommand>();
this.commands.RegisterCommands<ListAssignedCommand>(); this.commands.RegisterCommands<ListAssignedCommand>();
this.commands.RegisterCommands<ListCommand>(); this.commands.RegisterCommands<ListCommand>();
this.commands.RegisterCommands<ListOldestCommand>(); this.commands.RegisterCommands<ListOldestCommand>();
this.commands.RegisterCommands<ListUnassignedCommand>(); this.commands.RegisterCommands<ListUnassignedCommand>();
this.commands.RegisterCommands<MoveCommand>(); this.commands.RegisterCommands<MoveCommand>();
this.commands.RegisterCommands<NewCommand>(); this.commands.RegisterCommands<NewCommand>();
this.commands.RegisterCommands<RandomAssignCommand>(); this.commands.RegisterCommands<RandomAssignCommand>();
this.commands.RegisterCommands<ReloadCommand>(); this.commands.RegisterCommands<ReloadCommand>();
this.commands.RegisterCommands<RemoveMessageCommand>(); this.commands.RegisterCommands<RemoveMessageCommand>();
this.commands.RegisterCommands<RemoveStaffCommand>(); this.commands.RegisterCommands<RemoveStaffCommand>();
this.commands.RegisterCommands<SayCommand>(); this.commands.RegisterCommands<SayCommand>();
this.commands.RegisterCommands<SetSummaryCommand>(); this.commands.RegisterCommands<SetSummaryCommand>();
this.commands.RegisterCommands<SetTicketCommand>(); this.commands.RegisterCommands<SetTicketCommand>();
this.commands.RegisterCommands<StatusCommand>(); this.commands.RegisterCommands<StatusCommand>();
this.commands.RegisterCommands<SummaryCommand>(); this.commands.RegisterCommands<SummaryCommand>();
this.commands.RegisterCommands<ToggleActiveCommand>(); this.commands.RegisterCommands<ToggleActiveCommand>();
this.commands.RegisterCommands<TranscriptCommand>(); this.commands.RegisterCommands<TranscriptCommand>();
this.commands.RegisterCommands<UnassignCommand>(); this.commands.RegisterCommands<UnassignCommand>();
this.commands.RegisterCommands<UnblacklistCommand>(); this.commands.RegisterCommands<UnblacklistCommand>();
this.commands.RegisterCommands<UnsetTicketCommand>(); this.commands.RegisterCommands<UnsetTicketCommand>();
Console.WriteLine("Hooking command events..."); Console.WriteLine("Hooking command events...");
this.commands.CommandErrored += eventHandler.OnCommandError; this.commands.CommandErrored += this.eventHandler.OnCommandError;
Console.WriteLine("Connecting to Discord..."); Console.WriteLine("Connecting to Discord...");
await discordClient.ConnectAsync(); await this.discordClient.ConnectAsync();
} }
} }
} }