* Add SocketUser.MutualGuilds + various ext. methods. * Search through submodules for GetExecutableCommandAsync * Allow GetExecutableCommandsAsync(ModuleInfo) to recurse properly to all submodules. * Bump down lang. version & whitespace cleanup. * Change to use Task.WhenAll * Change to ICollection<CommandInfo> * Resolve build errors.pull/1181/head
@@ -0,0 +1,42 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Commands | |||||
{ | |||||
public static class CommandServiceExtensions | |||||
{ | |||||
public static async Task<IReadOnlyCollection<CommandInfo>> GetExecutableCommandsAsync(this ICollection<CommandInfo> commands, ICommandContext context, IServiceProvider provider) | |||||
{ | |||||
var executableCommands = new List<CommandInfo>(); | |||||
var tasks = commands.Select(async c => { var result = await c.CheckPreconditionsAsync(context, provider).ConfigureAwait(false); return new { Command = c, PreconditionResult = result }; }); | |||||
var results = await Task.WhenAll(tasks); | |||||
foreach (var result in results) | |||||
{ | |||||
if (result.PreconditionResult.IsSuccess) | |||||
executableCommands.Add(result.Command); | |||||
} | |||||
return executableCommands; | |||||
} | |||||
public static Task<IReadOnlyCollection<CommandInfo>> GetExecutableCommandsAsync(this CommandService commandService, ICommandContext context, IServiceProvider provider) | |||||
=> GetExecutableCommandsAsync(commandService.Commands.ToArray(), context, provider); | |||||
public static async Task<IReadOnlyCollection<CommandInfo>> GetExecutableCommandsAsync(this ModuleInfo module, ICommandContext context, IServiceProvider provider) | |||||
{ | |||||
var executableCommands = new List<CommandInfo>(); | |||||
executableCommands.AddRange(await module.Commands.ToArray().GetExecutableCommandsAsync(context, provider).ConfigureAwait(false)); | |||||
var tasks = module.Submodules.Select(async s => await s.GetExecutableCommandsAsync(context, provider).ConfigureAwait(false)); | |||||
var results = await Task.WhenAll(tasks); | |||||
executableCommands.AddRange(results.SelectMany(c => c)); | |||||
return executableCommands; | |||||
} | |||||
} | |||||
} |
@@ -1,4 +1,6 @@ | |||||
using System; | using System; | ||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
namespace Discord | namespace Discord | ||||
{ | { | ||||
@@ -65,5 +67,15 @@ namespace Discord | |||||
return builder; | return builder; | ||||
} | } | ||||
public static EmbedBuilder WithFields(this EmbedBuilder builder, IEnumerable<EmbedFieldBuilder> fields) | |||||
{ | |||||
foreach (var field in fields) | |||||
builder.AddField(field); | |||||
return builder; | |||||
} | |||||
public static EmbedBuilder WithFields(this EmbedBuilder builder, params EmbedFieldBuilder[] fields) | |||||
=> WithFields(builder, fields.AsEnumerable()); | |||||
} | } | ||||
} | } |
@@ -1,7 +1,9 @@ | |||||
using Discord.Rest; | |||||
using System; | using System; | ||||
using System.Collections.Generic; | |||||
using System.Diagnostics; | using System.Diagnostics; | ||||
using System.Linq; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using Discord.Rest; | |||||
using Model = Discord.API.User; | using Model = Discord.API.User; | ||||
namespace Discord.WebSocket | namespace Discord.WebSocket | ||||
@@ -35,6 +37,8 @@ namespace Discord.WebSocket | |||||
public IActivity Activity => Presence.Activity; | public IActivity Activity => Presence.Activity; | ||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public UserStatus Status => Presence.Status; | public UserStatus Status => Presence.Status; | ||||
public IEnumerable<SocketGuild> MutualGuilds | |||||
=> Discord.Guilds.Where(g => g.Users.Any(u => u.Id == Id)); | |||||
internal SocketUser(DiscordSocketClient discord, ulong id) | internal SocketUser(DiscordSocketClient discord, ulong id) | ||||
: base(discord, id) | : base(discord, id) | ||||