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); + } }