Browse Source

Adjust GuildExtensions per Voltana's feedback

Instead of implicitly soft-casting IGuild to SocketGuild, I added a method to soft-cast IGuild to SocketGuild, and throw an InvalidOp if it came up null for some reason.

GetUsers was changed from an IEnumerable to an IReadOnlyCollection to allow quick ".Count"-ing on the collection.
tags/1.0-rc
Christopher F 8 years ago
parent
commit
ee7bf028f7
1 changed files with 21 additions and 12 deletions
  1. +21
    -12
      src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs

+ 21
- 12
src/Discord.Net/WebSocket/Extensions/GuildExtensions.cs View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Discord.WebSocket.Extensions
@@ -9,36 +10,44 @@ namespace Discord.WebSocket.Extensions
// Channels

public static IGuildChannel GetChannel(this IGuild guild, ulong id) =>
(guild as SocketGuild).GetChannel(id);
GetSocketGuild(guild).GetChannel(id);

public static ITextChannel GetTextChannel(this IGuild guild, ulong id) =>
(guild as SocketGuild).GetChannel(id) as ITextChannel;
GetSocketGuild(guild).GetChannel(id) as ITextChannel;

public static IEnumerable<ITextChannel> GetTextChannels(this IGuild guild) =>
(guild as SocketGuild).Channels.Select(c => c as ITextChannel).Where(c => c != null);
GetSocketGuild(guild).Channels.Select(c => c as ITextChannel).Where(c => c != null);


public static IVoiceChannel GetVoiceChannel(this IGuild guild, ulong id) =>
(guild as SocketGuild).GetChannel(id) as IVoiceChannel;
GetSocketGuild(guild).GetChannel(id) as IVoiceChannel;

public static IEnumerable<IVoiceChannel> GetVoiceChannels(this IGuild guild) =>
(guild as SocketGuild).Channels.Select(c => c as IVoiceChannel).Where(c => c != null);
GetSocketGuild(guild).Channels.Select(c => c as IVoiceChannel).Where(c => c != null);

// Users

public static IGuildUser GetCurrentUser(this IGuild guild) =>
(guild as SocketGuild).CurrentUser;
GetSocketGuild(guild).CurrentUser;

public static IGuildUser GetUser(this IGuild guild, ulong id) =>
(guild as SocketGuild).GetUser(id);
GetSocketGuild(guild).GetUser(id);

public static IEnumerable<IGuildUser> GetUsers(this IGuild guild) =>
(guild as SocketGuild).Members;
public static IReadOnlyCollection<IGuildUser> GetUsers(this IGuild guild) =>
GetSocketGuild(guild).Members;

public static int GetUserCount(this IGuild guild) =>
(guild as SocketGuild).MemberCount;
GetSocketGuild(guild).MemberCount;

public static int GetCachedUserCount(this IGuild guild) =>
(guild as SocketGuild).DownloadedMemberCount;
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;
}
}
}

Loading…
Cancel
Save