mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
84 lines
1.8 KiB
C#
84 lines
1.8 KiB
C#
using CSharpDiscordWebhook;
|
|
using CSharpDiscordWebhook.Objects;
|
|
using System.Runtime.InteropServices;
|
|
using RadicadeServer.RecNet.Responses;
|
|
using RadicadeServer.Discord.Webhooks;
|
|
|
|
namespace RadicadeServer.Radicade.Discord;
|
|
public static class RadicadeWebhook
|
|
{
|
|
private static DiscordWebhook? Webhook;
|
|
|
|
private static string WebhookUrl => WebhookConfig.GeneralActivityUrl;
|
|
|
|
private static readonly Success WebhookInvalid = Success.Error("Webhook is disabled/invalid.");
|
|
private static readonly Success CouldntSendMessage = Success.Error("Couldn't send message.");
|
|
|
|
public static bool IsWebhookValid()
|
|
{
|
|
return Uri.TryCreate(WebhookUrl, UriKind.Absolute, out var uri)
|
|
&& (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps);
|
|
}
|
|
|
|
private static bool TryInit()
|
|
{
|
|
if (!IsWebhookValid()) return false;
|
|
if (Webhook != null) return true;
|
|
|
|
try
|
|
{
|
|
Webhook = new DiscordWebhook(new Uri(WebhookUrl));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
Webhook = null;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static async Task<Success> SendMessage(
|
|
[Optional] string content,
|
|
[Optional] List<EmbedBuilder> embed,
|
|
[Optional] PollBuilder poll,
|
|
[Optional] List<AttachmentBuilder> attachments
|
|
)
|
|
{
|
|
if (!TryInit()) return WebhookInvalid;
|
|
|
|
try
|
|
{
|
|
var result = await Webhook!.SendMessageAsync(new MessageBuilder
|
|
{
|
|
Content = content,
|
|
Embeds = embed,
|
|
Poll = poll,
|
|
Attachments = attachments
|
|
});
|
|
|
|
return (result.Success) ? Success.Ok : CouldntSendMessage;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return CouldntSendMessage;
|
|
}
|
|
}
|
|
|
|
public static async Task<Success> SendMessage(MessageBuilder message)
|
|
{
|
|
if (!TryInit()) return WebhookInvalid;
|
|
|
|
try
|
|
{
|
|
var result = await Webhook!.SendMessageAsync(message);
|
|
|
|
return (result.Success) ? Success.Ok : CouldntSendMessage;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return CouldntSendMessage;
|
|
}
|
|
}
|
|
}
|