mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user