[Feature] Follow news channels (#2590)

* initial commit

* Apply suggestions from code review

---------

Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>
This commit is contained in:
Misha133
2023-02-09 18:45:04 +03:00
committed by GitHub
parent 2ea8af1dde
commit bb056dfec0
6 changed files with 52 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
using System.Threading.Tasks;
namespace Discord namespace Discord
{ {
/// <summary> /// <summary>
@@ -5,5 +7,12 @@ namespace Discord
/// </summary> /// </summary>
public interface INewsChannel : ITextChannel public interface INewsChannel : ITextChannel
{ {
/// <summary>
/// Follow this channel to send messages to a target channel.
/// </summary>
/// <returns>
/// The Id of the created webhook.
/// </returns>
Task<ulong> FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options);
} }
} }

View File

@@ -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; }
}

View File

@@ -1146,6 +1146,16 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId); var ids = new BucketIds(channelId: channelId);
await SendAsync("POST", () => $"channels/{channelId}/messages/{messageId}/crosspost", ids, options: options).ConfigureAwait(false); await SendAsync("POST", () => $"channels/{channelId}/messages/{messageId}/crosspost", ids, options: options).ConfigureAwait(false);
} }
public async Task<FollowedChannel> 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<FollowedChannel>("POST", () => $"channels/{newsChannelId}/followers", new { webhook_channel_id = followingChannelId}, ids, options: options).ConfigureAwait(false);
}
#endregion #endregion
#region Channel Permissions #region Channel Permissions

View File

@@ -590,6 +590,12 @@ namespace Discord.Rest
return models.Select(x => RestWebhook.Create(client, channel, x)) return models.Select(x => RestWebhook.Create(client, channel, x))
.ToImmutableArray(); .ToImmutableArray();
} }
public static async Task<ulong> FollowAnnouncementChannelAsync(INewsChannel newsChannel, ulong channelId, BaseDiscordClient client, RequestOptions options)
{
var model = await client.ApiClient.FollowChannelAsync(newsChannel.Id, channelId, options);
return model.WebhookId;
}
#endregion #endregion
#region Categories #region Categories

View File

@@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Model = Discord.API.Channel; using Model = Discord.API.Channel;
namespace Discord.Rest namespace Discord.Rest
@@ -15,7 +16,7 @@ namespace Discord.Rest
public class RestNewsChannel : RestTextChannel, INewsChannel public class RestNewsChannel : RestTextChannel, INewsChannel
{ {
internal RestNewsChannel(BaseDiscordClient discord, IGuild guild, ulong id) 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) internal new static RestNewsChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
@@ -25,5 +26,9 @@ namespace Discord.Rest
return entity; return entity;
} }
public override int SlowModeInterval => throw new NotSupportedException("News channels do not support Slow Mode."); public override int SlowModeInterval => throw new NotSupportedException("News channels do not support Slow Mode.");
/// <inheritdoc />
public Task<ulong> FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options = null)
=> ChannelHelper.FollowAnnouncementChannelAsync(this, channelId, Discord, options);
} }
} }

View File

@@ -1,3 +1,4 @@
using Discord.Rest;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@@ -36,5 +37,13 @@ namespace Discord.WebSocket
public override int SlowModeInterval public override int SlowModeInterval
=> throw new NotSupportedException("News channels do not support Slow Mode."); => throw new NotSupportedException("News channels do not support Slow Mode.");
/// <inheritdoc cref="INewsChannel.FollowAnnouncementChannelAsync"/>
public Task<ulong> FollowAnnouncementChannelAsync(ITextChannel channel, RequestOptions options = null)
=> FollowAnnouncementChannelAsync(channel.Id, options);
/// <inheritdoc />
public Task<ulong> FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options = null)
=> ChannelHelper.FollowAnnouncementChannelAsync(this, channelId, Discord, options);
} }
} }