diff --git a/src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs b/src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs new file mode 100644 index 000000000..ce5d13d47 --- /dev/null +++ b/src/Discord.Net/WebSocket/Extensions/ChannelExtensions.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; + +namespace Discord.WebSocket.Extensions +{ + public static class ChannelExtensions + { + public static IUser GetUser(this IDMChannel channel, ulong id) + => GetSocketDMChannel(channel).GetUser(id); + + public static IReadOnlyCollection GetUsers(this IDMChannel channel) + => GetSocketDMChannel(channel).Users; + + public static IUser GetUser(this IGroupChannel channel, ulong id) + => GetSocketGroupChannel(channel).GetUser(id); + + public static IReadOnlyCollection GetUsers(this IGroupChannel channel) + => GetSocketGroupChannel(channel).Users; + + public static IGuildUser GetUser(this ITextChannel channel, ulong id) + => GetSocketTextChannel(channel).GetUser(id); + + public static IReadOnlyCollection GetUsers(this ITextChannel channel) + => GetSocketTextChannel(channel).Members; + + public static IGuildUser GetUser(this IVoiceChannel channel, ulong id) + => GetSocketVoiceChannel(channel).GetUser(id); + + public static IReadOnlyCollection GetUsers(this IVoiceChannel channel) + => GetSocketVoiceChannel(channel).Members; + + 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 new file mode 100644 index 000000000..8e6a2f878 --- /dev/null +++ b/src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Discord.WebSocket.Extensions +{ + // Todo: Docstrings + public static class GuildExtensions + { + // Channels + + public static IGuildChannel GetChannel(this IGuild guild, ulong id) => + GetSocketGuild(guild).GetChannel(id); + + public static IReadOnlyCollection GetChannels(this IGuild guild) => + GetSocketGuild(guild).Channels; + + public static ITextChannel GetTextChannel(this IGuild guild, ulong id) => + GetSocketGuild(guild).GetChannel(id) as ITextChannel; + + public static IEnumerable GetTextChannels(this IGuild guild) => + GetSocketGuild(guild).Channels.Select(c => c as ITextChannel).Where(c => c != null); + + + public static IVoiceChannel GetVoiceChannel(this IGuild guild, ulong id) => + GetSocketGuild(guild).GetChannel(id) as IVoiceChannel; + + public static IEnumerable GetVoiceChannels(this IGuild guild) => + GetSocketGuild(guild).Channels.Select(c => c as IVoiceChannel).Where(c => c != null); + + // Users + + public static IGuildUser GetCurrentUser(this IGuild guild) => + GetSocketGuild(guild).CurrentUser; + + public static IGuildUser GetUser(this IGuild guild, ulong id) => + GetSocketGuild(guild).GetUser(id); + + public static IReadOnlyCollection GetUsers(this IGuild guild) => + GetSocketGuild(guild).Members; + + public static int GetUserCount(this IGuild guild) => + GetSocketGuild(guild).MemberCount; + + public static int GetCachedUserCount(this IGuild guild) => + GetSocketGuild(guild).DownloadedMemberCount; + + internal static SocketGuild GetSocketGuild(IGuild guild) + { + var socketGuild = guild as SocketGuild; + if (socketGuild == null) + throw new InvalidOperationException("This extension method is only valid on WebSocket Entities"); + return socketGuild; + } + } +}