Updated performance in plugin loading
This commit is contained in:
140
DiscordBotCore/Bot/DiscordBotApplication.cs
Normal file
140
DiscordBotCore/Bot/DiscordBotApplication.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
|
||||
namespace DiscordBotCore.Bot;
|
||||
|
||||
public class DiscordBotApplication
|
||||
{
|
||||
/// <summary>
|
||||
/// The bot prefix
|
||||
/// </summary>
|
||||
private readonly string _BotPrefix;
|
||||
|
||||
/// <summary>
|
||||
/// The bot token
|
||||
/// </summary>
|
||||
private readonly string _BotToken;
|
||||
|
||||
/// <summary>
|
||||
/// The bot client
|
||||
/// </summary>
|
||||
public DiscordSocketClient Client;
|
||||
|
||||
/// <summary>
|
||||
/// The bot command handler
|
||||
/// </summary>
|
||||
private CommandHandler _CommandServiceHandler;
|
||||
|
||||
/// <summary>
|
||||
/// The command service
|
||||
/// </summary>
|
||||
private CommandService _Service;
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the bot is ready
|
||||
/// </summary>
|
||||
/// <value> true if the bot is ready, otherwise false </value>
|
||||
private bool IsReady { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The main Boot constructor
|
||||
/// </summary>
|
||||
/// <param name="botToken">The bot token</param>
|
||||
/// <param name="botPrefix">The bot prefix</param>
|
||||
public DiscordBotApplication(string botToken, string botPrefix)
|
||||
{
|
||||
this._BotPrefix = botPrefix;
|
||||
this._BotToken = botToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The start method for the bot. This method is used to load the bot
|
||||
/// </summary>
|
||||
public async Task StartAsync()
|
||||
{
|
||||
var config = new DiscordSocketConfig
|
||||
{
|
||||
AlwaysDownloadUsers = true,
|
||||
|
||||
//Disable system clock checkup (for responses at slash commands)
|
||||
UseInteractionSnowflakeDate = false,
|
||||
GatewayIntents = GatewayIntents.All
|
||||
};
|
||||
|
||||
Client = new DiscordSocketClient(config);
|
||||
_Service = new CommandService();
|
||||
|
||||
Client.Log += Log;
|
||||
Client.LoggedIn += LoggedIn;
|
||||
Client.Ready += Ready;
|
||||
Client.Disconnected += Client_Disconnected;
|
||||
|
||||
await Client.LoginAsync(TokenType.Bot, _BotToken);
|
||||
|
||||
await Client.StartAsync();
|
||||
|
||||
_CommandServiceHandler = new CommandHandler(Client, _Service, _BotPrefix);
|
||||
|
||||
await _CommandServiceHandler.InstallCommandsAsync();
|
||||
|
||||
Application.CurrentApplication.DiscordBotClient = this;
|
||||
|
||||
// wait for the bot to be ready
|
||||
while (!IsReady)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Client_Disconnected(Exception arg)
|
||||
{
|
||||
if (arg.Message.Contains("401"))
|
||||
{
|
||||
Application.CurrentApplication.ApplicationEnvironmentVariables.Remove("token");
|
||||
Application.Logger.Log("The token is invalid.", this, LogType.Critical);
|
||||
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
|
||||
await Task.Delay(3000);
|
||||
|
||||
Process.Start(Environment.ProcessPath);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private Task Ready()
|
||||
{
|
||||
IsReady = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task LoggedIn()
|
||||
{
|
||||
Application.Logger.Log("Successfully Logged In", this);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task Log(LogMessage message)
|
||||
{
|
||||
switch (message.Severity)
|
||||
{
|
||||
case LogSeverity.Error:
|
||||
case LogSeverity.Critical:
|
||||
Application.Logger.Log(message.Message, this, LogType.Error);
|
||||
break;
|
||||
|
||||
case LogSeverity.Info:
|
||||
case LogSeverity.Debug:
|
||||
Application.Logger.Log(message.Message, this, LogType.Info);
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user