2020-12-22 12:15:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Myriad.Rest.Types;
|
|
|
|
|
using Myriad.Rest.Types.Requests;
|
|
|
|
|
using Myriad.Types;
|
|
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
|
|
namespace Myriad.Rest
|
|
|
|
|
{
|
|
|
|
|
public class DiscordApiClient
|
|
|
|
|
{
|
2021-08-23 20:55:21 +00:00
|
|
|
|
public const string UserAgent = "DiscordBot (https://github.com/xSke/PluralKit/tree/main/Myriad/, v1)";
|
2020-12-22 12:15:26 +00:00
|
|
|
|
private readonly BaseRestClient _client;
|
|
|
|
|
|
|
|
|
|
public DiscordApiClient(string token, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
_client = new BaseRestClient(UserAgent, token, logger);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<GatewayInfo> GetGateway() =>
|
|
|
|
|
_client.Get<GatewayInfo>("/gateway", ("GetGateway", default))!;
|
|
|
|
|
|
|
|
|
|
public Task<GatewayInfo.Bot> GetGatewayBot() =>
|
|
|
|
|
_client.Get<GatewayInfo.Bot>("/gateway/bot", ("GetGatewayBot", default))!;
|
|
|
|
|
|
|
|
|
|
public Task<Channel?> GetChannel(ulong channelId) =>
|
|
|
|
|
_client.Get<Channel>($"/channels/{channelId}", ("GetChannel", channelId));
|
|
|
|
|
|
|
|
|
|
public Task<Message?> GetMessage(ulong channelId, ulong messageId) =>
|
|
|
|
|
_client.Get<Message>($"/channels/{channelId}/messages/{messageId}", ("GetMessage", channelId));
|
|
|
|
|
|
2020-12-24 13:52:44 +00:00
|
|
|
|
public Task<Guild?> GetGuild(ulong id) =>
|
|
|
|
|
_client.Get<Guild>($"/guilds/{id}", ("GetGuild", id));
|
|
|
|
|
|
|
|
|
|
public Task<Channel[]> GetGuildChannels(ulong id) =>
|
|
|
|
|
_client.Get<Channel[]>($"/guilds/{id}/channels", ("GetGuildChannels", id))!;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
|
|
|
|
|
public Task<User?> GetUser(ulong id) =>
|
|
|
|
|
_client.Get<User>($"/users/{id}", ("GetUser", default));
|
|
|
|
|
|
2020-12-23 01:19:02 +00:00
|
|
|
|
public Task<GuildMember?> GetGuildMember(ulong guildId, ulong userId) =>
|
|
|
|
|
_client.Get<GuildMember>($"/guilds/{guildId}/members/{userId}",
|
|
|
|
|
("GetGuildMember", guildId));
|
|
|
|
|
|
2021-01-15 10:29:43 +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-22 12:15:26 +00:00
|
|
|
|
|
|
|
|
|
public Task<Message> EditMessage(ulong channelId, ulong messageId, MessageEditRequest request) =>
|
|
|
|
|
_client.Patch<Message>($"/channels/{channelId}/messages/{messageId}", ("EditMessage", channelId), request)!;
|
|
|
|
|
|
|
|
|
|
public Task DeleteMessage(ulong channelId, ulong messageId) =>
|
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}", ("DeleteMessage", channelId));
|
2021-03-21 09:45:26 +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
|
|
|
|
public Task CreateReaction(ulong channelId, ulong messageId, Emoji emoji) =>
|
|
|
|
|
_client.Put<object>($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}/@me",
|
|
|
|
|
("CreateReaction", channelId), null);
|
|
|
|
|
|
|
|
|
|
public Task DeleteOwnReaction(ulong channelId, ulong messageId, Emoji emoji) =>
|
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}/@me",
|
|
|
|
|
("DeleteOwnReaction", channelId));
|
|
|
|
|
|
|
|
|
|
public Task DeleteUserReaction(ulong channelId, ulong messageId, Emoji emoji, ulong userId) =>
|
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}/{userId}",
|
|
|
|
|
("DeleteUserReaction", channelId));
|
|
|
|
|
|
|
|
|
|
public Task DeleteAllReactions(ulong channelId, ulong messageId) =>
|
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions",
|
|
|
|
|
("DeleteAllReactions", channelId));
|
|
|
|
|
|
|
|
|
|
public Task DeleteAllReactionsForEmoji(ulong channelId, ulong messageId, Emoji emoji) =>
|
|
|
|
|
_client.Delete($"/channels/{channelId}/messages/{messageId}/reactions/{EncodeEmoji(emoji)}",
|
|
|
|
|
("DeleteAllReactionsForEmoji", channelId));
|
|
|
|
|
|
|
|
|
|
public Task<ApplicationCommand> CreateGlobalApplicationCommand(ulong applicationId,
|
|
|
|
|
ApplicationCommandRequest request) =>
|
|
|
|
|
_client.Post<ApplicationCommand>($"/applications/{applicationId}/commands",
|
|
|
|
|
("CreateGlobalApplicationCommand", applicationId), request)!;
|
|
|
|
|
|
|
|
|
|
public Task<ApplicationCommand[]> GetGuildApplicationCommands(ulong applicationId, ulong guildId) =>
|
|
|
|
|
_client.Get<ApplicationCommand[]>($"/applications/{applicationId}/guilds/{guildId}/commands",
|
|
|
|
|
("GetGuildApplicationCommands", applicationId))!;
|
|
|
|
|
|
|
|
|
|
public Task<ApplicationCommand> CreateGuildApplicationCommand(ulong applicationId, ulong guildId,
|
|
|
|
|
ApplicationCommandRequest request) =>
|
|
|
|
|
_client.Post<ApplicationCommand>($"/applications/{applicationId}/guilds/{guildId}/commands",
|
|
|
|
|
("CreateGuildApplicationCommand", applicationId), request)!;
|
|
|
|
|
|
|
|
|
|
public Task<ApplicationCommand> EditGuildApplicationCommand(ulong applicationId, ulong guildId,
|
|
|
|
|
ApplicationCommandRequest request) =>
|
|
|
|
|
_client.Patch<ApplicationCommand>($"/applications/{applicationId}/guilds/{guildId}/commands",
|
|
|
|
|
("EditGuildApplicationCommand", applicationId), request)!;
|
|
|
|
|
|
|
|
|
|
public Task DeleteGuildApplicationCommand(ulong applicationId, ulong commandId) =>
|
|
|
|
|
_client.Delete($"/applications/{applicationId}/commands/{commandId}",
|
|
|
|
|
("DeleteGuildApplicationCommand", applicationId));
|
|
|
|
|
|
|
|
|
|
public Task CreateInteractionResponse(ulong interactionId, string token, InteractionResponse response) =>
|
|
|
|
|
_client.Post<object>($"/interactions/{interactionId}/{token}/callback",
|
|
|
|
|
("CreateInteractionResponse", interactionId), response);
|
|
|
|
|
|
|
|
|
|
public Task ModifyGuildMember(ulong guildId, ulong userId, ModifyGuildMemberRequest request) =>
|
|
|
|
|
_client.Patch<object>($"/guilds/{guildId}/members/{userId}",
|
|
|
|
|
("ModifyGuildMember", guildId), request);
|
|
|
|
|
|
|
|
|
|
public Task<Webhook> CreateWebhook(ulong channelId, CreateWebhookRequest request) =>
|
|
|
|
|
_client.Post<Webhook>($"/channels/{channelId}/webhooks", ("CreateWebhook", channelId), request)!;
|
|
|
|
|
|
|
|
|
|
public Task<Webhook> GetWebhook(ulong webhookId) =>
|
|
|
|
|
_client.Get<Webhook>($"/webhooks/{webhookId}/webhooks", ("GetWebhook", webhookId))!;
|
|
|
|
|
|
|
|
|
|
public Task<Webhook[]> GetChannelWebhooks(ulong channelId) =>
|
|
|
|
|
_client.Get<Webhook[]>($"/channels/{channelId}/webhooks", ("GetChannelWebhooks", channelId))!;
|
|
|
|
|
|
|
|
|
|
public Task<Message> ExecuteWebhook(ulong webhookId, string webhookToken, ExecuteWebhookRequest request,
|
2021-07-15 10:41:19 +00:00
|
|
|
|
MultipartFile[]? files = null, ulong? threadId = null)
|
|
|
|
|
{
|
|
|
|
|
var url = $"/webhooks/{webhookId}/{webhookToken}?wait=true";
|
|
|
|
|
if (threadId != null)
|
|
|
|
|
url += $"&thread_id={threadId}";
|
|
|
|
|
|
|
|
|
|
return _client.PostMultipart<Message>(url,
|
2020-12-22 12:15:26 +00:00
|
|
|
|
("ExecuteWebhook", webhookId), request, files)!;
|
2021-07-15 10:41:19 +00:00
|
|
|
|
}
|
2020-12-22 12:15:26 +00:00
|
|
|
|
|
2021-05-03 10:33:30 +00:00
|
|
|
|
public Task<Message> EditWebhookMessage(ulong webhookId, string webhookToken, ulong messageId,
|
|
|
|
|
WebhookMessageEditRequest request) =>
|
|
|
|
|
_client.Patch<Message>($"/webhooks/{webhookId}/{webhookToken}/messages/{messageId}",
|
|
|
|
|
("EditWebhookMessage", webhookId), request)!;
|
|
|
|
|
|
2020-12-25 12:19:35 +00:00
|
|
|
|
public Task<Channel> CreateDm(ulong recipientId) =>
|
|
|
|
|
_client.Post<Channel>($"/users/@me/channels", ("CreateDM", default), new CreateDmRequest(recipientId))!;
|
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
|
private static string EncodeEmoji(Emoji emoji) =>
|
2021-07-01 05:59:04 +00:00
|
|
|
|
WebUtility.UrlEncode(emoji.Id != null ? $"{emoji.Name}:{emoji.Id}" : emoji.Name) ??
|
2020-12-22 12:15:26 +00:00
|
|
|
|
throw new ArgumentException("Could not encode emoji");
|
|
|
|
|
}
|
|
|
|
|
}
|