From e3930d8a5bc08557518d10a0bc679d4279b6649f Mon Sep 17 00:00:00 2001 From: Alex Gravely Date: Thu, 29 Mar 2018 18:10:25 -0400 Subject: [PATCH] Add SocketUser.MutualGuilds + various ext. methods. --- .../Extensions/CommandServiceExtensions.cs | 27 +++++++++++++++++++ .../Extensions/EmbedBuilderExtensions.cs | 12 +++++++++ .../Entities/Users/SocketUser.cs | 6 ++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/Discord.Net.Commands/Extensions/CommandServiceExtensions.cs diff --git a/src/Discord.Net.Commands/Extensions/CommandServiceExtensions.cs b/src/Discord.Net.Commands/Extensions/CommandServiceExtensions.cs new file mode 100644 index 000000000..014dc14cd --- /dev/null +++ b/src/Discord.Net.Commands/Extensions/CommandServiceExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Discord.Commands +{ + public static class CommandServiceExtensions + { + public static async Task> GetExecutableCommandsAsync(this IEnumerable commands, ICommandContext context, IServiceProvider provider) + { + var executableCommands = new List(); + + foreach (var command in commands) + { + var result = await command.CheckPreconditionsAsync(context, provider).ConfigureAwait(false); + if (result.IsSuccess) + executableCommands.Add(command); + } + + return executableCommands; + } + public static Task> GetExecutableCommandsAsync(this CommandService commandService, ICommandContext context, IServiceProvider provider) + => GetExecutableCommandsAsync(commandService.Commands, context, provider); + public static Task> GetExecutableCommandAsync(this ModuleInfo module, ICommandContext context, IServiceProvider provider) + => GetExecutableCommandsAsync(module.Commands, context, provider); + } +} diff --git a/src/Discord.Net.Core/Extensions/EmbedBuilderExtensions.cs b/src/Discord.Net.Core/Extensions/EmbedBuilderExtensions.cs index 2eb4ed473..3e3c8ccaa 100644 --- a/src/Discord.Net.Core/Extensions/EmbedBuilderExtensions.cs +++ b/src/Discord.Net.Core/Extensions/EmbedBuilderExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace Discord { @@ -54,5 +56,15 @@ namespace Discord return builder; } + + public static EmbedBuilder WithFields(this EmbedBuilder builder, IEnumerable 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()); } } diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs index 58d5c62a1..a42ac3b85 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs @@ -1,5 +1,7 @@ -using Discord.Rest; +using Discord.Rest; using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Model = Discord.API.User; @@ -20,6 +22,8 @@ namespace Discord.WebSocket public string Mention => MentionUtils.MentionUser(Id); public IActivity Activity => Presence.Activity; public UserStatus Status => Presence.Status; + public IEnumerable MutualGuilds + => Discord.Guilds.Where(g => g.Users.Any(u => u.Id == Id)); internal SocketUser(DiscordSocketClient discord, ulong id) : base(discord, id)