Skip to content

Commit

Permalink
Bot\* - Further Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudTheWolf committed Dec 9, 2023
1 parent d5ecb07 commit d5888c8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>E:\Harmony\</OutputPath>
<OutputPath>C:\bot\Plugins\Example</OutputPath>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

Expand Down
35 changes: 33 additions & 2 deletions CloudTheWolf.DSharpPlus.Scaffolding.Example.Module/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace CloudTheWolf.DSharpPlus.Scaffolding.Example.Module
{
public class Example : IShardPlugin
public class Example : IPlugin, IShardPlugin
{
public string Name => "Example Plugin";

Expand All @@ -26,7 +26,7 @@ public class Example : IShardPlugin
public static ILogger<Logger> Logger;

public List<string> MyCommandsList = new List<string>();
public IShardBot Bot { get; set; }
public dynamic Bot { get; set; }

public void InitPlugin(IShardBot bot, ILogger<Logger> logger, DiscordConfiguration discordConfiguration, IConfigurationRoot applicationConfig)
{
Expand All @@ -37,13 +37,32 @@ public void InitPlugin(IShardBot bot, ILogger<Logger> logger, DiscordConfigurati
Bot = bot;
}

public void InitPlugin(IBot bot, ILogger<Logger> logger, DiscordConfiguration discordConfiguration, IConfigurationRoot applicationConfig)
{
Logger = logger;
LoadConfig(applicationConfig);
RegisterCommands(bot);
Console.WriteLine("Hello World");
Bot = bot;
}

private void RegisterCommands(IShardBot bot)
{
bot.SlashCommandsExt.RegisterCommands<ExampleSlashCommands>();
bot.Commands.RegisterCommands<ExampleCommands>();
Logger.LogInformation($"{Name}: Registered {nameof(ExampleCommands)}!");
}

private void RegisterCommands(IBot bot)
{
bot.SlashCommandsExt.RegisterCommands<ExampleSlashCommands>();
bot.Commands.RegisterCommands<ExampleCommands>();
GetCommandNames(typeof(ExampleCommands));
Logger.LogInformation($"{Name}: Registered {nameof(ExampleCommands)}!");


}

private void LoadConfig(IConfigurationRoot applicationConfig)
{
Options.MySqlHost = applicationConfig.GetValue<string>("SQL:Host");
Expand Down Expand Up @@ -89,5 +108,17 @@ public void UnloadPlugin(IShardBot bot, ILogger<Logger> logger, DiscordConfigura
}
}
}

public void UnloadPlugin(IBot bot, ILogger<Logger> logger, DiscordConfiguration discordConfiguration)
{
var commands = bot.Commands;

foreach (var command in commands.RegisteredCommands)
{
if (!MyCommandsList.Contains(command.Value.Name)) continue;
bot.Commands.UnregisterCommands(command.Value);
}

}
}
}
2 changes: 1 addition & 1 deletion CloudTheWolf.DSharpPlus.Scaffolding.Worker/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class Bot : IBot

private static DiscordConfiguration _config;
private static dynamic _myConfig;
private static readonly PluginLoader PluginLoader = new PluginLoader();
internal static readonly PluginLoader PluginLoader = new PluginLoader();

public static ILogger<Logger> Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<ApplicationIcon>logo.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
Expand All @@ -17,6 +18,7 @@
<Content Include="appsettings.json.example">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="logo.ico" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PluginLoader
/// Dictionary of <see cref="IShardPlugin"/>s
/// </summary>
public Dictionary<string, IShardPlugin> ShardPlugins = new();
private Dictionary<string, CustomLoadContext> PluginLoadContexts = new();
public static Dictionary<string, CustomLoadContext> PluginLoadContexts = new();


/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion CloudTheWolf.DSharpPlus.Scaffolding.Worker/ShardBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class ShardBot : IShardBot

private static DiscordConfiguration _config;
private static dynamic _myConfig;
private static readonly PluginLoader PluginLoader = new PluginLoader();
internal static readonly PluginLoader PluginLoader = new PluginLoader();

public static ILogger<Logger> Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CloudTheWolf.DSharpPlus.Scaffolding.Shared.Interfaces;
using CloudTheWolf.DSharpPlus.Scaffolding.Worker.Services;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;

Expand All @@ -12,10 +14,33 @@ internal class PluginManagerCommands : BaseCommandModule
{
[Command("plugin.remove")]
[RequireOwner]
[Hidden]
public async Task RemovePlugin(CommandContext ctx, [RemainingText]string pluginName)
{
await ctx.TriggerTypingAsync();
var plugins = Bot.PluginLoader.Plugins;

foreach (var plugin in plugins)
{
if (plugin.Value.Name != pluginName) return;

if (plugin.Value is IDisposable disposable)
{
disposable.Dispose();
}

var loadContext = PluginLoader.PluginLoadContexts[pluginName];
PluginLoader.PluginLoadContexts.Remove(pluginName);

loadContext.Unload();

GC.Collect();
GC.WaitForPendingFinalizers();

await ctx.RespondAsync($"Unloaded [{pluginName}]");
return;
}
await ctx.RespondAsync($"Could not find [{pluginName}] to unload");
}
}
}
Binary file not shown.

0 comments on commit d5888c8

Please sign in to comment.