Browse Source

Merged GuildExtensions into IGuild

tags/1.0-rc
RogueException 8 years ago
parent
commit
158222bb78
4 changed files with 164 additions and 47 deletions
  1. +9
    -0
      src/Discord.Net.Core/Entities/Guilds/IGuild.cs
  2. +0
    -38
      src/Discord.Net.Core/Extensions/GuildExtensions.cs
  3. +106
    -3
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  4. +49
    -6
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 9
- 0
src/Discord.Net.Core/Entities/Guilds/IGuild.cs View File

@@ -77,6 +77,13 @@ namespace Discord
Task<IReadOnlyCollection<IGuildChannel>> GetChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Gets the channel in this guild with the provided id, or null if not found. </summary>
Task<IGuildChannel> GetChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IReadOnlyCollection<ITextChannel>> GetTextChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<ITextChannel> GetTextChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IReadOnlyCollection<IVoiceChannel>> GetVoiceChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IVoiceChannel> GetVoiceChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IVoiceChannel> GetAFKChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<ITextChannel> GetDefaultChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IVoiceChannel> GetEmbedChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Creates a new text channel. </summary>
Task<ITextChannel> CreateTextChannelAsync(string name, RequestOptions options = null);
/// <summary> Creates a new voice channel. </summary>
@@ -99,6 +106,8 @@ namespace Discord
Task<IGuildUser> GetUserAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Gets the current user for this guild. </summary>
Task<IGuildUser> GetCurrentUserAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Gets the owner of this guild. </summary>
Task<IGuildUser> GetOwnerAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Downloads all users for this guild if the current list is incomplete. </summary>
Task DownloadUsersAsync();
/// <summary> Removes all users from this guild if they have not logged on in a provided number of days or, if simulate is true, returns the number of users that would be removed. </summary>


+ 0
- 38
src/Discord.Net.Core/Extensions/GuildExtensions.cs View File

@@ -1,38 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Discord
{
public static class GuildExtensions
{
public static async Task<ITextChannel> GetTextChannelAsync(this IGuild guild, ulong id)
=> await guild.GetChannelAsync(id).ConfigureAwait(false) as ITextChannel;
public static async Task<IEnumerable<ITextChannel>> GetTextChannelsAsync(this IGuild guild)
=> (await guild.GetChannelsAsync().ConfigureAwait(false)).Select(x => x as ITextChannel).Where(x => x != null);

public static async Task<IVoiceChannel> GetVoiceChannelAsync(this IGuild guild, ulong id)
=> await guild.GetChannelAsync(id).ConfigureAwait(false) as IVoiceChannel;
public static async Task<IEnumerable<IVoiceChannel>> GetVoiceChannelsAsync(this IGuild guild)
=> (await guild.GetChannelsAsync().ConfigureAwait(false)).Select(x => x as IVoiceChannel).Where(x => x != null);

public static async Task<IVoiceChannel> GetAFKChannelAsync(this IGuild guild)
{
var afkId = guild.AFKChannelId;
if (afkId.HasValue)
return await guild.GetChannelAsync(afkId.Value).ConfigureAwait(false) as IVoiceChannel;
return null;
}
public static async Task<ITextChannel> GetDefaultChannelAsync(this IGuild guild)
=> await guild.GetChannelAsync(guild.DefaultChannelId).ConfigureAwait(false) as ITextChannel;
public static async Task<IVoiceChannel> GetEmbedChannelAsync(this IGuild guild)
{
var embedId = guild.EmbedChannelId;
if (embedId.HasValue)
return await guild.GetChannelAsync(embedId.Value).ConfigureAwait(false) as IVoiceChannel;
return null;
}
public static async Task<IGuildUser> GetOwnerAsync(this IGuild guild)
=> await guild.GetUserAsync(guild.OwnerId).ConfigureAwait(false);
}
}

