From f1777de1649563edb13bf03aa9bd02e6870bbf47 Mon Sep 17 00:00:00 2001
From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>
Date: Sun, 11 Feb 2024 23:48:40 +0300
Subject: [PATCH] Allow creating announcement channels (#2837)
---
.../Channels/TextChannelProperties.cs | 4 +++
.../Entities/Guilds/IGuild.cs | 15 +++++++++
.../API/Rest/ModifyTextChannelParams.cs | 3 ++
.../Entities/Channels/ChannelHelper.cs | 1 +
.../Entities/Guilds/GuildHelper.cs | 33 +++++++++++++++++++
.../Entities/Guilds/RestGuild.cs | 8 +++++
.../Entities/Guilds/SocketGuild.cs | 10 ++++++
7 files changed, 74 insertions(+)
diff --git a/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs b/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs
index dac86d2d..a0d6846a 100644
--- a/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs
+++ b/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs
@@ -59,5 +59,9 @@ namespace Discord
/// Thrown if the value does not fall within [0, 21600].
public Optional DefaultSlowModeInterval { get; set; }
+ ///
+ /// Gets or sets the type of the channel. Only applicable for or channels.
+ ///
+ public Optional ChannelType { get; set; }
}
}
diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs
index 65f01480..143efd61 100644
--- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs
+++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs
@@ -800,6 +800,21 @@ namespace Discord
/// text channel.
///
Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null);
+ ///
+ /// Creates a new announcement channel in this guild.
+ ///
+ ///
+ /// Announcement channels are only available in Community guilds.
+ ///
+ /// The new name for the announcement channel.
+ /// The delegate containing the properties to be applied to the channel upon its creation.
+ /// The options to be used when sending the request.
+ ///
+ /// A task that represents the asynchronous creation operation. The task result contains the newly created
+ /// announcement channel.
+ ///
+ Task CreateNewsChannelAsync(string name, Action func = null, RequestOptions options = null);
+
///
/// Creates a new voice channel in this guild.
///
diff --git a/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs
index 6952b1a9..7884d748 100644
--- a/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs
+++ b/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs
@@ -16,5 +16,8 @@ namespace Discord.API.Rest
[JsonProperty("default_thread_rate_limit_per_user")]
public Optional DefaultSlowModeInterval { get; set; }
+
+ [JsonProperty("type")]
+ public Optional Type { get; set; }
}
}
diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
index 3063ccfd..f09616d3 100644
--- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
@@ -49,6 +49,7 @@ namespace Discord.Rest
func(args);
var apiArgs = new API.Rest.ModifyTextChannelParams
{
+ Type = args.ChannelType,
Name = args.Name,
Position = args.Position,
CategoryId = args.CategoryId,
diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
index 7558ee4c..4627e50f 100644
--- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
@@ -280,6 +280,39 @@ namespace Discord.Rest
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestTextChannel.Create(client, guild, model);
}
+
+ /// is .
+ public static async Task CreateNewsChannelAsync(IGuild guild, BaseDiscordClient client,
+ string name, RequestOptions options, Action 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(),
+ DefaultAutoArchiveDuration = props.AutoArchiveDuration
+ };
+ var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
+ return RestNewsChannel.Create(client, guild, model);
+ }
+
/// is .
public static async Task CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options, Action func = null)
diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
index 0cbb9a05..7cb9d0dc 100644
--- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
+++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
@@ -760,6 +760,11 @@ namespace Discord.Rest
///
public Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
+
+ ///
+ public Task CreateNewsChannelAsync(string name, Action func = null, RequestOptions options = null)
+ => GuildHelper.CreateNewsChannelAsync(this, Discord, name, options, func);
+
///
/// Creates a voice channel with the provided name.
///
@@ -1547,6 +1552,9 @@ namespace Discord.Rest
async Task IGuild.CreateTextChannelAsync(string name, Action func, RequestOptions options)
=> await CreateTextChannelAsync(name, func, options).ConfigureAwait(false);
///
+ async Task IGuild.CreateNewsChannelAsync(string name, Action func, RequestOptions options)
+ => await CreateNewsChannelAsync(name, func, options).ConfigureAwait(false);
+ ///
async Task IGuild.CreateVoiceChannelAsync(string name, Action func, RequestOptions options)
=> await CreateVoiceChannelAsync(name, func, options).ConfigureAwait(false);
///
diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
index 84c39d47..e59673ea 100644
--- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
+++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
@@ -840,6 +840,11 @@ namespace Discord.WebSocket
///
public Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
+
+ ///
+ public Task CreateNewsChannelAsync(string name, Action func = null, RequestOptions options = null)
+ => GuildHelper.CreateNewsChannelAsync(this, Discord, name, options, func);
+
///
/// Creates a new voice channel in this guild.
///
@@ -2132,6 +2137,11 @@ namespace Discord.WebSocket
///
async Task IGuild.CreateTextChannelAsync(string name, Action func, RequestOptions options)
=> await CreateTextChannelAsync(name, func, options).ConfigureAwait(false);
+
+ ///
+ async Task IGuild.CreateNewsChannelAsync(string name, Action func, RequestOptions options)
+ => await CreateNewsChannelAsync(name, func, options).ConfigureAwait(false);
+
///
async Task IGuild.CreateVoiceChannelAsync(string name, Action func, RequestOptions options)
=> await CreateVoiceChannelAsync(name, func, options).ConfigureAwait(false);