Packages can be loaded/unloaded. IUnloadableService interface added whose method Unload, if service implements it, will be called when the module is unloaded.

This commit is contained in:
Master Kwoth
2017-10-05 00:51:12 +02:00
parent 599245b1ca
commit 33ac43e1b5
74 changed files with 866 additions and 520 deletions

View File

@ -0,0 +1,64 @@
using Discord.Commands;
using NadekoBot.Common.Attributes;
using NadekoBot.Core.Modules.Administration.Services;
using NadekoBot.Extensions;
using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class PackagesCommands : NadekoSubmodule<PackagesService>
{
private readonly NadekoBot _bot;
public PackagesCommands(NadekoBot bot)
{
_bot = bot;
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task PackageList()
{
_service.ReloadAvailablePackages();
await Context.Channel.SendConfirmAsync(string.Join("\n", _service.Packages));
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task PackageUnload(string name)
{
if (name.Contains(":") || name.Contains(".") || name.Contains("\\") || name.Contains("/") || name.Contains("~"))
return;
name = name.ToTitleCase();
var package = Assembly.LoadFrom(Path.Combine(AppContext.BaseDirectory,
"modules",
$"NadekoBot.Modules.{name}",
$"NadekoBot.Modules.{name}.dll"));
await _bot.UnloadPackage(name).ConfigureAwait(false);
await ReplyAsync(":ok:");
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task PackageLoad(string name)
{
if (name.Contains(".") || name.Contains("\\") || name.Contains("/") || name.Contains("~"))
return;
name = name.ToTitleCase();
await _bot.LoadPackage(name);
await ReplyAsync(":ok:");
}
}
}
}

View File

@ -48,6 +48,7 @@ namespace NadekoBot.Modules.Administration
[OwnerOnly]
public async Task StartupCommandAdd([Remainder] string cmdText)
{
//todo don't let .die be a startup command
var guser = ((IGuildUser)Context.User);
var cmd = new StartupCommand()
{

View File

@ -0,0 +1,31 @@
using NadekoBot.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace NadekoBot.Core.Modules.Administration.Services
{
public class PackagesService : INService
{
public IEnumerable<string> Packages { get; private set; }
public PackagesService()
{
ReloadAvailablePackages();
}
public void ReloadAvailablePackages()
{
Packages = Directory.GetDirectories(Path.Combine(AppContext.BaseDirectory, "modules\\"), "NadekoBot.Modules.*", SearchOption.AllDirectories)
.SelectMany(x => Directory.GetFiles(x, "NadekoBot.Modules.*.dll"))
.Select(x => Path.GetFileNameWithoutExtension(x))
.Select(x =>
{
var m = Regex.Match(x, @"NadekoBot\.Modules\.(?<name>.*)");
return m.Groups["name"].Value;
});
}
}
}