Skip to content

Commit

Permalink
Allow creating announcement channels (#2837)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 committed Feb 11, 2024
1 parent 4d7e384 commit f1777de
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ public class TextChannelProperties : GuildChannelProperties
/// <exception cref="ArgumentOutOfRangeException">Thrown if the value does not fall within [0, 21600].</exception>
public Optional<int> DefaultSlowModeInterval { get; set; }

/// <summary>
/// Gets or sets the type of the channel. Only applicable for <see cref="ChannelType.Text"/> or <see cref="ChannelType.News"/> channels.
/// </summary>
public Optional<ChannelType> ChannelType { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,21 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// text channel.
/// </returns>
Task<ITextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null);
/// <summary>
/// Creates a new announcement channel in this guild.
/// </summary>
/// <remarks>
/// Announcement channels are only available in Community guilds.
/// </remarks>
/// <param name="name">The new name for the announcement channel.</param>
/// <param name="func">The delegate containing the properties to be applied to the channel upon its creation.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// announcement channel.
/// </returns>
Task<INewsChannel> CreateNewsChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null);

/// <summary>
/// Creates a new voice channel in this guild.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ internal class ModifyTextChannelParams : ModifyGuildChannelParams

[JsonProperty("default_thread_rate_limit_per_user")]
public Optional<int> DefaultSlowModeInterval { get; set; }

[JsonProperty("type")]
public Optional<ChannelType> Type { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordClient cl
func(args);
var apiArgs = new API.Rest.ModifyTextChannelParams
{
Type = args.ChannelType,
Name = args.Name,
Position = args.Position,
CategoryId = args.CategoryId,
Expand Down
33 changes: 33 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,39 @@ public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, B
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestTextChannel.Create(client, guild, model);
}

/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null" />.</exception>
public static async Task<RestNewsChannel> CreateNewsChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options, Action<TextChannelProperties> func = null)
{
if (name == null)
throw new ArgumentNullException(paramName: nameof(name));

var props = new TextChannelProperties();
func?.Invoke(props);

var args = new CreateGuildChannelParams(name, ChannelType.News)
{
CategoryId = props.CategoryId,
Topic = props.Topic,
IsNsfw = props.IsNsfw,
Position = props.Position,
SlowModeInterval = props.SlowModeInterval,
Overwrites = props.PermissionOverwrites.IsSpecified
? props.PermissionOverwrites.Value.Select(overwrite => new API.Overwrite
{
TargetId = overwrite.TargetId,
TargetType = overwrite.TargetType,
Allow = overwrite.Permissions.AllowValue.ToString(),
Deny = overwrite.Permissions.DenyValue.ToString()
}).ToArray()
: Optional.Create<API.Overwrite[]>(),
DefaultAutoArchiveDuration = props.AutoArchiveDuration
};
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestNewsChannel.Create(client, guild, model);
}

/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null" />.</exception>
public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options, Action<VoiceChannelProperties> func = null)
Expand Down
8 changes: 8 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,11 @@ public async Task<RestTextChannel> GetPublicUpdatesChannelAsync(RequestOptions o
/// </returns>
public Task<RestTextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);

/// <inheritdoc cref="IGuild.CreateNewsChannelAsync"/>
public Task<RestNewsChannel> CreateNewsChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateNewsChannelAsync(this, Discord, name, options, func);

/// <summary>
/// Creates a voice channel with the provided name.
/// </summary>
Expand Down Expand Up @@ -1547,6 +1552,9 @@ async Task<ITextChannel> IGuild.GetPublicUpdatesChannelAsync(CacheMode mode, Req
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, Action<TextChannelProperties> func, RequestOptions options)
=> await CreateTextChannelAsync(name, func, options).ConfigureAwait(false);
/// <inheritdoc />
async Task<INewsChannel> IGuild.CreateNewsChannelAsync(string name, Action<TextChannelProperties> func, RequestOptions options)
=> await CreateNewsChannelAsync(name, func, options).ConfigureAwait(false);
/// <inheritdoc />
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func, RequestOptions options)
=> await CreateVoiceChannelAsync(name, func, options).ConfigureAwait(false);
/// <inheritdoc />
Expand Down
10 changes: 10 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,11 @@ public SocketMediaChannel GetMediaChannel(ulong id)
/// </returns>
public Task<RestTextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);

/// <inheritdoc cref="IGuild.CreateNewsChannelAsync"/>
public Task<RestNewsChannel> CreateNewsChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateNewsChannelAsync(this, Discord, name, options, func);

/// <summary>
/// Creates a new voice channel in this guild.
/// </summary>
Expand Down Expand Up @@ -2132,6 +2137,11 @@ Task<IReadOnlyCollection<IMediaChannel>> IGuild.GetMediaChannelsAsync(CacheMode
/// <inheritdoc />
async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, Action<TextChannelProperties> func, RequestOptions options)
=> await CreateTextChannelAsync(name, func, options).ConfigureAwait(false);

/// <inheritdoc />
async Task<INewsChannel> IGuild.CreateNewsChannelAsync(string name, Action<TextChannelProperties> func, RequestOptions options)
=> await CreateNewsChannelAsync(name, func, options).ConfigureAwait(false);

/// <inheritdoc />
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func, RequestOptions options)
=> await CreateVoiceChannelAsync(name, func, options).ConfigureAwait(false);
Expand Down

0 comments on commit f1777de

Please sign in to comment.