Module, Command, PermissionAction typereaders, fixed HS (thanks to fearnlj01)
This commit is contained in:
		
							
								
								
									
										35
									
								
								src/NadekoBot/Modules/Permissions/PermissionAction.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/NadekoBot/Modules/Permissions/PermissionAction.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								src/NadekoBot/Modules/Permissions/Permissions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/NadekoBot/Modules/Permissions/Permissions.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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}");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -229,8 +229,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
 | 
			
		||||
                    var images = new List<Image>();
 | 
			
		||||
                    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()))
 | 
			
		||||
                        {
 | 
			
		||||
 
 | 
			
		||||
@@ -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<ILocalization>(Localizer);
 | 
			
		||||
            depMap.Add<DiscordSocketClient>(Client);
 | 
			
		||||
            depMap.Add<CommandService>(Commands);
 | 
			
		||||
            depMap.Add<CommandService>(CommandService);
 | 
			
		||||
            depMap.Add<IGoogleApiService>(Google);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            //setup typereaders
 | 
			
		||||
            CommandService.AddTypeReader<PermissionAction>(new PermissionActionTypeReader());
 | 
			
		||||
            CommandService.AddTypeReader<Command>(new CommandTypeReader());
 | 
			
		||||
            CommandService.AddTypeReader<Module>(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<string, string>(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));
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										25
									
								
								src/NadekoBot/TypeReaders/BotCommandTypeReader.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/NadekoBot/TypeReaders/BotCommandTypeReader.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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<TypeReaderResult> 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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								src/NadekoBot/TypeReaders/ModuleTypeReader.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/NadekoBot/TypeReaders/ModuleTypeReader.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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<TypeReaderResult> 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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								src/NadekoBot/TypeReaders/PermissionActionTypeReader.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/NadekoBot/TypeReaders/PermissionActionTypeReader.cs
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Used instead of bool for more flexible keywords for true/false only in the permission module
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class PermissionActionTypeReader : TypeReader
 | 
			
		||||
    {
 | 
			
		||||
        public override Task<TypeReaderResult> 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"));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user