From 48513daa64847848ca9b8abb92302d7dcaaa0f5d Mon Sep 17 00:00:00 2001 From: Kwoth Date: Fri, 16 Sep 2016 19:20:37 +0200 Subject: [PATCH] Module, Command, PermissionAction typereaders, fixed HS (thanks to fearnlj01) --- .../Modules/Permissions/PermissionAction.cs | 35 +++++++++++++++ .../Modules/Permissions/Permissions.cs | 39 ++++++++++++++++ src/NadekoBot/Modules/Searches/Searches.cs | 3 +- src/NadekoBot/NadekoBot.cs | 19 +++++--- .../TypeReaders/BotCommandTypeReader.cs | 25 +++++++++++ src/NadekoBot/TypeReaders/ModuleTypeReader.cs | 23 ++++++++++ .../TypeReaders/PermissionActionTypeReader.cs | 45 +++++++++++++++++++ 7 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 src/NadekoBot/Modules/Permissions/PermissionAction.cs create mode 100644 src/NadekoBot/Modules/Permissions/Permissions.cs create mode 100644 src/NadekoBot/TypeReaders/BotCommandTypeReader.cs create mode 100644 src/NadekoBot/TypeReaders/ModuleTypeReader.cs create mode 100644 src/NadekoBot/TypeReaders/PermissionActionTypeReader.cs diff --git a/src/NadekoBot/Modules/Permissions/PermissionAction.cs b/src/NadekoBot/Modules/Permissions/PermissionAction.cs new file mode 100644 index 00000000..ea2df7ed --- /dev/null +++ b/src/NadekoBot/Modules/Permissions/PermissionAction.cs @@ -0,0 +1,35 @@ +using Discord.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; + +namespace NadekoBot.Modules.Permissions +{ + public class PermissionAction + { + public static PermissionAction Enable => new PermissionAction(true); + public static PermissionAction Disable => new PermissionAction(false); + + public bool Value { get; } + + public PermissionAction(bool value) + { + this.Value = value; + } + + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + return this.Value == ((PermissionAction)obj).Value; + } + + public override int GetHashCode() => Value.GetHashCode(); + } +} diff --git a/src/NadekoBot/Modules/Permissions/Permissions.cs b/src/NadekoBot/Modules/Permissions/Permissions.cs new file mode 100644 index 00000000..5619ddf0 --- /dev/null +++ b/src/NadekoBot/Modules/Permissions/Permissions.cs @@ -0,0 +1,39 @@ +using NadekoBot.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using Discord.WebSocket; +using NadekoBot.Services; +using Discord; + +namespace NadekoBot.Modules.Permissions +{ + [NadekoModule("Permissions", ";")] + public class Permissions : DiscordModule + { + public Permissions(ILocalization loc, CommandService cmds, DiscordSocketClient client) : base(loc, cmds, client) + { + } + + [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] + [RequireContext(ContextType.Guild)] + public async Task UsrCmd(IUserMessage imsg, Command command, PermissionAction action, IGuildUser user) + { + var channel = (ITextChannel)imsg.Channel; + + await channel.SendMessageAsync($"{command.Text} {action.Value} {user}"); + } + + [LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias] + [RequireContext(ContextType.Guild)] + public async Task UsrMdl(IUserMessage imsg, Module module, PermissionAction action, IGuildUser user) + { + var channel = (ITextChannel)imsg.Channel; + + await channel.SendMessageAsync($"{module.Name} {action.Value} {user}"); + } + } +} diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 80c52226..da2fee95 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -229,8 +229,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】 var images = new List(); if (items == null) throw new KeyNotFoundException("Cannot find a card by that name"); - var cnt = 0; - foreach (var item in items.TakeWhile(item => cnt++ < 4).Where(item => item.HasValues && item["img"] != null)) + foreach (var item in items.Where(item => item.HasValues && item["img"] != null).Take(4)) { using (var sr =await http.GetStreamAsync(item["img"].ToString())) { diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 705d0e02..d4e4d904 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -14,6 +14,9 @@ using System.Reflection; using System.Threading.Tasks; using System.Collections.Generic; using System.Collections.ObjectModel; +using NadekoBot.Modules.Permissions; +using Module = Discord.Commands.Module; +using NadekoBot.TypeReaders; namespace NadekoBot { @@ -21,7 +24,7 @@ namespace NadekoBot { private Logger _log; - public static CommandService Commands { get; private set; } + public static CommandService CommandService { get; private set; } public static CommandHandler CommandHandler { get; private set; } public static DiscordSocketClient Client { get; private set; } public static Localization Localizer { get; private set; } @@ -49,19 +52,25 @@ namespace NadekoBot //initialize Services Credentials = new BotCredentials(); - Commands = new CommandService(); + CommandService = new CommandService(); Localizer = new Localization(); Google = new GoogleApiService(); - CommandHandler = new CommandHandler(Client, Commands); + CommandHandler = new CommandHandler(Client, CommandService); Stats = new StatsService(Client, CommandHandler); //setup DI var depMap = new DependencyMap(); depMap.Add(Localizer); depMap.Add(Client); - depMap.Add(Commands); + depMap.Add(CommandService); depMap.Add(Google); + + //setup typereaders + CommandService.AddTypeReader(new PermissionActionTypeReader()); + CommandService.AddTypeReader(new CommandTypeReader()); + CommandService.AddTypeReader(new ModuleTypeReader()); + //connect await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false); await Client.ConnectAsync().ConfigureAwait(false); @@ -74,7 +83,7 @@ namespace NadekoBot { ModulePrefixes = new ReadOnlyDictionary(uow.BotConfig.GetOrCreate().ModulePrefixes.ToDictionary(m => m.ModuleName, m => m.Prefix)); } - await Commands.LoadAssembly(Assembly.GetEntryAssembly(), depMap).ConfigureAwait(false); + await CommandService.LoadAssembly(Assembly.GetEntryAssembly(), depMap).ConfigureAwait(false); Console.WriteLine(await Stats.Print().ConfigureAwait(false)); diff --git a/src/NadekoBot/TypeReaders/BotCommandTypeReader.cs b/src/NadekoBot/TypeReaders/BotCommandTypeReader.cs new file mode 100644 index 00000000..1ab7869f --- /dev/null +++ b/src/NadekoBot/TypeReaders/BotCommandTypeReader.cs @@ -0,0 +1,25 @@ +using Discord.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; + +namespace NadekoBot.TypeReaders +{ + public class CommandTypeReader : TypeReader + { + public override Task Read(IUserMessage context, string input) + { + input = input.ToUpperInvariant(); + var cmd = NadekoBot.CommandService.Commands.FirstOrDefault(c => + c.Aliases.Select(a => a.ToUpperInvariant()).Contains(input) || + c.Text.ToUpperInvariant() == input); + if (cmd == null) + return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "No such command found.")); + + return Task.FromResult(TypeReaderResult.FromSuccess(cmd)); + } + } +} diff --git a/src/NadekoBot/TypeReaders/ModuleTypeReader.cs b/src/NadekoBot/TypeReaders/ModuleTypeReader.cs new file mode 100644 index 00000000..acd3e5a5 --- /dev/null +++ b/src/NadekoBot/TypeReaders/ModuleTypeReader.cs @@ -0,0 +1,23 @@ +using Discord.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; + +namespace NadekoBot.TypeReaders +{ + public class ModuleTypeReader : TypeReader + { + public override Task Read(IUserMessage context, string input) + { + input = input.ToUpperInvariant(); + var module = NadekoBot.CommandService.Modules.FirstOrDefault(m => m.Name.ToUpperInvariant() == input); + if (module == null) + return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "No such module found.")); + + return Task.FromResult(TypeReaderResult.FromSuccess(module)); + } + } +} diff --git a/src/NadekoBot/TypeReaders/PermissionActionTypeReader.cs b/src/NadekoBot/TypeReaders/PermissionActionTypeReader.cs new file mode 100644 index 00000000..1beff942 --- /dev/null +++ b/src/NadekoBot/TypeReaders/PermissionActionTypeReader.cs @@ -0,0 +1,45 @@ +using Discord.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; +using NadekoBot.Modules.Permissions; + +namespace NadekoBot.TypeReaders +{ + /// + /// Used instead of bool for more flexible keywords for true/false only in the permission module + /// + public class PermissionActionTypeReader : TypeReader + { + public override Task Read(IUserMessage context, string input) + { + input = input.ToUpperInvariant(); + switch (input) + { + case "1": + case "T": + case "TRUE": + case "ENABLE": + case "ENABLED": + case "ALLOW": + case "PERMIT": + case "UNBAN": + return Task.FromResult(TypeReaderResult.FromSuccess(PermissionAction.Enable)); + case "0": + case "F": + case "FALSE": + case "DENY": + case "DISABLE": + case "DISABLED": + case "DISALLOW": + case "BAN": + return Task.FromResult(TypeReaderResult.FromSuccess(PermissionAction.Disable)); + default: + return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Did not receive a valid boolean value")); + } + } + } +}