From e5909c49bd4b3ba7e959454efefad2b717347ab7 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 30 Jul 2016 18:05:01 -0400 Subject: [PATCH] Added extension methods for WebSocket->Channels For users importing `Discord.WebSocket.Extensions`, there are now non-async extensions for GetUser. These methods point to the appropriate method on the respective SocketgChannel. --- .../WebSocket/Extensions/ChannelExtensions.cs | 48 +++++++++++++++++++ .../WebSocket/Extensions/GuildExtensions.cs | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs diff --git a/src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs b/src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs new file mode 100644 index 000000000..ce70d590a --- /dev/null +++ b/src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs @@ -0,0 +1,48 @@ +using System; + +namespace Discord.WebSocket.Extensions +{ + public static class ChannelExtensions + { + public static IUser GetUser(this IDMChannel channel, ulong id) + => GetSocketDMChannel(channel).GetUser(id); + + public static IGroupUser GetUser(this IGroupChannel channel, ulong id) + => GetSocketGroupChannel(channel).GetUser(id) as IGroupUser; + + public static IGuildUser GetUser(this ITextChannel channel, ulong id) + => GetSocketTextChannel(channel).GetUser(id); + + public static IGuildUser GetUser(this IVoiceChannel channel, ulong id) + => GetSocketVoiceChannel(channel).GetUser(id); + + internal static SocketDMChannel GetSocketDMChannel(IDMChannel channel) + { + var socketChannel = channel as SocketDMChannel; + if (socketChannel == null) + throw new InvalidOperationException("This extension method is only valid on WebSocket Entities"); + return socketChannel; + } + internal static SocketGroupChannel GetSocketGroupChannel(IGroupChannel channel) + { + var socketChannel = channel as SocketGroupChannel; + if (socketChannel == null) + throw new InvalidOperationException("This extension method is only valid on WebSocket Entities"); + return socketChannel; + } + internal static SocketTextChannel GetSocketTextChannel(ITextChannel channel) + { + var socketChannel = channel as SocketTextChannel; + if (socketChannel == null) + throw new InvalidOperationException("This extension method is only valid on WebSocket Entities"); + return socketChannel; + } + internal static SocketVoiceChannel GetSocketVoiceChannel(IVoiceChannel channel) + { + var socketChannel = channel as SocketVoiceChannel; + if (socketChannel == null) + throw new InvalidOperationException("This extension method is only valid on WebSocket Entities"); + return socketChannel; + } + } +} diff --git a/src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs b/src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs index d2e8e1482..a90fd1b56 100644 --- a/src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs +++ b/src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs @@ -4,7 +4,7 @@ using System.Linq; namespace Discord.WebSocket.Extensions { - // TODO: Docstrings + // Todo: Docstrings public static class GuildExtensions { // Channels