lilybird
Version:
A bun-first discord api wrapper written in TS
766 lines (765 loc) • 35.8 kB
JavaScript
import packageJson from "../../package.json" with { type: "json" };
export class RestError extends Error {
code;
errors;
constructor(error) {
super(error.message);
this.code = error.code;
this.errors = error.errors;
}
}
export class REST {
static BaseURL = "https://discord.com/api/v10/";
#token;
#tokenType;
constructor(token, tokenType = "Bot") {
this.#token = token;
this.#tokenType = tokenType;
}
async makeAPIRequest(method, path, data, filesOrReason) {
const opts = {
method,
headers: {
Authorization: `${this.#tokenType} ${this.#token}`,
"User-Agent": `DiscordBot/LilyBird/${packageJson.version}`
}
};
if (data instanceof FormData) {
opts.body = data;
if (typeof filesOrReason !== "undefined") {
opts.headers["X-Audit-Log-Reason"] = filesOrReason;
}
}
else if (typeof data !== "undefined") {
let reason;
let obj;
if ("reason" in data)
({ reason, ...obj } = data);
else
obj = data;
if (typeof filesOrReason !== "undefined" && typeof filesOrReason !== "string" && filesOrReason.length > 0) {
const temp = [];
const form = new FormData();
for (let i = 0, { length } = filesOrReason; i < length; i++) {
form.append(`files[${i}]`, filesOrReason[i].file, filesOrReason[i].name);
temp.push({
id: i,
filename: filesOrReason[i].name
});
}
if ("data" in obj)
obj.data.attachments = [...temp, ...obj.data.attachments ?? []];
else
obj.attachments = [...temp, ...obj.attachments ?? []];
form.append("payload_json", JSON.stringify(obj));
opts.body = form;
}
else {
opts.headers["Content-Type"] = "application/json";
opts.body = JSON.stringify(data);
}
if (typeof reason !== "undefined") {
opts.headers["X-Audit-Log-Reason"] = reason;
}
}
const response = await fetch(`${REST.BaseURL}${path}`, opts);
if (!response.ok) {
const errorMessage = await response.json();
throw new RestError(errorMessage);
}
if (response.status === 204)
return null;
return await response.json();
}
setToken(token, tokenType = "Bot") {
this.#token = token;
this.#tokenType = tokenType;
}
async getGateway() {
return this.makeAPIRequest("GET", "gateway");
}
async getGatewayBot() {
return this.makeAPIRequest("GET", "gateway/bot");
}
async getGlobalApplicationCommands(clientId, withLocalizations = false) {
return this.makeAPIRequest("GET", `applications/${clientId}/commands?with_localizations=${withLocalizations}`);
}
async createGlobalApplicationCommand(clientId, body) {
return this.makeAPIRequest("POST", `applications/${clientId}/commands`, body);
}
async getGlobalApplicationCommand(clientId, commandId) {
return this.makeAPIRequest("GET", `applications/${clientId}/commands/${commandId}`);
}
async editGlobalApplicationCommand(clientId, commandId, body) {
return this.makeAPIRequest("PATCH", `applications/${clientId}/commands/${commandId}`, body);
}
async deleteGlobalApplicationCommand(clientId, commandId) {
return this.makeAPIRequest("DELETE", `applications/${clientId}/commands/${commandId}`);
}
async bulkOverwriteGlobalApplicationCommand(clientId, body) {
return this.makeAPIRequest("PUT", `applications/${clientId}/commands`, body);
}
async getGuildApplicationCommands(clientId, withLocalizations = false) {
return this.makeAPIRequest("GET", `applications/${clientId}/commands?with_localizations=${withLocalizations}`);
}
async createGuildApplicationCommand(clientId, guildId, body) {
return this.makeAPIRequest("POST", `applications/${clientId}/guilds/${guildId}/commands`, body);
}
async getGuildApplicationCommand(clientId, guildId, commandId) {
return this.makeAPIRequest("POST", `applications/${clientId}/guilds/${guildId}/commands/${commandId}`);
}
async editGuildApplicationCommand(clientId, guildId, commandId, body) {
return this.makeAPIRequest("PATCH", `applications/${clientId}/guilds/${guildId}/commands/${commandId}`, body);
}
async deleteGuildApplicationCommand(clientId, guildId, commandId) {
return this.makeAPIRequest("DELETE", `applications/${clientId}/guilds/${guildId}/commands/${commandId}`);
}
async bulkOverwriteGuildApplicationCommand(clientId, guildId, body) {
return this.makeAPIRequest("PATCH", `applications/${clientId}/guilds/${guildId}/commands`, body);
}
async getGuildApplicationCommandPermissions(clientId, guildId) {
return this.makeAPIRequest("GET", `applications/${clientId}/guilds/${guildId}/commands/permissions`);
}
async getApplicationCommandPermissions(clientId, guildId, commandId) {
return this.makeAPIRequest("GET", `applications/${clientId}/guilds/${guildId}/commands/${commandId}/permissions`);
}
async editApplicationCommandPermissions(clientId, guildId, commandId, body) {
return this.makeAPIRequest("PATCH", `applications/${clientId}/guilds/${guildId}/commands/${commandId}/permissions`, body);
}
async createInteractionResponse(interactionId, interactionToken, body, files) {
return this.makeAPIRequest("POST", `interactions/${interactionId}/${interactionToken}/callback`, body, files);
}
async getOriginalInteractionResponse(clientId, interactionToken) {
return this.makeAPIRequest("GET", `webhooks/${clientId}/${interactionToken}/messages/@original`);
}
async editOriginalInteractionResponse(clientId, interactionToken, body, files) {
return this.makeAPIRequest("PATCH", `webhooks/${clientId}/${interactionToken}/messages/@original`, body, files);
}
async deleteOriginalInteractionResponse(clientId, interactionToken) {
return this.makeAPIRequest("DELETE", `webhooks/${clientId}/${interactionToken}/messages/@original`);
}
async createFollowupMessage(clientId, interactionToken, body, files) {
return this.makeAPIRequest("POST", `webhooks/${clientId}/${interactionToken}`, body, files);
}
async getFollowupMessage(clientId, interactionToken, messageId) {
return this.makeAPIRequest("GET", `webhooks/${clientId}/${interactionToken}/messages/${messageId}`);
}
async editFollowupMessage(clientId, interactionToken, messageId, body, files) {
return this.makeAPIRequest("PATCH", `webhooks/${clientId}/${interactionToken}/messages/${messageId}`, body, files);
}
async deleteFollowupMessage(clientId, interactionToken, messageId) {
return this.makeAPIRequest("DELETE", `webhooks/${clientId}/${interactionToken}/messages/${messageId}`);
}
async getCurrentApplication() {
return this.makeAPIRequest("GET", "applications/@me");
}
async editCurrentApplication(app) {
return this.makeAPIRequest("PATCH", "applications/@me", app);
}
async getApplicationRoleConnectionMetadataRecords(applicationId) {
return this.makeAPIRequest("GET", `applications/${applicationId}/role-connections/metadata`);
}
async updateApplicationRoleConnectionMetadataRecords(applicationId) {
return this.makeAPIRequest("PUT", `applications/${applicationId}/role-connections/metadata`);
}
async getGuildAuditLog(guildId, params) {
let url = `guilds/${guildId}/audit-logs?`;
if (typeof params.user_id !== "undefined")
url += `user_id=${params.user_id}&`;
if (typeof params.action_type !== "undefined")
url += `action_type=${params.action_type}&`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async listAutoModerationRulesForGuild(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/auto-moderation/rules`);
}
async getAutoModerationRule(guildId, ruleId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/auto-moderation/rules/${ruleId}`);
}
async createAutoModerationRule(guildId, rule) {
return this.makeAPIRequest("POST", `guilds/${guildId}/auto-moderation/rules`, rule);
}
async modifyAutoModerationRule(guildId, ruleId, rule) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/auto-moderation/rules/${ruleId}`, rule);
}
async deleteAutoModerationRule(guildId, ruleId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/auto-moderation/rules/${ruleId}`, { reason });
}
async getChannel(channelId) {
return this.makeAPIRequest("GET", `channels/${channelId}`);
}
async modifyChannel(channelId, body) {
return this.makeAPIRequest("PATCH", `channels/${channelId}`, body);
}
async deleteChannel(channelId, reason) {
return this.makeAPIRequest("DELETE", `channels/${channelId}`, { reason });
}
async editChannelPermissions(channelId, overwriteId, params) {
return this.makeAPIRequest("PUT", `channels/${channelId}/permissions/${overwriteId}`, params);
}
async getChannelInvites(channelId) {
return this.makeAPIRequest("GET", `channels/${channelId}/invites`);
}
async createChannelInvite(channelId, body) {
return this.makeAPIRequest("POST", `channels/${channelId}/invites`, body);
}
async deleteChannelPermission(channelId, overwriteId, reason) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/permissions/${overwriteId}`, { reason });
}
async followAnnouncementChannel(channelId, body) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/followers`, body);
}
async triggerTypingIndicator(channelId) {
return this.makeAPIRequest("POST", `channels/${channelId}/typing`);
}
async getPinnedMessages(channelId) {
return this.makeAPIRequest("GET", `channels/${channelId}/pins`);
}
async pinMessage(channelId, messageId, reason) {
return this.makeAPIRequest("PUT", `channels/${channelId}/pins/${messageId}`, { reason });
}
async unpinMessage(channelId, messageId, reason) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/pins/${messageId}`, { reason });
}
async groupDMAddRecipient(channelId, userId, body) {
return this.makeAPIRequest("PUT", `channels/${channelId}/recipients/${userId}`, body);
}
async groupDMRemoveRecipient(channelId, userId) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/recipients/${userId}`);
}
async startThreadFromMessage(channelId, messageId, body) {
return this.makeAPIRequest("POST", `channels/${channelId}/messages/${messageId}/threads`, body);
}
async startThreadWithoutMessage(channelId, body) {
return this.makeAPIRequest("POST", `channels/${channelId}/threads`, body);
}
async startThreadInForumOrMediaChannel(channelId, body, files) {
return this.makeAPIRequest("POST", `channels/${channelId}/threads`, body, files);
}
async joinThread(channelId) {
return this.makeAPIRequest("PUT", `channels/${channelId}/thread-members/@me`);
}
async addThreadMember(channelId, userId) {
return this.makeAPIRequest("PUT", `channels/${channelId}/thread-members/${userId}`);
}
async leaveThread(channelId) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/thread-members/@me`);
}
async removeThreadMember(channelId, userId) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/thread-members/${userId}`);
}
async getThreadMember(channelId, userId, withMember = false) {
return this.makeAPIRequest("GET", `channels/${channelId}/thread-members/${userId}?with_member=${withMember}`);
}
async listThreadMembers(channelId, params = {}) {
let url = `channels/${channelId}/thread-members`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async listPublicArchivedThreads(channelId, params = {}) {
let url = `channels/${channelId}/threads/archived/public`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async listPrivateArchivedThreads(channelId, params = {}) {
let url = `channels/${channelId}/threads/archived/private`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async listJoinedPrivateArchivedThreads(channelId, params = {}) {
let url = `channels/${channelId}/users/@me/threads/archived/private`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async listGuildEmojis(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/emojis`);
}
async getGuildEmoji(guildId, emojiId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/emojis/${emojiId}`);
}
async createGuildEmoji(guildId, params) {
return this.makeAPIRequest("POST", `guilds/${guildId}/emojis`, params);
}
async modifyGuildEmoji(guildId, emojiId, params) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/emojis/${emojiId}`, params);
}
async deleteGuildEmoji(guildId, emojiId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/emojis/${emojiId}`, { reason });
}
async listApplicationEmojis(applicationId) {
return this.makeAPIRequest("GET", `applications/${applicationId}/emojis`);
}
async getApplicationEmoji(applicationId, emojiId) {
return this.makeAPIRequest("GET", `applications/${applicationId}/emojis/${emojiId}`);
}
async createApplicationEmoji(applicationId, params) {
return this.makeAPIRequest("POST", `applications/${applicationId}/emojis`, params);
}
async modifyApplicationEmoji(applicationId, emojiId, params) {
return this.makeAPIRequest("PATCH", `applications/${applicationId}/emojis/${emojiId}`, params);
}
async deleteApplicationEmoji(applicationId, emojiId) {
return this.makeAPIRequest("DELETE", `applications/${applicationId}/emojis/${emojiId}`);
}
async getGuild(guildId, withCounts = false) {
return this.makeAPIRequest("GET", `guilds/${guildId}?with_counts=${withCounts}`);
}
async getGuildPreview(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/preview`);
}
async modifyGuild(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}`, body);
}
async deleteGuild(guildId) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}`);
}
async getGuildChannels(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/channels`);
}
async createGuildChannel(guildId, body) {
return this.makeAPIRequest("POST", `guilds/${guildId}/channels`, body);
}
async modifyGuildChannelPositions(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/channels`, body);
}
async listActiveGuildThreads(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/threads/active`);
}
async getGuildMember(guildId, userId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/members/${userId}`);
}
async listGuildMembers(guildId, params) {
let url = `guilds/${guildId}/members`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async searchGuildMembers(guildId, params) {
let url = `guilds/${guildId}/members/search`;
if (typeof params.query !== "undefined")
url += `query=${params.query}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async addGuildMember(guildId, userId, body) {
return this.makeAPIRequest("PUT", `guilds/${guildId}/members/${userId}`, body);
}
async modifyGuildMember(guildId, userId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/members/${userId}`, body);
}
async modifyCurrentMember(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/members/@me`, body);
}
async addGuildMemberRole(guildId, userId, roleId, reason) {
return this.makeAPIRequest("PUT", `guilds/${guildId}/members/${userId}/roles/${roleId}`, { reason });
}
async removeGuildMemberRole(guildId, userId, roleId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/members/${userId}/roles/${roleId}`, { reason });
}
async removeGuildMember(guildId, userId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/members/${userId}`, { reason });
}
async getGuildBans(guildId, params) {
let url = `guilds/${guildId}/bans`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async getGuildBan(guildId, userId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/bans/${userId}`);
}
async createGuildBan(guildId, userId, body) {
return this.makeAPIRequest("PUT", `guilds/${guildId}/bans/${userId}`, body);
}
async removeGuildBan(guildId, userId, reason) {
return this.makeAPIRequest("PUT", `guilds/${guildId}/bans/${userId}`, { reason });
}
async getGuildRoles(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/roles`);
}
async getGuildRole(guildId, roleId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/roles/${roleId}`);
}
async createGuildRole(guildId, body) {
return this.makeAPIRequest("POST", `guilds/${guildId}/roles`, body);
}
async modifyGuildRolePosition(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/roles`, body);
}
async modifyGuildRole(guildId, roleId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/roles/${roleId}`, body);
}
async modifyGuildMFALevel(guildId, level) {
return this.makeAPIRequest("POST", `guilds/${guildId}/mfa`, { level });
}
async deleteGuildRole(guildId, roleId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/roles/${roleId}`, { reason });
}
async getGuildPruneCount(guildId, params) {
let url = `guilds/${guildId}/prune`;
if (typeof params.days !== "undefined")
url += `days=${params.days}&`;
if (typeof params.include_roles !== "undefined")
url += `include_roles=${params.include_roles}`;
return this.makeAPIRequest("GET", url);
}
async beginGuildPrune(guildId, body) {
return this.makeAPIRequest("POST", `guilds/${guildId}/prune`, body);
}
async getGuildVoiceRegions(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/regions`);
}
async getGuildInvites(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/invites`);
}
async getGuildIntegrations(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/integrations`);
}
async deleteGuildIntegration(guildId, integrationId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/integrations/${integrationId}`, { reason });
}
async getGuildWidgetSettings(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/widget`);
}
async modifyGuildWidget(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/widget`, body);
}
async getGuildWidget(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/widget.json`);
}
async getGuildVanityUrl(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/vanity-url`);
}
async getGuildWidgetImage(guildId, style = "shield") {
return this.makeAPIRequest("GET", `guilds/${guildId}/widget.png?style=${style}`);
}
async getGuildWelcomeScreen(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/welcome-screen`);
}
async modifyGuildWelcomeScreen(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/welcome-screen`, body);
}
async getGuildOnboarding(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/onboarding`);
}
async modifyGuildOnboarding(guildId, body) {
return this.makeAPIRequest("PUT", `guilds/${guildId}/onboarding`, body);
}
async modifyCurrentUserVoiceState(guildId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/voice-states/@me`, body);
}
async modifyUserVoiceState(guildId, userId, body) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/voice-states/${userId}`, body);
}
async listScheduledEventsForGuild(guildId, params) {
let url = `guilds/${guildId}/scheduled-events?`;
if (typeof params.with_user_count !== "undefined")
url += `with_user_count=${params.with_user_count}`;
return this.makeAPIRequest("GET", url);
}
async createGuildScheduledEvent(guildId, event) {
return this.makeAPIRequest("POST", `guilds/${guildId}/scheduled-events`, event);
}
async getGuildScheduledEvent(guildId, eventId, params) {
let url = `guilds/${guildId}/scheduled-events/${eventId}?`;
if (typeof params.with_user_count !== "undefined")
url += `with_user_count=${params.with_user_count}`;
return this.makeAPIRequest("GET", url);
}
async modifyGuildScheduledEvent(guildId, eventId, event) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/scheduled-events/${eventId}`, event);
}
async deleteGuildScheduledEvent(guildId, eventId) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/scheduled-events/${eventId}`);
}
async getGuildScheduledEventUsers(guildId, eventId, params) {
let url = `guilds/${guildId}/scheduled-events/${eventId}?`;
if (typeof params.with_member !== "undefined")
url += `with_member=${params.with_member}&`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("DELETE", url);
}
async getGuildTemplate(templateCode) {
return this.makeAPIRequest("GET", `guilds/templates/${templateCode}`);
}
async createGuildFromGuildTemplate(templateCode, guild) {
return this.makeAPIRequest("POST", `guilds/templates/${templateCode}`, guild);
}
async getGuildTemplates(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/templates`);
}
async createGuildTemplate(guildId, template) {
return this.makeAPIRequest("POST", `guilds/${guildId}/templates`, template);
}
async syncGuildTemplate(guildId, templateCode) {
return this.makeAPIRequest("PUT", `guilds/${guildId}/templates/${templateCode}`);
}
async modifyGuildTemplate(guildId, templateCode, template) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/templates/${templateCode}`, template);
}
async deleteGuildTemplate(guildId, templateCode) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/templates/${templateCode}`);
}
async getInvite(inviteCode) {
return this.makeAPIRequest("GET", `invites/${inviteCode}`);
}
async deleteInvite(inviteCode, reason) {
return this.makeAPIRequest("DELETE", `invites/${inviteCode}`, { reason });
}
async getChannelMessages(channelId, params) {
let url = `channels/${channelId}/messages?`;
if (typeof params.around !== "undefined")
url += `around=${params.around}&`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async getChannelMessage(channelId, messageId) {
return this.makeAPIRequest("GET", `channels/${channelId}/messages/${messageId}`);
}
async createMessage(channelId, body, files) {
return this.makeAPIRequest("POST", `channels/${channelId}/messages`, body, files);
}
async crosspostMessage(channelId, messageId) {
return this.makeAPIRequest("POST", `channels/${channelId}/messages/${messageId}/crosspost`);
}
async createReaction(channelId, messageId, emoji, isCustom = false) {
if (!isCustom)
emoji = encodeURIComponent(emoji);
return this.makeAPIRequest("PUT", `channels/${channelId}/messages/${messageId}/reactions/${emoji}/@me`);
}
async deleteOwnReaction(channelId, messageId, emoji, isCustom = false) {
if (!isCustom)
emoji = encodeURIComponent(emoji);
return this.makeAPIRequest("DELETE", `channels/${channelId}/messages/${messageId}/reactions/${emoji}/@me`);
}
async deleteUserReaction(channelId, messageId, userId, emoji, isCustom = false) {
if (!isCustom)
emoji = encodeURIComponent(emoji);
return this.makeAPIRequest("DELETE", `channels/${channelId}/messages/${messageId}/reactions/${emoji}/${userId}`);
}
async getReactions(channelId, messageId, emoji, isCustom = false, params = {}) {
if (!isCustom)
emoji = encodeURIComponent(emoji);
let url = `channels/${channelId}/messages/${messageId}/reactions/${emoji}?`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async deleteAllReactions(channelId, messageId) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/messages/${messageId}/reactions`);
}
async deleteAllReactionsForEmoji(channelId, messageId, emoji, isCustom = false) {
if (!isCustom)
emoji = encodeURIComponent(emoji);
return this.makeAPIRequest("DELETE", `channels/${channelId}/messages/${messageId}/reactions/${emoji}`);
}
async editMessage(channelId, messageId, body, files) {
return this.makeAPIRequest("PATCH", `channels/${channelId}/messages/${messageId}`, body, files);
}
async deleteMessage(channelId, messageId, reason) {
return this.makeAPIRequest("DELETE", `channels/${channelId}/messages/${messageId}`, { reason });
}
async bulkDeleteMessages(channelId, messageIds, reason) {
return this.makeAPIRequest("POST", `channels/${channelId}/messages/bulk-delete`, { messages: messageIds, reason });
}
async getAnswerVoters(channelId, messageId, answerId, params) {
let url = `channels/${channelId}/polls/${messageId}/answers/${answerId}?`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async endPoll(channelId, messageId) {
return this.makeAPIRequest("POST", `channels/${channelId}/polls/${messageId}/expire`);
}
async createStageInstance(instance) {
return this.makeAPIRequest("POST", "stage-instances", instance);
}
async getStageInstance(channelId) {
return this.makeAPIRequest("GET", `stage-instances/${channelId}`);
}
async modifyStageInstance(channelId, data) {
return this.makeAPIRequest("PATCH", `stage-instances/${channelId}`, data);
}
async deleteStageInstance(channelId, reason) {
return this.makeAPIRequest("DELETE", `stage-instances/${channelId}`, { reason });
}
async getSticker(stickerId) {
return this.makeAPIRequest("GET", `stickers/${stickerId}`);
}
async listStickerPacks() {
return this.makeAPIRequest("GET", "sticker-packs");
}
async getStickerPack(packId) {
return this.makeAPIRequest("GET", `sticker-packs/${packId}`);
}
async listGuildStickers(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/stickers`);
}
async getGuildSticker(guildId, stickerId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/stickers/${stickerId}`);
}
async createGuildSticker(guildId, stickerId, params) {
const form = new FormData();
const { reason, ...obj } = params;
for (const key in obj)
form.append(key, obj[key]);
return this.makeAPIRequest("POST", `guilds/${guildId}/stickers/${stickerId}`, form, reason);
}
async modifyGuildSticker(guildId, stickerId, params) {
return this.makeAPIRequest("PATCH", `guilds/${guildId}/stickers/${stickerId}`, params);
}
async deleteGuildSticker(guildId, stickerId, reason) {
return this.makeAPIRequest("DELETE", `guilds/${guildId}/stickers/${stickerId}`, { reason });
}
async getCurrentUser() {
return this.makeAPIRequest("GET", "users/@me");
}
async getUser(userId) {
return this.makeAPIRequest("GET", `users/${userId}`);
}
async modifyCurrentUser(body) {
return this.makeAPIRequest("PATCH", "users/@me", body);
}
async getCurrentUserGuilds(params) {
let url = "users/@me/guilds?";
if (typeof params.withCounts !== "undefined")
url += `with_counts=${params.withCounts}&`;
if (typeof params.before !== "undefined")
url += `before=${params.before}&`;
if (typeof params.after !== "undefined")
url += `after=${params.after}&`;
if (typeof params.limit !== "undefined")
url += `limit=${params.limit}`;
return this.makeAPIRequest("GET", url);
}
async getCurrentUserGuildMember(guildId) {
return this.makeAPIRequest("GET", `users/@me/guilds/${guildId}/member`);
}
async leaveGuild(guildId) {
return this.makeAPIRequest("DELETE", `users/@me/guilds/${guildId}`);
}
async createDM(userId) {
return this.makeAPIRequest("POST", "users/@me/channels", { recipient_id: userId });
}
async createGroupDM(tokens, nicks) {
return this.makeAPIRequest("POST", "users/@me/channels", { access_tokens: tokens, nicks });
}
async listVoiceRegions() {
return this.makeAPIRequest("GET", "voice/regions");
}
async getCurrentUserVoiceState(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/voice-states/@me`);
}
async getUserVoiceState(guildId, userId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/voice-states/${userId}`);
}
async createWebhook(channelId, webhook) {
return this.makeAPIRequest("POST", `channels/${channelId}/webhooks`, webhook);
}
async getChannelWebhooks(channelId) {
return this.makeAPIRequest("GET", `channels/${channelId}/webhooks`);
}
async getGuildWebhooks(guildId) {
return this.makeAPIRequest("GET", `guilds/${guildId}/webhooks`);
}
async getWebhook(webhookId) {
return this.makeAPIRequest("GET", `webhooks/${webhookId}`);
}
async getWebhookWithToken(webhookId, token) {
return this.makeAPIRequest("GET", `webhooks/${webhookId}/${token}`);
}
async modifyWebhook(webhookId, webhook) {
return this.makeAPIRequest("PATCH", `webhooks/${webhookId}`, webhook);
}
async modifyWebhookWithToken(webhookId, token, webhook) {
return this.makeAPIRequest("PATCH", `webhooks/${webhookId}/${token}`, webhook);
}
async deleteWebhook(webhookId, reason) {
return this.makeAPIRequest("DELETE", `webhooks/${webhookId}`, { reason });
}
async deleteWebhookWithToken(webhookId, token, reason) {
return this.makeAPIRequest("DELETE", `webhooks/${webhookId}/${token}`, { reason });
}
async executeWebhook(webhookId, token, params, body, files) {
let url = `webhooks/${webhookId}/${token}?`;
if (typeof params.wait !== "undefined")
url += `wait=${params.wait}&`;
if (typeof params.thread_id !== "undefined")
url += `thread_id=${params.thread_id}&`;
return this.makeAPIRequest("POST", url, body, files);
}
async getWebhookMessage(webhookId, token, messageId, params) {
let url = `webhooks/${webhookId}/${token}/messages/${messageId}?`;
if (typeof params.thread_id !== "undefined")
url += `thread_id=${params.thread_id}`;
return this.makeAPIRequest("GET", url);
}
async editWebhookMessage(webhookId, token, messageId, params, body, files) {
let url = `webhooks/${webhookId}/${token}/messages/${messageId}?`;
if (typeof params.thread_id !== "undefined")
url += `thread_id=${params.thread_id}`;
return this.makeAPIRequest("PATCH", url, body, files);
}
async deleteWebhookMessage(webhookId, token, messageId, params) {
let url = `webhooks/${webhookId}/${token}/messages/${messageId}?`;
if (typeof params.thread_id !== "undefined")
url += `thread_id=${params.thread_id}`;
return this.makeAPIRequest("DELETE", url);
}
}
export class DebugREST extends REST {
#debug;
constructor(debug, token) {
super(token);
this.#debug = debug ?? (() => { });
}
async makeAPIRequest(method, path, data, filesOrReason) {
this.#debug(14, { method, path, data, filesOrReason });
try {
return await super.makeAPIRequest(method, path, data, filesOrReason);
}
catch (e) {
this.#debug(15, e);
throw e;
}
}
}