+ 106
- 3
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -1,5 +1,4 @@
using Discord.API.Rest;
using Discord.Audio;
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -152,7 +151,53 @@ namespace Discord.Rest
public Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(RequestOptions options = null)
=> GuildHelper.GetChannelsAsync(this, Discord, options);
public Task<RestGuildChannel> GetChannelAsync(ulong id, RequestOptions options = null)
=> GuildHelper.GetChannelAsync(this, Discord, id, options);
=> GuildHelper.GetChannelAsync(this, Discord, id, options);
public async Task<RestTextChannel> GetTextChannelAsync(ulong id, RequestOptions options = null)
{
var channel = await GuildHelper.GetChannelAsync(this, Discord, id, options).ConfigureAwait(false);
return channel as RestTextChannel;
}
public async Task<IReadOnlyCollection<RestTextChannel>> GetTextChannelsAsync(RequestOptions options = null)
{
var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
return channels.Select(x => x as RestTextChannel).Where(x => x != null).ToImmutableArray();
}
public async Task<RestVoiceChannel> GetVoiceChannelAsync(ulong id, RequestOptions options = null)
{
var channel = await GuildHelper.GetChannelAsync(this, Discord, id, options).ConfigureAwait(false);
return channel as RestVoiceChannel;
}
public async Task<IReadOnlyCollection<RestVoiceChannel>> GetVoiceChannelsAsync(RequestOptions options = null)
{
var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
return channels.Select(x => x as RestVoiceChannel).Where(x => x != null).ToImmutableArray();
}

public async Task<RestVoiceChannel> GetAFKChannelAsync(RequestOptions options = null)
{
var afkId = AFKChannelId;
if (afkId.HasValue)
{
var channel = await GuildHelper.GetChannelAsync(this, Discord, afkId.Value, options).ConfigureAwait(false);
return channel as RestVoiceChannel;
}
return null;
}
public async Task<RestTextChannel> GetDefaultChannelAsync(RequestOptions options = null)
{
var channel = await GuildHelper.GetChannelAsync(this, Discord, DefaultChannelId, options).ConfigureAwait(false);
return channel as RestTextChannel;
}
public async Task<RestVoiceChannel> GetEmbedChannelAsync(RequestOptions options = null)
{
var embedId = EmbedChannelId;
if (embedId.HasValue)
{
var channel = await GuildHelper.GetChannelAsync(this, Discord, embedId.Value, options).ConfigureAwait(false);
return channel as RestVoiceChannel;
}
return null;
}
public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null)
@@ -192,6 +237,8 @@ namespace Discord.Rest
=> GuildHelper.GetUserAsync(this, Discord, id, options);
public Task<RestGuildUser> GetCurrentUserAsync(RequestOptions options = null)
=> GuildHelper.GetUserAsync(this, Discord, Discord.CurrentUser.Id, options);
public Task<RestGuildUser> GetOwnerAsync(RequestOptions options = null)
=> GuildHelper.GetUserAsync(this, Discord, OwnerId, options);

public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
@@ -222,6 +269,55 @@ namespace Discord.Rest
else
return null;
}
async Task<IReadOnlyCollection<ITextChannel>> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetTextChannelsAsync(options).ConfigureAwait(false);
else
return ImmutableArray.Create<ITextChannel>();
}
async Task<ITextChannel> IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetTextChannelAsync(id, options).ConfigureAwait(false);
else
return null;
}
async Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetVoiceChannelsAsync(options).ConfigureAwait(false);
else
return ImmutableArray.Create<IVoiceChannel>();
}
async Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetVoiceChannelAsync(id, options).ConfigureAwait(false);
else
return null;
}
async Task<IVoiceChannel> IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetAFKChannelAsync(options).ConfigureAwait(false);
else
return null;
}
async Task<ITextChannel> IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetDefaultChannelAsync(options).ConfigureAwait(false);
else
return null;
}
async Task<IVoiceChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetEmbedChannelAsync(options).ConfigureAwait(false);
else
return null;
}
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options)
=> await CreateTextChannelAsync(name, options).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
@@ -254,6 +350,13 @@ namespace Discord.Rest
else
return null;
}
async Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetOwnerAsync(options).ConfigureAwait(false);
else
return null;
}
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)


