Skip to content

Commit

Permalink
Add support for channel categories (as its own channel type)
Browse files Browse the repository at this point in the history
  • Loading branch information
pegasy committed Sep 24, 2017
1 parent 30e867a commit e18bd8c
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 33 deletions.
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
//public class GuildChannelCategoryProperties : GuildChannelProperties
//{
//}
}
Expand Up @@ -26,5 +26,9 @@ public class GuildChannelProperties
/// Move the channel to the following position. This is 0-based!
/// </summary>
public Optional<int> Position { get; set; }
/// <summary>
/// Sets the category for this channel
/// </summary>
public Optional<ulong?> ParentId { get; set; }
}
}
6 changes: 5 additions & 1 deletion src/Discord.Net.Core/Entities/Channels/IGuildChannel.cs
Expand Up @@ -9,6 +9,10 @@ public interface IGuildChannel : IChannel, IDeletable
/// <summary> Gets the position of this channel in the guild's channel list, relative to others of the same type. </summary>
int Position { get; }

/// <summary> Gets the parentid (category) of this channel in the guild's channel list. </summary>
ulong? ParentId { get; }
/// <summary> Gets the parent channel (category) of this channel. </summary>
Task<IGuildChannel> GetParentChannelAsync();
/// <summary> Gets the guild this channel is a member of. </summary>
IGuild Guild { get; }
/// <summary> Gets the id of the guild this channel is a member of. </summary>
Expand All @@ -23,7 +27,7 @@ public interface IGuildChannel : IChannel, IDeletable
Task<IInviteMetadata> CreateInviteAsync(int? maxAge = 1800, int? maxUses = default(int?), bool isTemporary = false, bool isUnique = false, RequestOptions options = null);
/// <summary> Returns a collection of all invites to this channel. </summary>
Task<IReadOnlyCollection<IInviteMetadata>> GetInvitesAsync(RequestOptions options = null);

/// <summary> Modifies this guild channel. </summary>
Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null);

