This commit is contained in:
43
SlashCommands/Initializer.cs
Normal file
43
SlashCommands/Initializer.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager;
|
||||
using PluginManager.Interfaces;
|
||||
|
||||
using SlashCommands.Items;
|
||||
|
||||
namespace SlashCommands
|
||||
{
|
||||
public class Initializer : DBEvent
|
||||
{
|
||||
public string name => "Slash command engine";
|
||||
|
||||
public string description => "The slash commands initializer and engine";
|
||||
|
||||
public async void Start(DiscordSocketClient client)
|
||||
{
|
||||
if(!Config.ContainsKey("ServerID") || Config.GetValue<string>("ServerID") == "null" || Config.GetValue<string>("ServerID").Length != 18)
|
||||
{
|
||||
Console.WriteLine("Invalid Server ID. Change config.json from file and restart bot");
|
||||
await Task.Delay(2000);
|
||||
return;
|
||||
}
|
||||
|
||||
SlashCommandLoader loader = new SlashCommandLoader("./Data/Plugins/SlashCommands/", "dll", client);
|
||||
loader.FileLoaded += (args) => Console.WriteLine(args[0] + " => " + args[1]);
|
||||
loader.PluginLoaded += (args) => Console.WriteLine(args[0] + " => " + args[1]);
|
||||
Globals.commands = await loader.Load();
|
||||
|
||||
client.SlashCommandExecuted += async (args) =>
|
||||
{
|
||||
foreach (var cmd in Globals.commands)
|
||||
{
|
||||
if (cmd.Command == args.Data.Name)
|
||||
{
|
||||
await cmd.ExecuteServer(args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
22
SlashCommands/Items/DBSlashCommand.cs
Normal file
22
SlashCommands/Items/DBSlashCommand.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SlashCommands.Items
|
||||
{
|
||||
public interface DBSlashCommand
|
||||
{
|
||||
string Command { get; }
|
||||
string Description { get; }
|
||||
string Usage { get; }
|
||||
bool requireAdmin { get; }
|
||||
bool PrivateResponse { get; }
|
||||
Task ExecuteServer(SocketSlashCommand command);
|
||||
Task InitializeCommand(DiscordSocketClient client);
|
||||
}
|
||||
}
|
||||
13
SlashCommands/Items/Globals.cs
Normal file
13
SlashCommands/Items/Globals.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SlashCommands.Items
|
||||
{
|
||||
internal class Globals
|
||||
{
|
||||
internal static List<DBSlashCommand> commands = null;
|
||||
}
|
||||
}
|
||||
85
SlashCommands/Items/SlashCommandLoader.cs
Normal file
85
SlashCommands/Items/SlashCommandLoader.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using Discord.WebSocket;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SlashCommands.Items
|
||||
{
|
||||
public class SlashCommandLoader
|
||||
{
|
||||
internal DiscordSocketClient client;
|
||||
|
||||
internal delegate void FileLoadedEventHandler(string[] args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(string[] args);
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
private string location, extension;
|
||||
internal SlashCommandLoader(string location, string extension, DiscordSocketClient client)
|
||||
{
|
||||
this.location = location;
|
||||
this.extension = extension;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
internal async Task<List<DBSlashCommand>> Load()
|
||||
{
|
||||
List<DBSlashCommand> slashCommands = new();
|
||||
var files = Directory.GetFiles(location, $"*.{extension}", SearchOption.AllDirectories);
|
||||
foreach(var file in files)
|
||||
{
|
||||
Assembly.LoadFrom(file);
|
||||
if(FileLoaded != null)
|
||||
{
|
||||
var args = new string[] { file, "Loaded" };
|
||||
FileLoaded.Invoke(args);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var interfaceType = typeof(DBSlashCommand);
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
|
||||
.ToArray();
|
||||
|
||||
foreach(var type in types)
|
||||
{
|
||||
try
|
||||
{
|
||||
var plugin = (DBSlashCommand)Activator.CreateInstance(type);
|
||||
slashCommands.Add(plugin);
|
||||
if (PluginLoaded != null)
|
||||
{
|
||||
var args = new string[] { plugin.Command, "Loaded successfully" };
|
||||
PluginLoaded.Invoke(args);
|
||||
|
||||
await plugin.InitializeCommand(client);
|
||||
}
|
||||
}
|
||||
catch {
|
||||
var args = new string[] { type.Name, "Failed to load" };
|
||||
PluginLoaded!.Invoke(args);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
|
||||
return slashCommands;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
13
SlashCommands/SlashCommands.csproj
Normal file
13
SlashCommands/SlashCommands.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PluginManager\PluginManager.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user