Browse Source

[Feature] Follow news channels (#2590)

* initial commit

* Apply suggestions from code review

---------

Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>
pull/2594/head
Misha133 GitHub 2 years ago
parent
commit
bb056dfec0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 1 deletions
  1. +9
    -0
      src/Discord.Net.Core/Entities/Channels/INewsChannel.cs
  2. +12
    -0
      src/Discord.Net.Rest/API/Common/FollowedChannel.cs
  3. +10
    -0
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  4. +6
    -0
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  5. +6
    -1
      src/Discord.Net.Rest/Entities/Channels/RestNewsChannel.cs
  6. +9
    -0
      src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs

+ 9
- 0
src/Discord.Net.Core/Entities/Channels/INewsChannel.cs View File

@@ -1,3 +1,5 @@
using System.Threading.Tasks;

namespace Discord
{
/// <summary>
@@ -5,5 +7,12 @@ namespace Discord
/// </summary>
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);
}
}

+ 12
- 0
src/Discord.Net.Rest/API/Common/FollowedChannel.cs 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; }
}

+ 10
- 0
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

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

#region Channel Permissions


+ 6
- 0
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -590,6 +590,12 @@ namespace Discord.Rest
return models.Select(x => RestWebhook.Create(client, channel, x))
.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

#region Categories


+ 6
- 1
src/Discord.Net.Rest/Entities/Channels/RestNewsChannel.cs View File

@@ -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.");

/// <inheritdoc />
public Task<ulong> FollowAnnouncementChannelAsync(ulong channelId, RequestOptions options = null)
=> ChannelHelper.FollowAnnouncementChannelAsync(this, channelId, Discord, options);
}
}

+ 9
- 0
src/Discord.Net.WebSocket/Entities/Channels/SocketNewsChannel.cs View File

@@ -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.");

/// <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);

}
}

Loading…
Cancel
Save