From bb056dfec09dd7976838e70f3e703ac04b26255d Mon Sep 17 00:00:00 2001
From: Misha133 <61027276+Misha-133@users.noreply.github.com>
Date: Thu, 9 Feb 2023 18:45:04 +0300
Subject: [PATCH] [Feature] Follow news channels (#2590)
* initial commit
* Apply suggestions from code review
---------
Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>
---
.../Entities/Channels/INewsChannel.cs | 9 +++++++++
src/Discord.Net.Rest/API/Common/FollowedChannel.cs | 12 ++++++++++++
src/Discord.Net.Rest/DiscordRestApiClient.cs | 10 ++++++++++
.../Entities/Channels/ChannelHelper.cs | 6 ++++++
.../Entities/Channels/RestNewsChannel.cs | 7 ++++++-
.../Entities/Channels/SocketNewsChannel.cs | 9 +++++++++
6 files changed, 52 insertions(+), 1 deletion(-)
create mode 100644 src/Discord.Net.Rest/API/Common/FollowedChannel.cs
diff --git a/src/Discord.Net.Core/Entities/Channels/INewsChannel.cs b/src/Discord.Net.Core/Entities/Channels/INewsChannel.cs
index a1223b48b..b3c9fe8e0 100644
--- a/src/Discord.Net.Core/Entities/Channels/INewsChannel.cs
+++ b/src/Discord.Net.Core/Entities/Channels/INewsChannel.cs
@@ -1,3 +1,5 @@
+using System.Threading.Tasks;
+
namespace Discord
{
///
@@ -5,5 +7,12 @@ namespace Discord
///
public interface INewsChannel : ITextChannel
{
+ ///
+ /// Follow this channel to send messages to a target channel.
+ ///
+ ///
+ /// The Id of the created webhook.
+ ///
+ Task FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options);
}
}
diff --git a/src/Discord.Net.Rest/API/Common/FollowedChannel.cs b/src/Discord.Net.Rest/API/Common/FollowedChannel.cs
new file mode 100644
index 000000000..ad42568fc
--- /dev/null
+++ b/src/Discord.Net.Rest/API/Common/FollowedChannel.cs
@@ -0,0 +1,12 @@
+using Newtonsoft.Json;
+
+namespace Discord.API;
+
+internal class FollowedChannel
+{
+ [JsonProperty("channel_id")]
+ public ulong ChannelId { get; set; }
+
+ [JsonProperty("webhook_id")]
+ public ulong WebhookId { get; set; }
+}
diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs
index 955952d00..64964adf0 100644
--- a/src/Discord.Net.Rest/DiscordRestApiClient.cs
+++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs
@@ -1146,6 +1146,16 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
await SendAsync("POST", () => $"channels/{channelId}/messages/{messageId}/crosspost", ids, options: options).ConfigureAwait(false);
}
+
+ public async Task FollowChannelAsync(ulong newsChannelId, ulong followingChannelId, RequestOptions options = null)
+ {
+ Preconditions.NotEqual(newsChannelId, 0, nameof(newsChannelId));
+ Preconditions.NotEqual(followingChannelId, 0, nameof(followingChannelId));
+ options = RequestOptions.CreateOrClone(options);
+
+ var ids = new BucketIds(channelId: newsChannelId);
+ return await SendJsonAsync("POST", () => $"channels/{newsChannelId}/followers", new { webhook_channel_id = followingChannelId}, ids, options: options).ConfigureAwait(false);
+ }
#endregion
#region Channel Permissions
diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
index 1baa84c50..2b5511e0f 100644
--- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
@@ -590,6 +590,12 @@ namespace Discord.Rest
return models.Select(x => RestWebhook.Create(client, channel, x))
.ToImmutableArray();
}
+
+ public static async Task FollowAnnouncementChannelAsync(INewsChannel newsChannel, ulong channelId, BaseDiscordClient client, RequestOptions options)
+ {
+ var model = await client.ApiClient.FollowChannelAsync(newsChannel.Id, channelId, options);
+ return model.WebhookId;
+ }
#endregion
#region Categories
diff --git a/src/Discord.Net.Rest/Entities/Channels/RestNewsChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestNewsChannel.cs
index fad3358dc..7554c69dd 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestNewsChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestNewsChannel.cs
@@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+
using Model = Discord.API.Channel;
namespace Discord.Rest
@@ -15,7 +16,7 @@ namespace Discord.Rest
public class RestNewsChannel : RestTextChannel, INewsChannel
{
internal RestNewsChannel(BaseDiscordClient discord, IGuild guild, ulong id)
- :base(discord, guild, id)
+ : base(discord, guild, id)
{
}
internal new static RestNewsChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
@@ -25,5 +26,9 @@ namespace Discord.Rest
return entity;
}
public override int SlowModeInterval => throw new NotSupportedException("News channels do not support Slow Mode.");
+
+ ///
+ public Task FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options = null)
+ => ChannelHelper.FollowAnnouncementChannelAsync(this, channelId, Discord, options);
}
}
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs
index 81e152530..85990150d 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs
@@ -1,3 +1,4 @@
+using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -36,5 +37,13 @@ namespace Discord.WebSocket
public override int SlowModeInterval
=> throw new NotSupportedException("News channels do not support Slow Mode.");
+ ///
+ public Task FollowAnnouncementChannelAsync(ITextChannel channel, RequestOptions options = null)
+ => FollowAnnouncementChannelAsync(channel.Id, options);
+
+ ///
+ public Task FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options = null)
+ => ChannelHelper.FollowAnnouncementChannelAsync(this, channelId, Discord, options);
+
}
}