Add project files.
This commit is contained in:
37
DiscordBot/Discord/Commands/Help.cs
Normal file
37
DiscordBot/Discord/Commands/Help.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager.Loaders;
|
||||
using PluginManager.Interfaces;
|
||||
|
||||
namespace PluginManager.Commands
|
||||
{
|
||||
internal class Help : DBCommand
|
||||
{
|
||||
public string Command => "help";
|
||||
|
||||
public string Description => "This command allows you to check all loadded commands";
|
||||
|
||||
public string Usage => "help";
|
||||
|
||||
public bool canUseDM => true;
|
||||
public bool canUseServer => true;
|
||||
|
||||
public void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM)
|
||||
{
|
||||
if (isDM)
|
||||
{
|
||||
foreach (DBCommand p in PluginLoader.Plugins!)
|
||||
if (p.canUseDM)
|
||||
context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (DBCommand p in PluginLoader.Plugins!)
|
||||
if (p.canUseServer)
|
||||
context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
113
DiscordBot/Discord/Core/Boot.cs
Normal file
113
DiscordBot/Discord/Core/Boot.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
85
DiscordBot/Discord/Core/CommandHandler.cs
Normal file
85
DiscordBot/Discord/Core/CommandHandler.cs
Normal 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 { }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user