From 75f3c91f9a58df920211f683c69cfbd0df2bfde2 Mon Sep 17 00:00:00 2001
From: Misha133 <61027276+Misha-133@users.noreply.github.com>
Date: Thu, 9 Feb 2023 14:36:35 +0300
Subject: [PATCH] IIntegrationChannel & create webhooks in forums (#2582)
---
.../Entities/Channels/IForumChannel.cs | 5 ++-
.../Entities/Channels/IIntegrationChannel.cs | 44 +++++++++++++++++++
.../Entities/Channels/ITextChannel.cs | 33 +-------------
.../Entities/Webhooks/IWebhook.cs | 2 +-
.../Entities/Channels/ChannelHelper.cs | 6 +--
.../Entities/Channels/RestForumChannel.cs | 27 ++++++++++++
.../Entities/Channels/RestTextChannel.cs | 13 ++++--
.../Entities/Webhooks/RestWebhook.cs | 8 ++--
.../Entities/Channels/SocketForumChannel.cs | 29 ++++++++++++
.../Entities/Channels/SocketTextChannel.cs | 12 +++--
.../Entities/Webhooks/RestInternalWebhook.cs | 2 +-
11 files changed, 131 insertions(+), 50 deletions(-)
create mode 100644 src/Discord.Net.Core/Entities/Channels/IIntegrationChannel.cs
diff --git a/src/Discord.Net.Core/Entities/Channels/IForumChannel.cs b/src/Discord.Net.Core/Entities/Channels/IForumChannel.cs
index 09fb0fb7e..9de4b7b35 100644
--- a/src/Discord.Net.Core/Entities/Channels/IForumChannel.cs
+++ b/src/Discord.Net.Core/Entities/Channels/IForumChannel.cs
@@ -7,7 +7,10 @@ using System.Threading.Tasks;
namespace Discord
{
- public interface IForumChannel : IGuildChannel, IMentionable, INestedChannel
+ ///
+ /// Represents a forum channel in a guild that can create posts.
+ ///
+ public interface IForumChannel : IMentionable, INestedChannel, IIntegrationChannel
{
///
/// Gets a value that indicates whether the channel is NSFW.
diff --git a/src/Discord.Net.Core/Entities/Channels/IIntegrationChannel.cs b/src/Discord.Net.Core/Entities/Channels/IIntegrationChannel.cs
new file mode 100644
index 000000000..7c28ea9f8
--- /dev/null
+++ b/src/Discord.Net.Core/Entities/Channels/IIntegrationChannel.cs
@@ -0,0 +1,44 @@
+using System.Collections.Generic;
+using System.IO;
+using System.Threading.Tasks;
+
+namespace Discord;
+
+///
+/// Represents a channel in a guild that can create webhooks.
+///
+public interface IIntegrationChannel : IGuildChannel
+{
+ ///
+ /// Creates a webhook in this channel.
+ ///
+ /// The name of the webhook.
+ /// The avatar of the webhook.
+ /// The options to be used when sending the request.
+ ///
+ /// A task that represents the asynchronous creation operation. The task result contains the newly created
+ /// webhook.
+ ///
+ Task CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null);
+
+ ///
+ /// Gets a webhook available in this channel.
+ ///
+ /// The identifier of the webhook.
+ /// The options to be used when sending the request.
+ ///
+ /// A task that represents the asynchronous get operation. The task result contains a webhook associated
+ /// with the identifier; null if the webhook is not found.
+ ///
+ Task GetWebhookAsync(ulong id, RequestOptions options = null);
+
+ ///
+ /// Gets the webhooks available in this channel.
+ ///
+ /// The options to be used when sending the request.
+ ///
+ /// A task that represents the asynchronous get operation. The task result contains a read-only collection
+ /// of webhooks that is available in this channel.
+ ///
+ Task> GetWebhooksAsync(RequestOptions options = null);
+}
diff --git a/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs b/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs
index f5605d84c..e27317b00 100644
--- a/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs
+++ b/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs
@@ -8,7 +8,7 @@ namespace Discord
///
/// Represents a generic channel in a guild that can send and receive messages.
///
- public interface ITextChannel : IMessageChannel, IMentionable, INestedChannel
+ public interface ITextChannel : IMessageChannel, IMentionable, INestedChannel, IIntegrationChannel
{
///
/// Gets a value that indicates whether the channel is NSFW.
@@ -94,37 +94,6 @@ namespace Discord
///
///
Task ModifyAsync(Action func, RequestOptions options = null);
-
- ///
- /// Creates a webhook in this text channel.
- ///
- /// The name of the webhook.
- /// The avatar of the webhook.
- /// The options to be used when sending the request.
- ///
- /// A task that represents the asynchronous creation operation. The task result contains the newly created
- /// webhook.
- ///
- Task CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null);
- ///
- /// Gets a webhook available in this text channel.
- ///
- /// The identifier of the webhook.
- /// The options to be used when sending the request.
- ///
- /// A task that represents the asynchronous get operation. The task result contains a webhook associated
- /// with the identifier; null if the webhook is not found.
- ///
- Task GetWebhookAsync(ulong id, RequestOptions options = null);
- ///
- /// Gets the webhooks available in this text channel.
- ///
- /// The options to be used when sending the request.
- ///
- /// A task that represents the asynchronous get operation. The task result contains a read-only collection
- /// of webhooks that is available in this channel.
- ///
- Task> GetWebhooksAsync(RequestOptions options = null);
///
/// Creates a thread within this .
diff --git a/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs b/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
index d5bc70d71..e0aebae39 100644
--- a/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
+++ b/src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
@@ -29,7 +29,7 @@ namespace Discord
///
/// Gets the channel for this webhook.
///
- ITextChannel Channel { get; }
+ IIntegrationChannel Channel { get; }
///
/// Gets the ID of the channel for this webhook.
///
diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
index 4e353c39b..1baa84c50 100644
--- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
@@ -568,7 +568,7 @@ namespace Discord.Rest
#endregion
#region Webhooks
- public static async Task CreateWebhookAsync(ITextChannel channel, BaseDiscordClient client, string name, Stream avatar, RequestOptions options)
+ public static async Task CreateWebhookAsync(IIntegrationChannel channel, BaseDiscordClient client, string name, Stream avatar, RequestOptions options)
{
var args = new CreateWebhookParams { Name = name };
if (avatar != null)
@@ -577,14 +577,14 @@ namespace Discord.Rest
var model = await client.ApiClient.CreateWebhookAsync(channel.Id, args, options).ConfigureAwait(false);
return RestWebhook.Create(client, channel, model);
}
- public static async Task GetWebhookAsync(ITextChannel channel, BaseDiscordClient client, ulong id, RequestOptions options)
+ public static async Task GetWebhookAsync(IIntegrationChannel channel, BaseDiscordClient client, ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false);
if (model == null)
return null;
return RestWebhook.Create(client, channel, model);
}
- public static async Task> GetWebhooksAsync(ITextChannel channel, BaseDiscordClient client, RequestOptions options)
+ public static async Task> GetWebhooksAsync(IIntegrationChannel channel, BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetChannelWebhooksAsync(channel.Id, options).ConfigureAwait(false);
return models.Select(x => RestWebhook.Create(client, channel, x))
diff --git a/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs
index 3f17642f8..4068eea23 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestForumChannel.cs
@@ -154,6 +154,18 @@ namespace Discord.Rest
public Task> GetPublicArchivedThreadsAsync(int? limit = null, DateTimeOffset? before = null, RequestOptions options = null)
=> ThreadHelper.GetPublicArchivedThreadsAsync(this, Discord, limit, before, options);
+ ///
+ public Task CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
+ => ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
+
+ ///
+ public Task GetWebhookAsync(ulong id, RequestOptions options = null)
+ => ChannelHelper.GetWebhookAsync(this, Discord, id, options);
+
+ ///
+ public Task> GetWebhooksAsync(RequestOptions options = null)
+ => ChannelHelper.GetWebhooksAsync(this, Discord, options);
+
#region IForumChannel
async Task> IForumChannel.GetActiveThreadsAsync(RequestOptions options)
=> await GetActiveThreadsAsync(options).ConfigureAwait(false);
@@ -203,5 +215,20 @@ namespace Discord.Rest
public Task SyncPermissionsAsync(RequestOptions options = null)
=> ChannelHelper.SyncPermissionsAsync(this, Discord, options);
#endregion
+
+ #region IIntegrationChannel
+
+ ///
+ async Task IIntegrationChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
+ => await CreateWebhookAsync(name, avatar, options).ConfigureAwait(false);
+ ///
+ async Task IIntegrationChannel.GetWebhookAsync(ulong id, RequestOptions options)
+ => await GetWebhookAsync(id, options).ConfigureAwait(false);
+ ///
+ async Task> IIntegrationChannel.GetWebhooksAsync(RequestOptions options)
+ => await GetWebhooksAsync(options).ConfigureAwait(false);
+
+ #endregion
+
}
}
diff --git a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
index b48b1d0d8..25c92df33 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
@@ -307,17 +307,22 @@ namespace Discord.Rest
private string DebuggerDisplay => $"{Name} ({Id}, Text)";
#endregion
- #region ITextChannel
+ #region IIntegrationChannel
+
///
- async Task ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
+ async Task IIntegrationChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
=> await CreateWebhookAsync(name, avatar, options).ConfigureAwait(false);
///
- async Task ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
+ async Task IIntegrationChannel.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options).ConfigureAwait(false);
///
- async Task> ITextChannel.GetWebhooksAsync(RequestOptions options)
+ async Task> IIntegrationChannel.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options).ConfigureAwait(false);
+ #endregion
+
+ #region ITextChannel
+
async Task ITextChannel.CreateThreadAsync(string name, ThreadType type, ThreadArchiveDuration autoArchiveDuration, IMessage message, bool? invitable, int? slowmode, RequestOptions options)
=> await CreateThreadAsync(name, type, autoArchiveDuration, message, invitable, slowmode, options);
diff --git a/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs b/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
index f40b786cd..0528d6b1c 100644
--- a/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
+++ b/src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
@@ -10,7 +10,7 @@ namespace Discord.Rest
{
#region RestWebhook
internal IGuild Guild { get; private set; }
- internal ITextChannel Channel { get; private set; }
+ internal IIntegrationChannel Channel { get; private set; }
///
public string Token { get; }
@@ -38,7 +38,7 @@ namespace Discord.Rest
Token = token;
ChannelId = channelId;
}
- internal RestWebhook(BaseDiscordClient discord, ITextChannel channel, ulong id, string token, ulong channelId)
+ internal RestWebhook(BaseDiscordClient discord, IIntegrationChannel channel, ulong id, string token, ulong channelId)
: this(discord, channel.Guild, id, token, channelId)
{
Channel = channel;
@@ -50,7 +50,7 @@ namespace Discord.Rest
entity.Update(model);
return entity;
}
- internal static RestWebhook Create(BaseDiscordClient discord, ITextChannel channel, Model model)
+ internal static RestWebhook Create(BaseDiscordClient discord, IIntegrationChannel channel, Model model)
{
var entity = new RestWebhook(discord, channel, model.Id, model.Token, model.ChannelId);
entity.Update(model);
@@ -103,7 +103,7 @@ namespace Discord.Rest
IGuild IWebhook.Guild
=> Guild ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
///
- ITextChannel IWebhook.Channel
+ IIntegrationChannel IWebhook.Channel
=> Channel ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
///
Task IWebhook.ModifyAsync(Action func, RequestOptions options)
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs
index 5e67da68a..ddfe76346 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketForumChannel.cs
@@ -156,6 +156,35 @@ namespace Discord.WebSocket
public Task> GetPublicArchivedThreadsAsync(int? limit = null, DateTimeOffset? before = null, RequestOptions options = null)
=> ThreadHelper.GetPublicArchivedThreadsAsync(this, Discord, limit, before, options);
+ #region Webhooks
+
+ ///
+ public Task CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
+ => ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
+
+ ///
+ public Task GetWebhookAsync(ulong id, RequestOptions options = null)
+ => ChannelHelper.GetWebhookAsync(this, Discord, id, options);
+
+ ///
+ public Task> GetWebhooksAsync(RequestOptions options = null)
+ => ChannelHelper.GetWebhooksAsync(this, Discord, options);
+ #endregion
+
+ #region IIntegrationChannel
+
+ ///
+ async Task IIntegrationChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
+ => await CreateWebhookAsync(name, avatar, options).ConfigureAwait(false);
+ ///
+ async Task IIntegrationChannel.GetWebhookAsync(ulong id, RequestOptions options)
+ => await GetWebhookAsync(id, options).ConfigureAwait(false);
+ ///
+ async Task> IIntegrationChannel.GetWebhooksAsync(RequestOptions options)
+ => await GetWebhooksAsync(options).ConfigureAwait(false);
+ ///
+ #endregion
+
#region IForumChannel
async Task> IForumChannel.GetActiveThreadsAsync(RequestOptions options)
=> await GetActiveThreadsAsync(options).ConfigureAwait(false);
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
index 880eee87c..8a1b61928 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
@@ -365,17 +365,21 @@ namespace Discord.WebSocket
internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;
#endregion
- #region ITextChannel
+ #region IIntegrationChannel
+
///
- async Task ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
+ async Task IIntegrationChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
=> await CreateWebhookAsync(name, avatar, options).ConfigureAwait(false);
///
- async Task ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
+ async Task IIntegrationChannel.GetWebhookAsync(ulong id, RequestOptions options)
=> await GetWebhookAsync(id, options).ConfigureAwait(false);
///
- async Task> ITextChannel.GetWebhooksAsync(RequestOptions options)
+ async Task> IIntegrationChannel.GetWebhooksAsync(RequestOptions options)
=> await GetWebhooksAsync(options).ConfigureAwait(false);
///
+ #endregion
+
+ #region ITextChannel
async Task ITextChannel.CreateThreadAsync(string name, ThreadType type, ThreadArchiveDuration autoArchiveDuration, IMessage message, bool? invitable, int? slowmode, RequestOptions options)
=> await CreateThreadAsync(name, type, autoArchiveDuration, message, invitable, slowmode, options);
///
diff --git a/src/Discord.Net.Webhook/Entities/Webhooks/RestInternalWebhook.cs b/src/Discord.Net.Webhook/Entities/Webhooks/RestInternalWebhook.cs
index 2a5c4786e..1082c431d 100644
--- a/src/Discord.Net.Webhook/Entities/Webhooks/RestInternalWebhook.cs
+++ b/src/Discord.Net.Webhook/Entities/Webhooks/RestInternalWebhook.cs
@@ -65,7 +65,7 @@ namespace Discord.Webhook
private string DebuggerDisplay => $"Webhook: {Name} ({Id})";
IUser IWebhook.Creator => null;
- ITextChannel IWebhook.Channel => null;
+ IIntegrationChannel IWebhook.Channel => null;
IGuild IWebhook.Guild => null;
}
}