From 259d889ce39d037aec5f35173059651912ee387f Mon Sep 17 00:00:00 2001 From: MarkusGordathian Date: Sun, 26 Feb 2017 23:01:11 -0800 Subject: [PATCH 1/4] Added GetPrecondition() to ModuleInfo and CommandInfo --- src/Discord.Net.Commands/Info/CommandInfo.cs | 3 +++ src/Discord.Net.Commands/Info/ModuleInfo.cs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 031d37581..fe325d8fb 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -63,6 +63,9 @@ namespace Discord.Commands _action = builder.Callback; } + public TPrecondition GetPrecondition() where TPrecondition : PreconditionAttribute => + Preconditions.FirstOrDefault(x => x is TPrecondition) as TPrecondition; + public async Task CheckPreconditionsAsync(ICommandContext context, IDependencyMap map = null) { if (map == null) diff --git a/src/Discord.Net.Commands/Info/ModuleInfo.cs b/src/Discord.Net.Commands/Info/ModuleInfo.cs index a2094df65..3b1b843ae 100644 --- a/src/Discord.Net.Commands/Info/ModuleInfo.cs +++ b/src/Discord.Net.Commands/Info/ModuleInfo.cs @@ -20,6 +20,9 @@ namespace Discord.Commands public ModuleInfo Parent { get; } public bool IsSubmodule => Parent != null; + public TPrecondition GetPrecondition() where TPrecondition : PreconditionAttribute => + Preconditions.FirstOrDefault(x => x is TPrecondition) as TPrecondition; + internal ModuleInfo(ModuleBuilder builder, CommandService service, ModuleInfo parent = null) { Service = service; From ef899b06992690817ac75c488d4538255a3ec2af Mon Sep 17 00:00:00 2001 From: MarkusGordathian Date: Mon, 27 Feb 2017 16:55:33 -0800 Subject: [PATCH 2/4] Now matches exact type given, rather than any base preconditions from which it may derive --- src/Discord.Net.Commands/Info/CommandInfo.cs | 2 +- src/Discord.Net.Commands/Info/ModuleInfo.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index fe325d8fb..996799972 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -64,7 +64,7 @@ namespace Discord.Commands } public TPrecondition GetPrecondition() where TPrecondition : PreconditionAttribute => - Preconditions.FirstOrDefault(x => x is TPrecondition) as TPrecondition; + Preconditions.FirstOrDefault(x => x.GetType() == typeof(TPrecondition)) as TPrecondition; public async Task CheckPreconditionsAsync(ICommandContext context, IDependencyMap map = null) { diff --git a/src/Discord.Net.Commands/Info/ModuleInfo.cs b/src/Discord.Net.Commands/Info/ModuleInfo.cs index 3b1b843ae..9ea5c5661 100644 --- a/src/Discord.Net.Commands/Info/ModuleInfo.cs +++ b/src/Discord.Net.Commands/Info/ModuleInfo.cs @@ -21,7 +21,7 @@ namespace Discord.Commands public bool IsSubmodule => Parent != null; public TPrecondition GetPrecondition() where TPrecondition : PreconditionAttribute => - Preconditions.FirstOrDefault(x => x is TPrecondition) as TPrecondition; + Preconditions.FirstOrDefault(x => x.GetType() == typeof(TPrecondition)) as TPrecondition; internal ModuleInfo(ModuleBuilder builder, CommandService service, ModuleInfo parent = null) { From bb69e944b9c5dabef3311a189cd42c028d8613a9 Mon Sep 17 00:00:00 2001 From: MarkusGordathian Date: Sat, 18 Mar 2017 10:12:13 -0700 Subject: [PATCH 3/4] Now returns a collection instead of only the first result --- src/Discord.Net.Commands/Info/CommandInfo.cs | 4 ++-- src/Discord.Net.Commands/Info/ModuleInfo.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 996799972..27cd58d5f 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -63,8 +63,8 @@ namespace Discord.Commands _action = builder.Callback; } - public TPrecondition GetPrecondition() where TPrecondition : PreconditionAttribute => - Preconditions.FirstOrDefault(x => x.GetType() == typeof(TPrecondition)) as TPrecondition; + public IEnumerable GetPrecondition() where TPrecondition : PreconditionAttribute => + Preconditions.Where(x => x.GetType() == typeof(TPrecondition)).Select(x => x as TPrecondition); public async Task CheckPreconditionsAsync(ICommandContext context, IDependencyMap map = null) { diff --git a/src/Discord.Net.Commands/Info/ModuleInfo.cs b/src/Discord.Net.Commands/Info/ModuleInfo.cs index 9ea5c5661..43bcd8e21 100644 --- a/src/Discord.Net.Commands/Info/ModuleInfo.cs +++ b/src/Discord.Net.Commands/Info/ModuleInfo.cs @@ -20,8 +20,8 @@ namespace Discord.Commands public ModuleInfo Parent { get; } public bool IsSubmodule => Parent != null; - public TPrecondition GetPrecondition() where TPrecondition : PreconditionAttribute => - Preconditions.FirstOrDefault(x => x.GetType() == typeof(TPrecondition)) as TPrecondition; + public IEnumerable GetPrecondition() where TPrecondition : PreconditionAttribute => + Preconditions.Where(x => x.GetType() == typeof(TPrecondition)).Select(x => x as TPrecondition); internal ModuleInfo(ModuleBuilder builder, CommandService service, ModuleInfo parent = null) { From b3590165d06bafa0fe1a27a2c8fc5a498e0c95d3 Mon Sep 17 00:00:00 2001 From: MarkusGordathian Date: Sat, 18 Mar 2017 10:21:07 -0700 Subject: [PATCH 4/4] Pluralized name and used .Cast() --- src/Discord.Net.Commands/Info/CommandInfo.cs | 4 ++-- src/Discord.Net.Commands/Info/ModuleInfo.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 27cd58d5f..55231b47a 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -63,8 +63,8 @@ namespace Discord.Commands _action = builder.Callback; } - public IEnumerable GetPrecondition() where TPrecondition : PreconditionAttribute => - Preconditions.Where(x => x.GetType() == typeof(TPrecondition)).Select(x => x as TPrecondition); + public IEnumerable GetPreconditions() where TPrecondition : PreconditionAttribute => + Preconditions.Where(x => x.GetType() == typeof(TPrecondition)).Cast(); public async Task CheckPreconditionsAsync(ICommandContext context, IDependencyMap map = null) { diff --git a/src/Discord.Net.Commands/Info/ModuleInfo.cs b/src/Discord.Net.Commands/Info/ModuleInfo.cs index 43bcd8e21..ed385564e 100644 --- a/src/Discord.Net.Commands/Info/ModuleInfo.cs +++ b/src/Discord.Net.Commands/Info/ModuleInfo.cs @@ -20,8 +20,8 @@ namespace Discord.Commands public ModuleInfo Parent { get; } public bool IsSubmodule => Parent != null; - public IEnumerable GetPrecondition() where TPrecondition : PreconditionAttribute => - Preconditions.Where(x => x.GetType() == typeof(TPrecondition)).Select(x => x as TPrecondition); + public IEnumerable GetPreconditions() where TPrecondition : PreconditionAttribute => + Preconditions.Where(x => x.GetType() == typeof(TPrecondition)).Cast(); internal ModuleInfo(ModuleBuilder builder, CommandService service, ModuleInfo parent = null) {