From 64423a06535d8ca67f4c010dc8ccd79abb4afc2d Mon Sep 17 00:00:00 2001
From: Quin Lynch <49576606+quinchs@users.noreply.github.com>
Date: Fri, 14 Jan 2022 07:56:42 -0400
Subject: [PATCH] Add GetChannelType extension method (#2035)
---
.../Extensions/ChannelExtensions.cs | 54 +++++++++++++++++++
1 file changed, 54 insertions(+)
create mode 100644 src/Discord.Net.Core/Extensions/ChannelExtensions.cs
diff --git a/src/Discord.Net.Core/Extensions/ChannelExtensions.cs b/src/Discord.Net.Core/Extensions/ChannelExtensions.cs
new file mode 100644
index 000000000..b5ddae1cf
--- /dev/null
+++ b/src/Discord.Net.Core/Extensions/ChannelExtensions.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Discord
+{
+ public static class ChannelExtensions
+ {
+ ///
+ /// Attempts to get the based off of the channel's interfaces.
+ ///
+ /// The channel to get the type of.
+ /// The of the channel if found, otherwise .
+ public static ChannelType? GetChannelType(this IChannel channel)
+ {
+ switch (channel)
+ {
+ case IStageChannel:
+ return ChannelType.Stage;
+
+ case IThreadChannel thread:
+ return thread.Type switch
+ {
+ ThreadType.NewsThread => ChannelType.NewsThread,
+ ThreadType.PrivateThread => ChannelType.PrivateThread,
+ ThreadType.PublicThread => ChannelType.PublicThread,
+ _ => null,
+ };
+
+ case ICategoryChannel:
+ return ChannelType.Category;
+
+ case IDMChannel:
+ return ChannelType.DM;
+
+ case IGroupChannel:
+ return ChannelType.Group;
+
+ case INewsChannel:
+ return ChannelType.News;
+
+ case IVoiceChannel:
+ return ChannelType.Voice;
+
+ case ITextChannel:
+ return ChannelType.Text;
+ }
+
+ return null;
+ }
+ }
+}