Module, Command, PermissionAction typereaders, fixed HS (thanks to fearnlj01)

This commit is contained in:
Kwoth 2016-09-16 19:20:37 +02:00
parent 8fa90348a3
commit 48513daa64
7 changed files with 182 additions and 7 deletions

View 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();
}
}

View 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}");
}
}
}

View File

@ -229,8 +229,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
var images = new List<Image>(); var images = new List<Image>();
if (items == null) if (items == null)
throw new KeyNotFoundException("Cannot find a card by that name"); throw new KeyNotFoundException("Cannot find a card by that name");
var cnt = 0; foreach (var item in items.Where(item => item.HasValues && item["img"] != null).Take(4))
foreach (var item in items.TakeWhile(item => cnt++ < 4).Where(item => item.HasValues && item["img"] != null))
{ {
using (var sr =await http.GetStreamAsync(item["img"].ToString())) using (var sr =await http.GetStreamAsync(item["img"].ToString()))
{ {

View File

@ -14,6 +14,9 @@ using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using NadekoBot.Modules.Permissions;
using Module = Discord.Commands.Module;
using NadekoBot.TypeReaders;
namespace NadekoBot namespace NadekoBot
{ {
@ -21,7 +24,7 @@ namespace NadekoBot
{ {
private Logger _log; 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 CommandHandler CommandHandler { get; private set; }
public static DiscordSocketClient Client { get; private set; } public static DiscordSocketClient Client { get; private set; }
public static Localization Localizer { get; private set; } public static Localization Localizer { get; private set; }
@ -49,19 +52,25 @@ namespace NadekoBot
//initialize Services //initialize Services
Credentials = new BotCredentials(); Credentials = new BotCredentials();
Commands = new CommandService(); CommandService = new CommandService();
Localizer = new Localization(); Localizer = new Localization();
Google = new GoogleApiService(); Google = new GoogleApiService();
CommandHandler = new CommandHandler(Client, Commands); CommandHandler = new CommandHandler(Client, CommandService);
Stats = new StatsService(Client, CommandHandler); Stats = new StatsService(Client, CommandHandler);
//setup DI //setup DI
var depMap = new DependencyMap(); var depMap = new DependencyMap();
depMap.Add<ILocalization>(Localizer); depMap.Add<ILocalization>(Localizer);
depMap.Add<DiscordSocketClient>(Client); depMap.Add<DiscordSocketClient>(Client);
depMap.Add<CommandService>(Commands); depMap.Add<CommandService>(CommandService);
depMap.Add<IGoogleApiService>(Google); depMap.Add<IGoogleApiService>(Google);
//setup typereaders
CommandService.AddTypeReader<PermissionAction>(new PermissionActionTypeReader());
CommandService.AddTypeReader<Command>(new CommandTypeReader());
CommandService.AddTypeReader<Module>(new ModuleTypeReader());
//connect //connect
await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false); await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false);
await Client.ConnectAsync().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)); 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)); Console.WriteLine(await Stats.Print().ConfigureAwait(false));

View 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));
}
}
}

View 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));
}
}
}

View 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"));
}
}
}
}