Expand Down
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Entities/Channels/IGuildChannelCategory.cs
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IGuildChannelCategory : IGuildChannel
{
///// <summary> Modifies this text channel. </summary>
//Task ModifyAsync(Action<GuildChannelCategoryProperties> func, RequestOptions options = null);
}
}
1 change: 1 addition & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Expand Up @@ -84,6 +84,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
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<IReadOnlyCollection<IGuildChannelCategory>> GetChannelCategoriesAsync(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> GetSystemChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Expand Down
2 changes: 2 additions & 0 deletions src/Discord.Net.Rest/API/Common/Channel.cs
Expand Up @@ -23,6 +23,8 @@ internal class Channel
public Optional<int> Position { get; set; }
[JsonProperty("permission_overwrites")]
public Optional<Overwrite[]> PermissionOverwrites { get; set; }
[JsonProperty("parent_id")]
public ulong? ParentId { get; set; }

//TextChannel
[JsonProperty("topic")]
Expand Down
14 changes: 14 additions & 0 deletions src/Discord.Net.Rest/API/Rest/ModifyGuildChannelCategoryParams.cs
@@ -0,0 +1,14 @@
#pragma warning disable CS1591
using Newtonsoft.Json;

namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
internal class ModifyGuildChannelCategoryParams
{
[JsonProperty("name")]
public Optional<string> Name { get; set; }
[JsonProperty("position")]
public Optional<int> Position { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
Expand Up @@ -10,5 +10,7 @@ internal class ModifyGuildChannelParams
public Optional<string> Name { get; set; }
[JsonProperty("position")]
public Optional<int> Position { get; set; }
[JsonProperty("parent_id")]
public Optional<ulong?> ParentId { get; set; }
}
}
31 changes: 17 additions & 14 deletions src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
Expand Up @@ -13,26 +13,27 @@ namespace Discord.Rest
internal static class ChannelHelper
{
//General
public static async Task DeleteAsync(IChannel channel, BaseDiscordClient client,
public static async Task DeleteAsync(IChannel channel, BaseDiscordClient client,
RequestOptions options)
{
{
await client.ApiClient.DeleteChannelAsync(channel.Id, options).ConfigureAwait(false);
}
public static async Task<Model> ModifyAsync(IGuildChannel channel, BaseDiscordClient client,
Action<GuildChannelProperties> func,
public static async Task<Model> ModifyAsync(IGuildChannel channel, BaseDiscordClient client,
Action<GuildChannelProperties> func,
RequestOptions options)
{
var args = new GuildChannelProperties();
func(args);
var apiArgs = new API.Rest.ModifyGuildChannelParams
{
Name = args.Name,
Position = args.Position
Position = args.Position,
ParentId = args.ParentId
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
}
public static async Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordClient client,
Action<TextChannelProperties> func,
public static async Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordClient client,
Action<TextChannelProperties> func,
RequestOptions options)
{
var args = new TextChannelProperties();
Expand All @@ -41,13 +42,14 @@ internal static class ChannelHelper
{
Name = args.Name,
Position = args.Position,
ParentId = args.ParentId,
Topic = args.Topic,
IsNsfw = args.IsNsfw
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
}
public static async Task<Model> ModifyAsync(IVoiceChannel channel, BaseDiscordClient client,
Action<VoiceChannelProperties> func,
public static async Task<Model> ModifyAsync(IVoiceChannel channel, BaseDiscordClient client,
Action<VoiceChannelProperties> func,
RequestOptions options)
{
var args = new VoiceChannelProperties();
Expand All @@ -57,6 +59,7 @@ internal static class ChannelHelper
Bitrate = args.Bitrate,
Name = args.Name,
Position = args.Position,
ParentId = args.ParentId,
UserLimit = args.UserLimit.IsSpecified ? (args.UserLimit.Value ?? 0) : Optional.Create<int>()
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
Expand Down Expand Up @@ -86,7 +89,7 @@ internal static class ChannelHelper
}

//Messages
public static async Task<RestMessage> GetMessageAsync(IMessageChannel channel, BaseDiscordClient client,
public static async Task<RestMessage> GetMessageAsync(IMessageChannel channel, BaseDiscordClient client,
ulong id, RequestOptions options)
{
var guildId = (channel as IGuildChannel)?.GuildId;
Expand All @@ -97,7 +100,7 @@ internal static class ChannelHelper
var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
return RestMessage.Create(client, channel, author, model);
}
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
ulong? fromMessageId, Direction dir, int limit, RequestOptions options)
{
if (dir == Direction.Around)
Expand All @@ -123,7 +126,7 @@ internal static class ChannelHelper
foreach (var model in models)
{
var author = GetAuthor(client, guild, model.Author.Value, model.WebhookId.ToNullable());
builder.Add(RestMessage.Create(client, channel, author, model));
builder.Add(RestMessage.Create(client, channel, author, model));
}
return builder.ToImmutable();
},
Expand Down Expand Up @@ -179,7 +182,7 @@ internal static class ChannelHelper
var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS };
var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
}
}

public static async Task DeleteMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
IEnumerable<ulong> messageIds, RequestOptions options)
Expand Down Expand Up @@ -276,7 +279,7 @@ internal static class ChannelHelper
{
await client.ApiClient.TriggerTypingIndicatorAsync(channel.Id, options).ConfigureAwait(false);
}
public static IDisposable EnterTypingState(IMessageChannel channel, BaseDiscordClient client,
public static IDisposable EnterTypingState(IMessageChannel channel, BaseDiscordClient client,
RequestOptions options)
=> new TypingNotifier(client, channel, options);

Expand Down
3 changes: 2 additions & 1 deletion src/Discord.Net.Rest/Entities/Channels/ChannelType.cs
Expand Up @@ -5,6 +5,7 @@ public enum ChannelType
Text = 0,
DM = 1,
Voice = 2,
Group = 3
Group = 3,
Category = 4
}
}
18 changes: 10 additions & 8 deletions src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs
Expand Up @@ -16,6 +16,8 @@ public class RestGuildChannel : RestChannel, IGuildChannel, IUpdateable
internal IGuild Guild { get; }
public string Name { get; private set; }
public int Position { get; private set; }
public ulong? ParentId { get; private set; }
public Task<IGuildChannel> GetParentChannelAsync() => ParentId == null ? null : Guild.GetChannelAsync(ParentId.Value);

public ulong GuildId => Guild.Id;

Expand Down Expand Up @@ -61,7 +63,7 @@ public async Task ModifyAsync(Action<GuildChannelProperties> func, RequestOption
}
public Task DeleteAsync(RequestOptions options = null)
=> ChannelHelper.DeleteAsync(this, Discord, options);

public OverwritePermissions? GetPermissionOverwrite(IUser user)
{
for (int i = 0; i < _overwrites.Length; i++)
Expand Down Expand Up @@ -139,20 +141,20 @@ async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(R
=> await GetInvitesAsync(options).ConfigureAwait(false);
async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)
=> await CreateInviteAsync(maxAge, maxUses, isTemporary, isUnique, options).ConfigureAwait(false);
OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IRole role)

OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IRole role)
=> GetPermissionOverwrite(role);
OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IUser user)
=> GetPermissionOverwrite(user);
async Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options)
async Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options)
=> await AddPermissionOverwriteAsync(role, permissions, options).ConfigureAwait(false);
async Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options)
async Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options)
=> await AddPermissionOverwriteAsync(user, permissions, options).ConfigureAwait(false);
async Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role, RequestOptions options)
async Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role, RequestOptions options)
=> await RemovePermissionOverwriteAsync(role, options).ConfigureAwait(false);
async Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user, RequestOptions options)
async Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user, RequestOptions options)
=> await RemovePermissionOverwriteAsync(user, options).ConfigureAwait(false);

IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>(); //Overriden //Overriden in Text/Voice
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
Expand Down
38 changes: 38 additions & 0 deletions src/Discord.Net.Rest/Entities/Channels/RestGuildChannelCategory.cs
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;

namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestGuildChannelCategory : RestGuildChannel, IGuildChannelCategory
{
public string Mention => MentionUtils.MentionChannel(Id);

internal RestGuildChannelCategory(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, guild, id)
{
}
internal new static RestGuildChannelCategory Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestGuildChannelCategory(discord, guild, model.Id);
entity.Update(model);
return entity;
}
internal override void Update(Model model)
{
base.Update(model);
}

public Task<RestGuildUser> GetUserAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetUserAsync(this, Guild, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null)
=> ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options);

private string DebuggerDisplay => $"{Name} ({Id}, Text)";
}
}
24 changes: 18 additions & 6 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Expand Up @@ -23,7 +23,7 @@ public class RestGuild : RestEntity<ulong>, IGuild, IUpdateable
public VerificationLevel VerificationLevel { get; private set; }
public MfaLevel MfaLevel { get; private set; }
public DefaultMessageNotifications DefaultMessageNotifications { get; private set; }

public ulong? AFKChannelId { get; private set; }
public ulong? EmbedChannelId { get; private set; }
public ulong? SystemChannelId { get; private set; }
Expand Down Expand Up @@ -114,7 +114,7 @@ public async Task ModifyAsync(Action<GuildProperties> func, RequestOptions optio
Update(model);
}
public async Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
{
{
var model = await GuildHelper.ModifyEmbedAsync(this, Discord, func, options).ConfigureAwait(false);
Update(model);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
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);
Expand All @@ -176,6 +176,11 @@ public async Task<IReadOnlyCollection<RestVoiceChannel>> GetVoiceChannelsAsync(R
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<IReadOnlyCollection<RestGuildChannelCategory>> GetChannelCategoriesAsync(RequestOptions options = null)
{
var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
return channels.Select(x => x as RestGuildChannelCategory).Where(x => x != null).ToImmutableArray();
}

public async Task<RestVoiceChannel> GetAFKChannelAsync(RequestOptions options = null)
{
Expand All @@ -199,7 +204,7 @@ public async Task<RestTextChannel> GetDefaultChannelAsync(RequestOptions options
public async Task<RestGuildChannel> GetEmbedChannelAsync(RequestOptions options = null)
{
var embedId = EmbedChannelId;
if (embedId.HasValue)
if (embedId.HasValue)
return await GuildHelper.GetChannelAsync(this, Discord, embedId.Value, options).ConfigureAwait(false);
return null;
}
Expand Down Expand Up @@ -236,7 +241,7 @@ public RestRole GetRole(ulong id)
return null;
}

public async Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
public async Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
bool isHoisted = false, RequestOptions options = null)
{
var role = await GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options).ConfigureAwait(false);
Expand Down Expand Up @@ -304,6 +309,13 @@ async Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(Cach
else
return ImmutableArray.Create<IVoiceChannel>();
}
async Task<IReadOnlyCollection<IGuildChannelCategory>> IGuild.GetChannelCategoriesAsync(CacheMode mode , RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetChannelCategoriesAsync(options).ConfigureAwait(false);
else
return null;
}
async Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
Expand Down Expand Up @@ -352,7 +364,7 @@ async Task<IGuildIntegration> IGuild.CreateIntegrationAsync(ulong id, string typ
async Task<IReadOnlyCollection<IInviteMetadata>> IGuild.GetInvitesAsync(RequestOptions options)
=> await GetInvitesAsync(options).ConfigureAwait(false);

IRole IGuild.GetRole(ulong id)
IRole IGuild.GetRole(ulong id)
=> GetRole(id);
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
Expand Down

0 comments on commit e18bd8c

Please sign in to comment.