81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
using System.Threading.Tasks;
|
|
using DSharpPlus;
|
|
using DSharpPlus.Entities;
|
|
|
|
namespace T3k3rg0.Helpers
|
|
{
|
|
public static class IdExtractor
|
|
{
|
|
public static ulong? ExtractChannel(string channelInput)
|
|
{
|
|
if (channelInput.StartsWith("https://discord.com/channels/"))
|
|
{
|
|
var parts = channelInput.Split('/');
|
|
if (parts.Length >= 6 && ulong.TryParse(parts[5], out ulong parsedChannelLinkId))
|
|
return parsedChannelLinkId;
|
|
}
|
|
|
|
else if (channelInput.StartsWith("<#") && channelInput.EndsWith(">"))
|
|
{
|
|
var trimmed = channelInput.Trim('<', '#', '>');
|
|
if (ulong.TryParse(trimmed, out ulong parsedChannelMentionId))
|
|
return parsedChannelMentionId;
|
|
}
|
|
else if (ulong.TryParse(channelInput, out ulong parsedRawCannelId))
|
|
{
|
|
return parsedRawCannelId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static async Task<DiscordChannel?> ExtractChannelId(string channelInput, DiscordClient client)
|
|
{
|
|
var channelId = ExtractChannel(channelInput);
|
|
if (channelId is null)
|
|
return null;
|
|
|
|
try
|
|
{
|
|
var channel = await client.GetChannelAsync(channelId.Value);
|
|
return channel;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
public static ulong? ExtractMessage(string messageInput)
|
|
{
|
|
|
|
if (messageInput.StartsWith("https://discord.com/channels/"))
|
|
{
|
|
var parts = messageInput.Split('/');
|
|
if (parts.Length >= 7 && ulong.TryParse(parts[6], out ulong parsedMessageLinkId))
|
|
return parsedMessageLinkId;
|
|
}
|
|
|
|
else if (ulong.TryParse(messageInput, out ulong parsedRawMessageId))
|
|
{
|
|
return parsedRawMessageId;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static async Task<DiscordMessage?> ExtractMessageId(string messageInput, DiscordChannel channel)
|
|
{
|
|
var messageId = ExtractMessage(messageInput);
|
|
if (messageId is null)
|
|
return null;
|
|
try
|
|
{
|
|
var message = await channel.GetMessageAsync(messageId.Value);
|
|
return message;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|