Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify parameters on channel creation #1020

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down Expand Up @@ -91,9 +91,9 @@ public interface IGuild : IDeletable, ISnowflakeEntity
Task<ITextChannel> GetDefaultChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IGuildChannel> GetEmbedChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Creates a new text channel. </summary>
Task<ITextChannel> CreateTextChannelAsync(string name, RequestOptions options = null);
Task<ITextChannel> CreateTextChannelAsync(string name, RequestOptions options = null, Action<TextChannelProperties> func = null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think having the action being the last parameter is worth avoiding making a breaking change. Just way too inconsistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this should go before RequestOptions for sure.

/// <summary> Creates a new voice channel. </summary>
Task<IVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null);
Task<IVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null, Action<VoiceChannelProperties> func = null);
/// <summary> Creates a new channel category. </summary>
Task<ICategoryChannel> CreateCategoryAsync(string name, RequestOptions options = null);

Expand Down Expand Up @@ -135,4 +135,4 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// <summary> Deletes an existing emote from this guild. </summary>
Task DeleteEmoteAsync(GuildEmote emote, RequestOptions options = null);
}
}
}
13 changes: 12 additions & 1 deletion src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma warning disable CS1591
#pragma warning disable CS1591
using Newtonsoft.Json;

namespace Discord.API.Rest
Expand All @@ -10,9 +10,20 @@ internal class CreateGuildChannelParams
public string Name { get; }
[JsonProperty("type")]
public ChannelType Type { get; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field's actually optional.
https://discordapp.com/developers/docs/resources/guild#create-guild-channel

All parameters for this endpoint are optional excluding name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left that how I found it.

[JsonProperty("parent_id")]
public Optional<ulong?> CategoryId { get; set; }

//Text channels
[JsonProperty("topic")]
public Optional<string> Topic { get; set; }
[JsonProperty("nsfw")]
public Optional<bool> IsNsfw { get; set; }

//Voice channels
[JsonProperty("bitrate")]
public Optional<int> Bitrate { get; set; }
[JsonProperty("user_limit")]
public Optional<int?> UserLimit { get; set; }

public CreateGuildChannelParams(string name, ChannelType type)
{
Expand Down
26 changes: 21 additions & 5 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Discord.API.Rest;
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -140,20 +140,36 @@ public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync
return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray();
}
public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options)
string name, RequestOptions options, Action<TextChannelProperties> func = null)
{
if (name == null) throw new ArgumentNullException(nameof(name));

var args = new CreateGuildChannelParams(name, ChannelType.Text);
var props = new TextChannelProperties();
func?.Invoke(props);

var args = new CreateGuildChannelParams(name, ChannelType.Text)
{
CategoryId = props.CategoryId,
Topic = props.Topic,
IsNsfw = props.IsNsfw
};
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestTextChannel.Create(client, guild, model);
}
public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options)
string name, RequestOptions options, Action<VoiceChannelProperties> func = null)
{
if (name == null) throw new ArgumentNullException(nameof(name));

var args = new CreateGuildChannelParams(name, ChannelType.Voice);
var props = new VoiceChannelProperties();
func?.Invoke(props);

var args = new CreateGuildChannelParams(name, ChannelType.Voice)
{
CategoryId = props.CategoryId,
Bitrate = props.Bitrate,
UserLimit = props.UserLimit
};
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestVoiceChannel.Create(client, guild, model);
}
Expand Down
18 changes: 9 additions & 9 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -218,10 +218,10 @@ public async Task<RestTextChannel> GetSystemChannelAsync(RequestOptions options
}
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)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options);
public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null, Action<TextChannelProperties> func = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null, Action<VoiceChannelProperties> func = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func);
public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, RequestOptions options = null)
=> GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options);

Expand Down Expand Up @@ -369,10 +369,10 @@ async Task<ITextChannel> IGuild.GetSystemChannelAsync(CacheMode mode, RequestOpt
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)
=> await CreateVoiceChannelAsync(name, options).ConfigureAwait(false);
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options, Action<TextChannelProperties> func)
=> await CreateTextChannelAsync(name, options, func).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options, Action<VoiceChannelProperties> func)
=> await CreateVoiceChannelAsync(name, options, func).ConfigureAwait(false);
async Task<ICategoryChannel> IGuild.CreateCategoryAsync(string name, RequestOptions options)
=> await CreateCategoryChannelAsync(name, options).ConfigureAwait(false);

Expand Down
16 changes: 8 additions & 8 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ 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)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options);
public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null, Action<TextChannelProperties> func = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null, Action<VoiceChannelProperties> func = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func);
public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, RequestOptions options = null)
=> GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options);

Expand Down Expand Up @@ -664,10 +664,10 @@ Task<IGuildChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions o
=> Task.FromResult<IGuildChannel>(EmbedChannel);
Task<ITextChannel> IGuild.GetSystemChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<ITextChannel>(SystemChannel);
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options)
=> await CreateTextChannelAsync(name, options).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
=> await CreateVoiceChannelAsync(name, options).ConfigureAwait(false);
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options, Action<TextChannelProperties> func)
=> await CreateTextChannelAsync(name, options, func).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options, Action<VoiceChannelProperties> func)
=> await CreateVoiceChannelAsync(name, options, func).ConfigureAwait(false);
async Task<ICategoryChannel> IGuild.CreateCategoryAsync(string name, RequestOptions options)
=> await CreateCategoryChannelAsync(name, options).ConfigureAwait(false);

Expand Down