Add project files.

This commit is contained in:
Wizzy69
2022-01-07 20:26:10 +02:00
parent 8b8d4c7147
commit 84ee5c6235
53 changed files with 3544 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Threading.Tasks;
using static PluginManager.Others.Functions;
namespace PluginManager.Core
{
internal class Boot
{
private readonly string botPrefix;
private readonly string botToken;
private bool isReady = false;
public DiscordSocketClient? client;
private CommandHandler? commandServiceHandler;
private CommandService? service;
public Boot(string botToken, string botPrefix)
{
this.botPrefix = botPrefix;
this.botToken = botToken;
}
public async Task Awake()
{
client = new DiscordSocketClient();
service = new CommandService();
CommonTasks();
await client.LoginAsync(TokenType.Bot, botToken);
await client.StartAsync();
commandServiceHandler = new CommandHandler(client, service, botPrefix);
await commandServiceHandler.InstallCommandsAsync();
while (!isReady) ;
}
public async Task ShutDown()
{
if (client == null) return;
await client.StopAsync();
}
private void CommonTasks()
{
if (client == null)
return;
client.LoggedOut += Client_LoggedOut;
client.Log += Log;
client.LoggedIn += LoggedIn;
client.Ready += Ready;
}
private Task Client_LoggedOut()
{
WriteLogFile("Successfully Logged Out");
Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !"));
return Task.CompletedTask;
}
private Task Ready()
{
Console.Title = "ONLINE";
isReady = true;
return Task.CompletedTask;
}
private Task LoggedIn()
{
Console.Title = "CONNECTED";
WriteLogFile("The bot has been logged in at " + DateTime.Now.ToShortDateString() + " (" +
DateTime.Now.ToShortTimeString() + ")");
return Task.CompletedTask;
}
private Task Log(LogMessage message)
{
switch (message.Severity)
{
case LogSeverity.Error:
case LogSeverity.Critical:
WriteErrFile(message.Message);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[ERROR] " + message.Message);
Console.ForegroundColor = ConsoleColor.White;
break;
case LogSeverity.Info:
case LogSeverity.Debug:
WriteLogFile(message.Message);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("[INFO] " + message.Message);
Console.ForegroundColor = ConsoleColor.White;
break;
}
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,85 @@
using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces;
using System.Reflection;
using PluginManager.Others;
using PluginManager.Loaders;
using System.Threading.Tasks;
using System.Linq;
namespace PluginManager.Core
{
internal class CommandHandler
{
private readonly DiscordSocketClient client;
private readonly CommandService commandService;
private readonly string botPrefix;
public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix)
{
this.client = client;
this.commandService = commandService;
this.botPrefix = botPrefix;
}
public async Task InstallCommandsAsync()
{
client.MessageReceived += MessageHandler;
await commandService.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
}
private async Task MessageHandler(SocketMessage Message)
{
try
{
if (Message as SocketUserMessage == null)
return;
var message = Message as SocketUserMessage;
if (message == null) return;
int argPos = 0;
if (message.HasMentionPrefix(client.CurrentUser, ref argPos))
{
await message.Channel.SendMessageAsync("Can not exec mentioned commands !");
return;
}
if (!(message.HasStringPrefix(botPrefix, ref argPos) || message.Author.IsBot))
return;
var context = new SocketCommandContext(client, message);
await commandService.ExecuteAsync(
context: context,
argPos: argPos,
services: null
);
DBCommand? plugin = PluginLoader.Plugins!.Where(p => p.Command == (message.Content.Split(' ')[0]).Substring(botPrefix.Length)).FirstOrDefault();
if (plugin != null)
{
if (message.Channel == await message.Author.CreateDMChannelAsync())
{
if (plugin.canUseDM)
{
plugin.Execute(context, message, client, true);
Functions.WriteLogFile("Executed command (DM) : " + plugin.Command);
}
return;
}
plugin.Execute(context, message, client, false);
Functions.WriteLogFile("Executed command : " + plugin.Command);
}
}
catch { }
}
}
}