2020-12-22 12:15:26 +00:00
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
using Myriad.Rest.Types;
|
|
|
|
using Myriad.Rest.Types.Requests;
|
|
|
|
using Myriad.Types;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace Myriad.Rest;
|
|
|
|
|
|
|
|
public class DiscordApiClient
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public const string UserAgent = "DiscordBot (https://github.com/xSke/PluralKit/tree/main/Myriad/, v1)";
|
2022-02-26 21:28:20 +00:00
|
|
|
private const string DefaultApiBaseUrl = "https://discord.com/api/v10";
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly BaseRestClient _client;
|
|
|
|
|
|
|
|
public EventHandler<(string, int, long)> OnResponseEvent;
|
|
|
|
|
|
|
|
public DiscordApiClient(string token, ILogger logger, string? baseUrl = null)
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_client = new BaseRestClient(UserAgent, token, logger, baseUrl ?? DefaultApiBaseUrl);
|
|
|
|
_client.OnResponseEvent += (_, ev) => OnResponseEvent?.Invoke(null, ev);
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<GatewayInfo> GetGateway() =>
|
|
|
|
_client.Get<GatewayInfo>("/gateway", ("GetGateway", default))!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<GatewayInfo.Bot> GetGatewayBot() =>
|
|
|
|
_client.Get<GatewayInfo.Bot>("/gateway/bot", ("GetGatewayBot", default))!;
|
2021-11-02 09:34:17 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Channel?> GetChannel(ulong channelId) =>
|
|
|
|
_client.Get<Channel>($"/channels/{channelId}", ("GetChannel", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Message?> GetMessage(ulong channelId, ulong messageId) =>
|
|
|
|
_client.Get<Message>($"/channels/{channelId}/messages/{messageId}", ("GetMessage", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Guild?> GetGuild(ulong id) =>
|
|
|
|
_client.Get<Guild>($"/guilds/{id}", ("GetGuild", id));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Channel[]> GetGuildChannels(ulong id) =>
|
|
|
|
_client.Get<Channel[]>($"/guilds/{id}/channels", ("GetGuildChannels", id))!;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<User?> GetUser(ulong id) =>
|
|
|
|
_client.Get<User>($"/users/{id}", ("GetUser", default));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<GuildMember?> GetGuildMember(ulong guildId, ulong userId) =>
|
|
|
|
_client.Get<GuildMember>($"/guilds/{guildId}/members/{userId}",
|
|
|
|
("GetGuildMember", guildId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Message> CreateMessage(ulong channelId, MessageRequest request, MultipartFile[]? files = null) =>
|
|
|
|
_client.PostMultipart<Message>($"/channels/{channelId}/messages", ("CreateMessage", channelId), request,
|
|
|
|
files)!;
|
2020-12-23 01:19:02 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Message> EditMessage(ulong channelId, ulong messageId, MessageEditRequest request) =>
|
|
|
|
_client.Patch<Message>($"/channels/{channelId}/messages/{messageId}", ("EditMessage", channelId), request)!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteMessage(ulong channelId, ulong messageId) =>
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}", ("DeleteMessage", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteMessage(Message message) =>
|
|
|
|
_client.Delete($"/channels/{message.ChannelId}/messages/{message.Id}",
|
|
|
|
("DeleteMessage", message.ChannelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task CreateReaction(ulong channelId, ulong messageId, Emoji emoji) =>
|
|
|
|
_client.Put<object>($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}/@me",
|
|
|
|
("CreateReaction", channelId), null);
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteOwnReaction(ulong channelId, ulong messageId, Emoji emoji) =>
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}/@me",
|
|
|
|
("DeleteOwnReaction", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteUserReaction(ulong channelId, ulong messageId, Emoji emoji, ulong userId) =>
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}/{userId}",
|
|
|
|
("DeleteUserReaction", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteAllReactions(ulong channelId, ulong messageId) =>
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions",
|
|
|
|
("DeleteAllReactions", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteAllReactionsForEmoji(ulong channelId, ulong messageId, Emoji emoji) =>
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}",
|
|
|
|
("DeleteAllReactionsForEmoji", channelId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<ApplicationCommand> CreateGlobalApplicationCommand(ulong applicationId,
|
|
|
|
ApplicationCommandRequest request) =>
|
|
|
|
_client.Post<ApplicationCommand>($"/applications/{applicationId}/commands",
|
|
|
|
("CreateGlobalApplicationCommand", applicationId), request)!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<ApplicationCommand[]> GetGuildApplicationCommands(ulong applicationId, ulong guildId) =>
|
|
|
|
_client.Get<ApplicationCommand[]>($"/applications/{applicationId}/guilds/{guildId}/commands",
|
|
|
|
("GetGuildApplicationCommands", applicationId))!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<ApplicationCommand> CreateGuildApplicationCommand(ulong applicationId, ulong guildId,
|
|
|
|
ApplicationCommandRequest request) =>
|
|
|
|
_client.Post<ApplicationCommand>($"/applications/{applicationId}/guilds/{guildId}/commands",
|
|
|
|
("CreateGuildApplicationCommand", applicationId), request)!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<ApplicationCommand> EditGuildApplicationCommand(ulong applicationId, ulong guildId,
|
|
|
|
ApplicationCommandRequest request) =>
|
|
|
|
_client.Patch<ApplicationCommand>($"/applications/{applicationId}/guilds/{guildId}/commands",
|
|
|
|
("EditGuildApplicationCommand", applicationId), request)!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task DeleteGuildApplicationCommand(ulong applicationId, ulong commandId) =>
|
|
|
|
_client.Delete($"/applications/{applicationId}/commands/{commandId}",
|
|
|
|
("DeleteGuildApplicationCommand", applicationId));
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task CreateInteractionResponse(ulong interactionId, string token, InteractionResponse response) =>
|
|
|
|
_client.Post<object>($"/interactions/{interactionId}/{token}/callback",
|
|
|
|
("CreateInteractionResponse", interactionId), response);
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task ModifyGuildMember(ulong guildId, ulong userId, ModifyGuildMemberRequest request) =>
|
|
|
|
_client.Patch<object>($"/guilds/{guildId}/members/{userId}",
|
|
|
|
("ModifyGuildMember", guildId), request);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Webhook> CreateWebhook(ulong channelId, CreateWebhookRequest request) =>
|
|
|
|
_client.Post<Webhook>($"/channels/{channelId}/webhooks", ("CreateWebhook", channelId), request)!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Webhook> GetWebhook(ulong webhookId) =>
|
|
|
|
_client.Get<Webhook>($"/webhooks/{webhookId}/webhooks", ("GetWebhook", webhookId))!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Webhook[]> GetChannelWebhooks(ulong channelId) =>
|
|
|
|
_client.Get<Webhook[]>($"/channels/{channelId}/webhooks", ("GetChannelWebhooks", channelId))!;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<Message> ExecuteWebhook(ulong webhookId, string webhookToken, ExecuteWebhookRequest request,
|
|
|
|
MultipartFile[]? files = null, ulong? threadId = null)
|
|
|
|
{
|
|
|
|
var url = $"/webhooks/{webhookId}/{webhookToken}?wait=true";
|
|
|
|
if (threadId != null)
|
|
|
|
url += $"&thread_id={threadId}";
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return _client.PostMultipart<Message>(url,
|
|
|
|
("ExecuteWebhook", webhookId), request, files)!;
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
|
|
|
|
public Task<Message> EditWebhookMessage(ulong webhookId, string webhookToken, ulong messageId,
|
|
|
|
WebhookMessageEditRequest request, ulong? threadId = null)
|
|
|
|
{
|
|
|
|
var url = $"/webhooks/{webhookId}/{webhookToken}/messages/{messageId}";
|
|
|
|
if (threadId != null)
|
|
|
|
url += $"?thread_id={threadId}";
|
|
|
|
|
|
|
|
return _client.Patch<Message>(url, ("EditWebhookMessage", webhookId), request)!;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<Channel> CreateDm(ulong recipientId) =>
|
|
|
|
_client.Post<Channel>("/users/@me/channels", ("CreateDM", default), new CreateDmRequest(recipientId))!;
|
|
|
|
|
|
|
|
private static string EncodeEmoji(Emoji emoji) =>
|
|
|
|
WebUtility.UrlEncode(emoji.Id != null ? $"{emoji.Name}:{emoji.Id}" : emoji.Name) ??
|
|
|
|
throw new ArgumentException("Could not encode emoji");
|
2020-12-22 12:15:26 +00:00
|
|
|
}
|