+ 49
- 6
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -1,5 +1,4 @@
using Discord.API.Rest;
using Discord.Audio;
using Discord.Audio;
using Discord.Rest;
using System;
using System.Collections.Concurrent;
@@ -44,15 +43,16 @@ namespace Discord.WebSocket
public int MemberCount { get; set; }
public int DownloadedMemberCount { get; private set; }

public ulong? AFKChannelId { get; private set; }
public ulong? EmbedChannelId { get; private set; }
internal ulong? AFKChannelId { get; private set; }
internal ulong? EmbedChannelId { get; private set; }
public ulong OwnerId { get; private set; }
public SocketGuildUser Owner => GetUser(OwnerId);
public string VoiceRegionId { get; private set; }
public string IconId { get; private set; }
public string SplashId { get; private set; }

public DateTimeOffset CreatedAt => DateTimeUtils.FromSnowflake(Id);
public ulong DefaultChannelId => Id;
public SocketTextChannel DefaultChannel => GetTextChannel(Id);
public string IconUrl => CDN.GetGuildIconUrl(Id, IconId);
public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId);
public bool HasAllMembers => _downloaderPromise.Task.IsCompleted;
@@ -60,6 +60,26 @@ namespace Discord.WebSocket
public Task SyncPromise => _syncPromise.Task;
public Task DownloaderPromise => _downloaderPromise.Task;
public IAudioClient AudioClient => _audioClient;
public SocketVoiceChannel AFKChannel
{
get
{
var id = AFKChannelId;
return id.HasValue ? GetVoiceChannel(id.Value) : null;
}
}
public SocketVoiceChannel EmbedChannel
{
get
{
var id = EmbedChannelId;
return id.HasValue ? GetVoiceChannel(id.Value) : null;
}
}
public IReadOnlyCollection<SocketTextChannel> TextChannels
=> Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray();
public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels
=> Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray();
public SocketGuildUser CurrentUser
{
get
@@ -286,6 +306,10 @@ namespace Discord.WebSocket
return channel;
return null;
}
public SocketTextChannel GetTextChannel(ulong id)
=> GetChannel(id) as SocketTextChannel;
public SocketVoiceChannel GetVoiceChannel(ulong id)
=> GetChannel(id) as SocketVoiceChannel;
public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null)
@@ -560,8 +584,11 @@ namespace Discord.WebSocket
internal SocketGuild Clone() => MemberwiseClone() as SocketGuild;

//IGuild
bool IGuild.Available => true;
ulong? IGuild.AFKChannelId => AFKChannelId;
IAudioClient IGuild.AudioClient => null;
bool IGuild.Available => true;
ulong IGuild.DefaultChannelId => Id;
ulong? IGuild.EmbedChannelId => EmbedChannelId;
IRole IGuild.EveryoneRole => EveryoneRole;
IReadOnlyCollection<IRole> IGuild.Roles => Roles;

@@ -572,6 +599,20 @@ namespace Discord.WebSocket
=> Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
Task<IGuildChannel> IGuild.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildChannel>(GetChannel(id));
Task<IReadOnlyCollection<ITextChannel>> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IReadOnlyCollection<ITextChannel>>(TextChannels);
Task<ITextChannel> IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<ITextChannel>(GetTextChannel(id));
Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IReadOnlyCollection<IVoiceChannel>>(VoiceChannels);
Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IVoiceChannel>(GetVoiceChannel(id));
Task<IVoiceChannel> IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IVoiceChannel>(AFKChannel);
Task<ITextChannel> IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<ITextChannel>(DefaultChannel);
Task<IVoiceChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IVoiceChannel>(EmbedChannel);
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options)
=> await CreateTextChannelAsync(name, options).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
@@ -596,6 +637,8 @@ namespace Discord.WebSocket
=> Task.FromResult<IGuildUser>(GetUser(id));
Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(CurrentUser);
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(Owner);
Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); }
}
}

Loading…
Cancel
Save