refactor(bot,plugins/helpers,rest,util)!: improve rest route handling (#2248)

* refactor(bot,helpers,plugins,rest,util)!: improve rest route handling
 - rename endpoints constant to routes
 - simplify routes code by removing bases and function calls
 - url query params can now be passed to the route functions

* style: deno fmt

* fix base stuff

* suggestions
This commit is contained in:
ITOH
2022-05-25 20:35:25 +02:00
committed by GitHub
parent bc2f66c773
commit 1edb3c2110
152 changed files with 745 additions and 475 deletions
+2 -2
View File
@@ -26,7 +26,7 @@ import {
CONTEXT_MENU_COMMANDS_NAME_REGEX,
DISCORD_SNOWFLAKE_REGEX,
DISCORDENO_VERSION,
endpoints,
routes,
SLASH_COMMANDS_NAME_REGEX,
USER_AGENT,
} from "./util/constants.ts";
@@ -710,7 +710,7 @@ export function createBotConstants() {
USER_AGENT,
BASE_URL: baseEndpoints.BASE_URL,
CDN_URL: baseEndpoints.CDN_URL,
endpoints,
routes,
regexes: {
SLASH_COMMANDS_NAME_REGEX,
CONTEXT_MENU_COMMANDS_NAME_REGEX,
+1 -1
View File
@@ -11,7 +11,7 @@ export async function createChannel(bot: Bot, guildId: bigint, options?: CreateG
const result = await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"post",
bot.constants.endpoints.GUILD_CHANNELS(guildId),
bot.constants.routes.GUILD_CHANNELS(guildId),
options
? {
name: options.name,
+1 -1
View File
@@ -6,7 +6,7 @@ export async function createStageInstance(bot: Bot, options: CreateStageInstance
const result = await bot.rest.runMethod<DiscordStageInstance>(
bot.rest,
"post",
bot.constants.endpoints.STAGE_INSTANCES(),
bot.constants.routes.STAGE_INSTANCES(),
{
channel_id: options.channelId.toString(),
topic: options.topic,
+1 -1
View File
@@ -6,7 +6,7 @@ export async function deleteChannel(bot: Bot, channelId: bigint, reason?: string
await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"delete",
bot.constants.endpoints.CHANNEL_BASE(channelId),
bot.constants.routes.CHANNEL(channelId),
{
reason,
},
+1 -1
View File
@@ -5,6 +5,6 @@ export async function deleteChannelOverwrite(bot: Bot, channelId: bigint, overwr
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
bot.constants.endpoints.CHANNEL_OVERWRITE(channelId, overwriteId),
bot.constants.routes.CHANNEL_OVERWRITE(channelId, overwriteId),
);
}
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../bot.ts";
/** Deletes the Stage instance. Requires the user to be a moderator of the Stage channel. */
export async function deleteStageInstance(bot: Bot, channelId: bigint) {
await bot.rest.runMethod(bot.rest, "delete", bot.constants.endpoints.STAGE_INSTANCE(channelId));
await bot.rest.runMethod(bot.rest, "delete", bot.constants.routes.STAGE_INSTANCE(channelId));
}
+1 -1
View File
@@ -35,7 +35,7 @@ export async function editChannel(bot: Bot, channelId: bigint, options: ModifyCh
const result = await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"patch",
bot.constants.endpoints.CHANNEL_BASE(channelId),
bot.constants.routes.CHANNEL(channelId),
{
name: options.name,
topic: options.topic,
+1 -1
View File
@@ -10,7 +10,7 @@ export async function editChannelOverwrite(
await bot.rest.runMethod<undefined>(
bot.rest,
"put",
bot.constants.endpoints.CHANNEL_OVERWRITE(channelId, overwrite.id),
bot.constants.routes.CHANNEL_OVERWRITE(channelId, overwrite.id),
{
allow: overwrite.allow ? bot.utils.calculateBits(overwrite.allow) : "0",
deny: overwrite.deny ? bot.utils.calculateBits(overwrite.deny) : "0",
+1 -1
View File
@@ -6,7 +6,7 @@ export async function followChannel(bot: Bot, sourceChannelId: bigint, targetCha
const data = await bot.rest.runMethod<DiscordFollowedChannel>(
bot.rest,
"post",
bot.constants.endpoints.CHANNEL_FOLLOW(sourceChannelId),
bot.constants.routes.CHANNEL_FOLLOW(sourceChannelId),
{
webhook_channel_id: targetChannelId,
},
+1 -1
View File
@@ -12,7 +12,7 @@ export async function createForumPost(
const result = await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"post",
bot.constants.endpoints.FORUM_START(channelId),
bot.constants.routes.FORUM_START(channelId),
{
name: options.name,
auto_archive_duration: options.autoArchiveDuration,
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getChannel(bot: Bot, channelId: bigint) {
const result = await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"get",
bot.constants.endpoints.CHANNEL_BASE(channelId),
bot.constants.routes.CHANNEL(channelId),
);
// IF A CHANNEL DOESN'T EXIST, DISCORD RETURNS `{}`
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getChannelWebhooks(bot: Bot, channelId: bigint) {
const result = await bot.rest.runMethod<DiscordWebhook[]>(
bot.rest,
"get",
bot.constants.endpoints.CHANNEL_WEBHOOKS(channelId),
bot.constants.routes.CHANNEL_WEBHOOKS(channelId),
);
return new Collection(result.map((hook) => {
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getChannels(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordChannel[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_CHANNELS(guildId),
bot.constants.routes.GUILD_CHANNELS(guildId),
);
return new Collection(
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getPins(bot: Bot, channelId: bigint) {
const result = await bot.rest.runMethod<DiscordMessage[]>(
bot.rest,
"get",
bot.constants.endpoints.CHANNEL_PINS(channelId),
bot.constants.routes.CHANNEL_PINS(channelId),
);
return result.map((msg) => bot.transformers.message(bot, msg));
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getStageInstance(bot: Bot, channelId: bigint) {
const result = await bot.rest.runMethod<DiscordStageInstance>(
bot.rest,
"get",
bot.constants.endpoints.STAGE_INSTANCE(channelId),
bot.constants.routes.STAGE_INSTANCE(channelId),
);
return bot.transformers.stageInstance(bot, result);
+1 -1
View File
@@ -6,5 +6,5 @@ import type { Bot } from "../../bot.ts";
* this endpoint may be called to let the user know that the bot is processing their message.
*/
export async function startTyping(bot: Bot, channelId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "post", bot.constants.endpoints.CHANNEL_TYPING(channelId));
await bot.rest.runMethod<undefined>(bot.rest, "post", bot.constants.routes.CHANNEL_TYPING(channelId));
}
+1 -1
View File
@@ -9,7 +9,7 @@ export async function swapChannels(bot: Bot, guildId: bigint, channelPositions:
await bot.rest.runMethod<undefined>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_CHANNELS(guildId),
bot.constants.routes.GUILD_CHANNELS(guildId),
channelPositions.map((channelPosition) => {
return {
id: channelPosition.id,
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Adds a user to a thread. Requires the ability to send messages in the thread. Requires the thread is not archived. */
export async function addToThread(bot: Bot, threadId: bigint, userId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "put", bot.constants.endpoints.THREAD_USER(threadId, userId));
await bot.rest.runMethod<undefined>(bot.rest, "put", bot.constants.routes.THREAD_USER(threadId, userId));
}
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getActiveThreads(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordListActiveThreads>(
bot.rest,
"get",
bot.constants.endpoints.THREAD_ACTIVE(guildId),
bot.constants.routes.THREAD_ACTIVE(guildId),
);
return {
@@ -11,21 +11,17 @@ export async function getArchivedThreads(
},
) {
let url = options?.type === "privateJoinedThreads"
? bot.constants.endpoints.THREAD_ARCHIVED_PRIVATE_JOINED(channelId)
? bot.constants.routes.THREAD_ARCHIVED_PRIVATE_JOINED(channelId, options)
: options?.type === "private"
? bot.constants.endpoints.THREAD_ARCHIVED_PRIVATE(channelId)
: bot.constants.endpoints.THREAD_ARCHIVED_PUBLIC(channelId);
if (options) {
url += "?";
? bot.constants.routes.THREAD_ARCHIVED_PRIVATE(channelId, options)
: bot.constants.routes.THREAD_ARCHIVED_PUBLIC(channelId, options);
if (options.before) url += `before=${new Date(options.before).toISOString()}`;
if (options.limit) url += `&limit=${options.limit}`;
}
const result = (await bot.rest.runMethod<DiscordListArchivedThreads>(
bot.rest,
"get",
url,
));
return {
threads: new Collection(
result.threads.map((t) => {
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getThreadMember(bot: Bot, threadId: bigint, userId: bigint
const result = await bot.rest.runMethod<DiscordThreadMember>(
bot.rest,
"get",
bot.constants.endpoints.THREAD_USER(threadId, userId),
bot.constants.routes.THREAD_USER(threadId, userId),
);
return {
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getThreadMembers(bot: Bot, threadId: bigint) {
const result = await bot.rest.runMethod<DiscordThreadMember[]>(
bot.rest,
"get",
bot.constants.endpoints.THREAD_MEMBERS(threadId),
bot.constants.routes.THREAD_MEMBERS(threadId),
);
// return result;
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Adds the bot to the thread. Cannot join an archived thread. */
export async function joinThread(bot: Bot, threadId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "put", bot.constants.endpoints.THREAD_ME(threadId));
await bot.rest.runMethod<undefined>(bot.rest, "put", bot.constants.routes.THREAD_ME(threadId));
}
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Removes the bot from a thread. Requires the thread is not archived. */
export async function leaveThread(bot: Bot, threadId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.THREAD_ME(threadId));
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.THREAD_ME(threadId));
}
@@ -2,5 +2,5 @@ import type { Bot } from "../../../bot.ts";
/** Removes a user from a thread. Requires the MANAGE_THREADS permission or that you are the creator of the thread. Also requires the thread is not archived. */
export async function removeThreadMember(bot: Bot, threadId: bigint, userId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.THREAD_USER(threadId, userId));
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.THREAD_USER(threadId, userId));
}
@@ -11,7 +11,7 @@ export async function startThreadWithMessage(
const result = await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"post",
bot.constants.endpoints.THREAD_START_PUBLIC(channelId, messageId),
bot.constants.routes.THREAD_START_PUBLIC(channelId, messageId),
{
name: options.name,
auto_archive_duration: options.autoArchiveDuration,
@@ -7,7 +7,7 @@ export async function startThreadWithoutMessage(bot: Bot, channelId: bigint, opt
const result = await bot.rest.runMethod<DiscordChannel>(
bot.rest,
"post",
bot.constants.endpoints.THREAD_START_PRIVATE(channelId),
bot.constants.routes.THREAD_START_PRIVATE(channelId),
{
name: options.name,
auto_archive_duration: options.autoArchiveDuration,
+1 -1
View File
@@ -11,7 +11,7 @@ export async function updateStageInstance(
const result = await bot.rest.runMethod<DiscordStageInstance>(
bot.rest,
"patch",
bot.constants.endpoints.STAGE_INSTANCE(channelId),
bot.constants.routes.STAGE_INSTANCE(channelId),
{
topic: data.topic,
},
+2 -2
View File
@@ -11,7 +11,7 @@ import type { Bot } from "../../bot.ts";
* - When suppressed, the user will have their `request_to_speak_timestamp` removed.
*/
export async function updateBotVoiceState(bot: Bot, guildId: bigint, options: UpdateSelfVoiceState) {
await bot.rest.runMethod(bot.rest, "patch", bot.constants.endpoints.UPDATE_VOICE_STATE(guildId), {
await bot.rest.runMethod(bot.rest, "patch", bot.constants.routes.UPDATE_VOICE_STATE(guildId), {
channel_id: options.channelId,
suppress: options.suppress,
request_to_speak_timestamp: options.requestToSpeakTimestamp
@@ -35,7 +35,7 @@ export async function updateUserVoiceState(bot: Bot, guildId: bigint, options: U
await bot.rest.runMethod(
bot.rest,
"patch",
bot.constants.endpoints.UPDATE_VOICE_STATE(guildId, options.userId),
bot.constants.routes.UPDATE_VOICE_STATE(guildId, options.userId),
{
channel_id: options.channelId,
suppress: options.suppress,
+1 -1
View File
@@ -6,6 +6,6 @@ export async function addDiscoverySubcategory(bot: Bot, guildId: bigint, categor
await bot.rest.runMethod<DiscordAddGuildDiscoverySubcategory>(
bot.rest,
"post",
bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId),
bot.constants.routes.DISCOVERY_SUBCATEGORY(guildId, categoryId),
);
}
+1 -1
View File
@@ -6,7 +6,7 @@ export async function editDiscovery(bot: Bot, guildId: bigint, data: ModifyGuild
const result = await bot.rest.runMethod<DiscordDiscoveryMetadata>(
bot.rest,
"patch",
bot.constants.endpoints.DISCOVERY_METADATA(guildId),
bot.constants.routes.DISCOVERY_METADATA(guildId),
{
primary_category_id: data.primaryCategoryId,
keywords: data.keywords,
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getDiscovery(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordDiscoveryMetadata>(
bot.rest,
"get",
bot.constants.endpoints.DISCOVERY_METADATA(guildId),
bot.constants.routes.DISCOVERY_METADATA(guildId),
);
return {
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getDiscoveryCategories(bot: Bot) {
const result = await bot.rest.runMethod<DiscordDiscoveryCategory[]>(
bot.rest,
"get",
bot.constants.endpoints.DISCOVERY_CATEGORIES(),
bot.constants.routes.DISCOVERY_CATEGORIES(),
);
return new Collection<number, DiscordDiscoveryCategory>(
@@ -5,6 +5,6 @@ export async function removeDiscoverySubcategory(bot: Bot, guildId: bigint, cate
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId),
bot.constants.routes.DISCOVERY_SUBCATEGORY(guildId, categoryId),
);
}
+1 -1
View File
@@ -5,7 +5,7 @@ export async function validDiscoveryTerm(bot: Bot, term: string) {
const result = await bot.rest.runMethod<DiscordValidateDiscoverySearchTerm>(
bot.rest,
"get",
`${bot.constants.endpoints.DISCOVERY_VALID_TERM()}?term=${term}`,
bot.constants.routes.DISCOVERY_VALID_TERM(term),
);
return result.valid;
+1 -1
View File
@@ -10,7 +10,7 @@ export async function createEmoji(bot: Bot, guildId: bigint, options: CreateGuil
const emoji = await bot.rest.runMethod<DiscordEmoji>(
bot.rest,
"post",
bot.constants.endpoints.GUILD_EMOJIS(guildId),
bot.constants.routes.GUILD_EMOJIS(guildId),
{
name: options.name,
image: options.image,
+1 -1
View File
@@ -2,7 +2,7 @@ import type { Bot } from "../../bot.ts";
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
export async function deleteEmoji(bot: Bot, guildId: bigint, id: bigint, reason?: string) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.GUILD_EMOJI(guildId, id), {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.GUILD_EMOJI(guildId, id), {
reason,
});
}
+1 -1
View File
@@ -6,7 +6,7 @@ export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options:
const result = await bot.rest.runMethod<DiscordEmoji>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_EMOJI(guildId, id),
bot.constants.routes.GUILD_EMOJI(guildId, id),
{
name: options.name,
// NEED TERNARY TO SUPPORT NULL AS VALID
+1 -1
View File
@@ -8,7 +8,7 @@ export async function getEmoji(bot: Bot, guildId: bigint, emojiId: bigint) {
const result = await bot.rest.runMethod<DiscordEmoji>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_EMOJI(guildId, emojiId),
bot.constants.routes.GUILD_EMOJI(guildId, emojiId),
);
return bot.transformers.emoji(bot, result);
+1 -1
View File
@@ -9,7 +9,7 @@ export async function getEmojis(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordEmoji[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_EMOJIS(guildId),
bot.constants.routes.GUILD_EMOJIS(guildId),
);
return new Collection(result.map((e) => [bot.transformers.snowflake(e.id!), bot.transformers.emoji(bot, e)]));
+1 -1
View File
@@ -11,7 +11,7 @@ import {
/** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */
export async function createGuild(bot: Bot, options: CreateGuild) {
const result = await bot.rest.runMethod<DiscordGuild>(bot.rest, "post", bot.constants.endpoints.GUILDS(), {
const result = await bot.rest.runMethod<DiscordGuild>(bot.rest, "post", bot.constants.routes.GUILDS(), {
name: options.name,
afk_channel_id: options.afkChannelId,
afk_timeout: options.afkTimeout,
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../bot.ts";
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event. */
export async function deleteGuild(bot: Bot, guildId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.GUILDS_BASE(guildId));
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.GUILD(guildId));
}
+1 -1
View File
@@ -25,7 +25,7 @@ export async function editGuild(bot: Bot, guildId: bigint, options: ModifyGuild,
const result = await bot.rest.runMethod<DiscordGuild>(
bot.rest,
"patch",
bot.constants.endpoints.GUILDS_BASE(guildId),
bot.constants.routes.GUILD(guildId),
options,
);
+1 -1
View File
@@ -5,7 +5,7 @@ export async function editWelcomeScreen(bot: Bot, guildId: bigint, options: Modi
const result = await bot.rest.runMethod<DiscordWelcomeScreen>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_WELCOME_SCREEN(guildId),
bot.constants.routes.GUILD_WELCOME_SCREEN(guildId),
{
enabled: options.enabled,
welcome_screen: options.welcomeScreen?.map((welcomeScreen) => ({
+1 -1
View File
@@ -6,7 +6,7 @@ export async function editWidget(bot: Bot, guildId: bigint, enabled: boolean, ch
const result = await bot.rest.runMethod<DiscordGuildWidgetSettings>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_WIDGET(guildId),
bot.constants.routes.GUILD_WIDGET(guildId),
{
enabled,
channel_id: channelId,
+1 -10
View File
@@ -6,19 +6,10 @@ import { AuditLogEvents } from "../../types/shared.ts";
export async function getAuditLogs(bot: Bot, guildId: bigint, options?: GetGuildAuditLog) {
if (options?.limit) options.limit = options.limit >= 1 && options.limit <= 100 ? options.limit : 50;
let url = bot.constants.endpoints.GUILD_AUDIT_LOGS(guildId);
if (options) {
url += "?";
if (options.actionType) url += `action_type=${options.actionType}`;
if (options.before) url += `&before=${options.before}`;
if (options.limit) url += `&limit=${options.limit}`;
if (options.userId) url += `&user_id=${options.userId}`;
}
const auditlog = await bot.rest.runMethod<DiscordAuditLog>(
bot.rest,
"get",
url,
bot.constants.routes.GUILD_AUDIT_LOGS(guildId, options),
);
return {
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getAvailableVoiceRegions(bot: Bot) {
const result = await bot.rest.runMethod<DiscordVoiceRegion[]>(
bot.rest,
"get",
bot.constants.endpoints.VOICE_REGIONS(),
bot.constants.routes.VOICE_REGIONS(),
);
return new Collection(
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getBan(bot: Bot, guildId: bigint, memberId: bigint) {
const result = await bot.rest.runMethod<DiscordBan>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_BAN(guildId, memberId),
bot.constants.routes.GUILD_BAN(guildId, memberId),
);
return {
+5 -11
View File
@@ -5,17 +5,11 @@ import { User } from "../../transformers/member.ts";
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
export async function getBans(bot: Bot, guildId: bigint, options?: GetBans) {
let url = bot.constants.endpoints.GUILD_BANS(guildId);
if (options) {
url += "?";
if (options.limit) url += `limit=${options.limit}`;
if (options.after) url += `&after=${options.after}`;
if (options.before) url += `&before=${options.before}`;
}
const results = await bot.rest.runMethod<DiscordBan[]>(bot.rest, "get", url);
const results = await bot.rest.runMethod<DiscordBan[]>(
bot.rest,
"get",
bot.constants.routes.GUILD_BANS(guildId, options),
);
return new Collection<bigint, { reason?: string; user: User }>(
results.map((res) => [
+1 -1
View File
@@ -15,7 +15,7 @@ export async function getGuild(
const result = await bot.rest.runMethod<DiscordGuild>(
bot.rest,
"get",
`${bot.constants.endpoints.GUILDS_BASE(guildId)}?with_counts=${options.counts ?? false}`,
bot.constants.routes.GUILD(guildId, options.counts),
);
return bot.transformers.guild(bot, {
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getGuildPreview(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordGuildPreview>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_PREVIEW(guildId),
bot.constants.routes.GUILD_PREVIEW(guildId),
);
return {
+1 -10
View File
@@ -5,19 +5,10 @@ export async function getPruneCount(bot: Bot, guildId: bigint, options?: GetGuil
if (options?.days && options.days < 1) throw new Error(bot.constants.Errors.PRUNE_MIN_DAYS);
if (options?.days && options.days > 30) throw new Error(bot.constants.Errors.PRUNE_MAX_DAYS);
let url = bot.constants.endpoints.GUILD_PRUNE(guildId);
if (options) {
url += "?";
if (options.days) url += `days=${options.days}`;
if (options.includeRoles) url += `&include_roles=${options.includeRoles}`;
}
const result = await bot.rest.runMethod(
bot.rest,
"get",
url,
bot.constants.routes.GUILD_PRUNE(guildId),
);
return result.pruned as number;
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getVanityUrl(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<Partial<DiscordInviteMetadata>>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_VANITY_URL(guildId),
bot.constants.routes.GUILD_VANITY_URL(guildId),
);
return {
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getVoiceRegions(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordVoiceRegion[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_REGIONS(guildId),
bot.constants.routes.GUILD_REGIONS(guildId),
);
return new Collection(
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getWelcomeScreen(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordWelcomeScreen>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_WELCOME_SCREEN(guildId),
bot.constants.routes.GUILD_WELCOME_SCREEN(guildId),
);
return bot.transformers.welcomeScreen(bot, result);
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getWidget(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordGuildWidget>(
bot.rest,
"get",
`${bot.constants.endpoints.GUILD_WIDGET(guildId)}.json`,
bot.constants.routes.GUILD_WIDGET_JSON(guildId),
);
return bot.transformers.widget(bot, result);
+1 -1
View File
@@ -2,7 +2,7 @@ import type { Bot } from "../../bot.ts";
/** Returns the widget image URL for the guild. */
export async function getWidgetImageURL(bot: Bot, guildId: bigint, options?: GetGuildWidgetImageQuery) {
return `${bot.constants.endpoints.GUILD_WIDGET(guildId)}.png?style=${options?.style ?? "shield"}`;
return bot.constants.routes.GUILD_WIDGET_IMAGE(guildId, options?.style);
}
/** https://discord.com/developers/docs/resources/guild#get-guild-widget-image-query-string-params */
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getWidgetSettings(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordGuildWidgetSettings>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_WIDGET(guildId),
bot.constants.routes.GUILD_WIDGET(guildId),
);
return bot.transformers.widgetSettings(bot, result);
+1 -1
View File
@@ -13,7 +13,7 @@ export function guildBannerURL(
) {
return options.banner
? bot.utils.formatImageURL(
bot.constants.endpoints.GUILD_BANNER(
bot.constants.routes.GUILD_BANNER(
id,
typeof options.banner === "string" ? options.banner : bot.utils.iconBigintToHash(options.banner),
),
+1 -1
View File
@@ -13,7 +13,7 @@ export function guildIconURL(
) {
return icon
? bot.utils.formatImageURL(
bot.constants.endpoints.GUILD_ICON(
bot.constants.routes.GUILD_ICON(
id,
typeof icon === "string" ? icon : bot.utils.iconBigintToHash(icon),
),
+1 -1
View File
@@ -13,7 +13,7 @@ export function guildSplashURL(
) {
return splash
? bot.utils.formatImageURL(
bot.constants.endpoints.GUILD_SPLASH(
bot.constants.routes.GUILD_SPLASH(
id,
typeof splash === "string" ? splash : bot.utils.iconBigintToHash(splash),
),
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../bot.ts";
/** Leave a guild */
export async function leaveGuild(bot: Bot, guildId: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.GUILD_LEAVE(guildId));
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.GUILD_LEAVE(guildId));
}
@@ -29,7 +29,7 @@ export async function createScheduledEvent(bot: Bot, guildId: bigint, options: C
const event = await bot.rest.runMethod<DiscordScheduledEvent>(
bot.rest,
"post",
bot.constants.endpoints.GUILD_SCHEDULED_EVENTS(guildId),
bot.constants.routes.GUILD_SCHEDULED_EVENTS(guildId),
{
channel_id: options.channelId?.toString(),
entity_metadata: options.location ? { location: options.location } : undefined,
@@ -5,6 +5,6 @@ export async function deleteScheduledEvent(bot: Bot, guildId: bigint, eventId: b
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
bot.constants.endpoints.GUILD_SCHEDULED_EVENT(guildId, eventId),
bot.constants.routes.GUILD_SCHEDULED_EVENT(guildId, eventId),
);
}
@@ -25,7 +25,7 @@ export async function editScheduledEvent(
const event = await bot.rest.runMethod<DiscordScheduledEvent>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_SCHEDULED_EVENT(guildId, eventId),
bot.constants.routes.GUILD_SCHEDULED_EVENT(guildId, eventId),
{
channel_id: options.channelId === null ? null : options.channelId?.toString(),
entity_metadata: options.location ? { location: options.location } : undefined,
@@ -11,9 +11,7 @@ export async function getScheduledEvent(
const event = await bot.rest.runMethod<DiscordScheduledEvent>(
bot.rest,
"get",
`${bot.constants.endpoints.GUILD_SCHEDULED_EVENT(guildId, eventId)}?with_user_count=${
options?.withUserCount ?? false
}`,
bot.constants.routes.GUILD_SCHEDULED_EVENT(guildId, eventId, options?.withUserCount),
);
return bot.transformers.scheduledEvent(bot, event);
@@ -23,7 +23,7 @@ export async function getScheduledEventUsers(
): Promise<
Collection<bigint, User> | Collection<bigint, { user: User; member: Member }>
> {
let url = bot.constants.endpoints.GUILD_SCHEDULED_EVENT_USERS(guildId, eventId);
let url = bot.constants.routes.GUILD_SCHEDULED_EVENT_USERS(guildId, eventId, options);
if (options) {
url = "?";
@@ -8,7 +8,7 @@ export async function getScheduledEvents(bot: Bot, guildId: bigint, options?: Ge
const events = await bot.rest.runMethod<DiscordScheduledEvent[]>(
bot.rest,
"get",
`${bot.constants.endpoints.GUILD_SCHEDULED_EVENTS(guildId)}?with_user_count=${options?.withUserCount ?? false}`,
bot.constants.routes.GUILD_SCHEDULED_EVENTS(guildId, options?.withUserCount),
);
return new Collection<bigint, ScheduledEvent>(
+1 -1
View File
@@ -2,5 +2,5 @@ import type { Bot } from "../../bot.ts";
/** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */
export async function deleteIntegration(bot: Bot, guildId: bigint, id: bigint) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.GUILD_INTEGRATION(guildId, id));
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.GUILD_INTEGRATION(guildId, id));
}
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getIntegrations(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordIntegration[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_INTEGRATIONS(guildId),
bot.constants.routes.GUILD_INTEGRATIONS(guildId),
);
return new Collection(
@@ -23,8 +23,8 @@ export async function createApplicationCommand(
bot.rest,
"post",
guildId
? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.endpoints.COMMANDS(bot.applicationId),
? bot.constants.routes.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.routes.COMMANDS(bot.applicationId),
isContextApplicationCommand(options)
? { name: options.name, name_localizations: options.nameLocalizations, type: options.type }
: {
@@ -6,7 +6,7 @@ export async function deleteApplicationCommand(bot: Bot, id: bigint, guildId?: b
bot.rest,
"delete",
guildId
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, id)
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, id),
? bot.constants.routes.COMMANDS_GUILD_ID(bot.applicationId, guildId, id)
: bot.constants.routes.COMMANDS_ID(bot.applicationId, id),
);
}
@@ -6,7 +6,7 @@ export async function deleteInteractionResponse(bot: Bot, token: string, message
bot.rest,
"delete",
messageId
? bot.constants.endpoints.INTERACTION_ID_TOKEN_MESSAGE_ID(bot.applicationId, token, messageId)
: bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
? bot.constants.routes.INTERACTION_ID_TOKEN_MESSAGE_ID(bot.applicationId, token, messageId)
: bot.constants.routes.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
);
}
@@ -14,7 +14,7 @@ export async function editApplicationCommandPermissions(
const result = await bot.rest.runMethod<DiscordGuildApplicationCommandPermissions>(
bot.rest,
"put",
bot.constants.endpoints.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId),
bot.constants.routes.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId),
{
permissions: options,
},
@@ -15,8 +15,8 @@ export async function editInteractionResponse(
bot.rest,
"patch",
options.messageId
? bot.constants.endpoints.WEBHOOK_MESSAGE(bot.applicationId, token, options.messageId)
: bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
? bot.constants.routes.WEBHOOK_MESSAGE(bot.applicationId, token, options.messageId)
: bot.constants.routes.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
{
content: options.content,
embeds: options.embeds?.map((embed) => bot.transformers.reverse.embed(bot, embed)),
@@ -3,20 +3,17 @@ import { DiscordApplicationCommand } from "../../../types/discord.ts";
/** Fetches the global command for the given Id. If a guildId is provided, the guild command will be fetched. */
export async function getApplicationCommand(bot: Bot, commandId: bigint, options?: GetApplicationCommand) {
let url = `${
options?.guildId
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, options.guildId, commandId)
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, commandId)
}?`;
if (options?.withLocalizations !== undefined) {
url += `with_localizations=${options.withLocalizations}`;
}
const result = await bot.rest.runMethod<DiscordApplicationCommand>(
bot.rest,
"get",
url,
options?.guildId
? bot.constants.routes.COMMANDS_GUILD_ID(
bot.applicationId,
options.guildId,
commandId,
options?.withLocalizations,
)
: bot.constants.routes.COMMANDS_ID(bot.applicationId, commandId, options?.withLocalizations),
);
return bot.transformers.applicationCommand(bot, result);
@@ -6,7 +6,7 @@ export async function getApplicationCommandPermission(bot: Bot, guildId: bigint,
const result = await bot.rest.runMethod<DiscordGuildApplicationCommandPermissions>(
bot.rest,
"get",
bot.constants.endpoints.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId),
bot.constants.routes.COMMANDS_PERMISSION(bot.applicationId, guildId, commandId),
);
return bot.transformers.applicationCommandPermission(bot, result);
@@ -7,7 +7,7 @@ export async function getApplicationCommandPermissions(bot: Bot, guildId: bigint
const result = await bot.rest.runMethod<DiscordGuildApplicationCommandPermissions[]>(
bot.rest,
"get",
bot.constants.endpoints.COMMANDS_PERMISSIONS(bot.applicationId, guildId),
bot.constants.routes.COMMANDS_PERMISSIONS(bot.applicationId, guildId),
);
return new Collection(
@@ -8,8 +8,8 @@ export async function getApplicationCommands(bot: Bot, guildId?: bigint) {
bot.rest,
"get",
guildId
? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.endpoints.COMMANDS(bot.applicationId),
? bot.constants.routes.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.routes.COMMANDS(bot.applicationId),
);
return new Collection(
@@ -21,8 +21,8 @@ export async function upsertApplicationCommand(
bot.rest,
"patch",
guildId
? bot.constants.endpoints.COMMANDS_GUILD_ID(bot.applicationId, guildId, commandId)
: bot.constants.endpoints.COMMANDS_ID(bot.applicationId, commandId),
? bot.constants.routes.COMMANDS_GUILD_ID(bot.applicationId, guildId, commandId)
: bot.constants.routes.COMMANDS_ID(bot.applicationId, commandId),
isContextApplicationCommand(options)
? {
name: options.name,
@@ -23,8 +23,8 @@ export async function upsertApplicationCommands(
bot.rest,
"put",
guildId
? bot.constants.endpoints.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.endpoints.COMMANDS(bot.applicationId),
? bot.constants.routes.COMMANDS_GUILD(bot.applicationId, guildId)
: bot.constants.routes.COMMANDS(bot.applicationId),
options.map((option) => (isContextApplicationCommand(option)
? {
name: option.name,
@@ -5,6 +5,6 @@ export async function deleteFollowupMessage(bot: Bot, interactionToken: string,
await bot.rest.runMethod<undefined>(
bot.rest,
"delete",
bot.constants.endpoints.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId),
bot.constants.routes.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId),
);
}
@@ -13,7 +13,7 @@ export async function editFollowupMessage(
const result = await bot.rest.runMethod<DiscordMessage>(
bot.rest,
"patch",
bot.constants.endpoints.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId),
bot.constants.routes.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId),
{
content: options.content,
embeds: options.embeds?.map((embed) => bot.transformers.reverse.embed(bot, embed)),
@@ -6,7 +6,7 @@ export async function getFollowupMessage(bot: Bot, interactionToken: string, mes
const result = await bot.rest.runMethod<DiscordMessage>(
bot.rest,
"get",
bot.constants.endpoints.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId),
bot.constants.routes.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId),
);
return bot.transformers.message(bot, result);
@@ -6,7 +6,7 @@ export async function getOriginalInteractionResponse(bot: Bot, token: string) {
const result = await bot.rest.runMethod<DiscordMessage>(
bot.rest,
"get",
bot.constants.endpoints.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
bot.constants.routes.INTERACTION_ORIGINAL_ID_TOKEN(bot.applicationId, token),
);
return bot.transformers.message(bot, result);
@@ -100,7 +100,7 @@ export async function sendInteractionResponse(
return await bot.rest.runMethod<undefined>(
bot.rest,
"post",
bot.constants.endpoints.INTERACTION_ID_TOKEN(id, token),
bot.constants.routes.INTERACTION_ID_TOKEN(id, token),
{
type: options.type,
data,
@@ -113,7 +113,7 @@ export async function sendInteractionResponse(
const result = await bot.rest.runMethod<DiscordMessage>(
bot.rest,
"post",
bot.constants.endpoints.WEBHOOK(bot.applicationId, token),
bot.constants.routes.WEBHOOK(bot.applicationId, token),
{ ...data, file: options.data.file },
);
+1 -1
View File
@@ -7,7 +7,7 @@ export async function createInvite(bot: Bot, channelId: bigint, options: CreateC
const result = await bot.rest.runMethod<DiscordInvite>(
bot.rest,
"post",
bot.constants.endpoints.CHANNEL_INVITES(channelId),
bot.constants.routes.CHANNEL_INVITES(channelId),
{
max_age: options.maxAge,
max_uses: options.maxUses,
+1 -1
View File
@@ -3,5 +3,5 @@ import { DiscordInviteMetadata } from "../../types/discord.ts";
/** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */
export async function deleteInvite(bot: Bot, inviteCode: string) {
await bot.rest.runMethod<DiscordInviteMetadata>(bot.rest, "delete", bot.constants.endpoints.INVITE(inviteCode));
await bot.rest.runMethod<DiscordInviteMetadata>(bot.rest, "delete", bot.constants.routes.INVITE(inviteCode));
}
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getChannelInvites(bot: Bot, channelId: bigint) {
const result = await bot.rest.runMethod<DiscordInviteMetadata[]>(
bot.rest,
"get",
bot.constants.endpoints.CHANNEL_INVITES(channelId),
bot.constants.routes.CHANNEL_INVITES(channelId),
);
return new Collection(
+1 -10
View File
@@ -3,19 +3,10 @@ import { DiscordInviteMetadata } from "../../types/discord.ts";
/** Returns an invite for the given code or throws an error if the invite doesn't exists. */
export async function getInvite(bot: Bot, inviteCode: string, options?: GetInvite) {
let url = bot.constants.endpoints.INVITE(inviteCode);
if (options) {
url += "?";
if (options.withCounts) url += `with_counts=${options.withCounts}`;
if (options.withExpiration) url += `&with_expiration=${options.withExpiration}`;
if (options.scheduledEventId) url += `&guild_scheduled_event_id=${options.scheduledEventId}`;
}
const result = await bot.rest.runMethod<DiscordInviteMetadata>(
bot.rest,
"get",
url,
bot.constants.routes.INVITE(inviteCode, options),
);
return {
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getInvites(bot: Bot, guildId: bigint) {
const result = await bot.rest.runMethod<DiscordInviteMetadata[]>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_INVITES(guildId),
bot.constants.routes.GUILD_INVITES(guildId),
);
return new Collection(
+2 -2
View File
@@ -13,14 +13,14 @@ export function avatarURL(
) {
return options?.avatar
? bot.utils.formatImageURL(
bot.constants.endpoints.USER_AVATAR(
bot.constants.routes.USER_AVATAR(
userId,
typeof options?.avatar === "string" ? options.avatar : bot.utils.iconBigintToHash(options?.avatar),
),
options?.size || 128,
options?.format,
)
: bot.constants.endpoints.USER_DEFAULT_AVATAR(Number(discriminator) % 5);
: bot.constants.routes.USER_DEFAULT_AVATAR(Number(discriminator) % 5);
}
/**
+1 -1
View File
@@ -5,7 +5,7 @@ export async function banMember(bot: Bot, guildId: bigint, id: bigint, options?:
await bot.rest.runMethod<undefined>(
bot.rest,
"put",
bot.constants.endpoints.GUILD_BAN(guildId, id),
bot.constants.routes.GUILD_BAN(guildId, id),
options
? {
delete_message_days: options.deleteMessageDays,
+1 -1
View File
@@ -5,7 +5,7 @@ export async function editBotNickname(bot: Bot, guildId: bigint, options: { nick
const response = await bot.rest.runMethod<{ nick: string }>(
bot.rest,
"patch",
bot.constants.endpoints.USER_NICK(guildId),
bot.constants.routes.USER_NICK(guildId),
options,
);
+1 -1
View File
@@ -6,7 +6,7 @@ export async function editMember(bot: Bot, guildId: bigint, memberId: bigint, op
const result = await bot.rest.runMethod<DiscordMemberWithUser>(
bot.rest,
"patch",
bot.constants.endpoints.GUILD_MEMBER(guildId, memberId),
bot.constants.routes.GUILD_MEMBER(guildId, memberId),
{
nick: options.nick,
roles: options.roles?.map((id) => id.toString()),
+1 -1
View File
@@ -5,7 +5,7 @@ import { DiscordChannel } from "../../types/discord.ts";
export async function getDmChannel(bot: Bot, userId: bigint) {
if (userId === bot.id) throw new Error(bot.constants.Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF);
const dmChannelData = await bot.rest.runMethod<DiscordChannel>(bot.rest, "post", bot.constants.endpoints.USER_DM(), {
const dmChannelData = await bot.rest.runMethod<DiscordChannel>(bot.rest, "post", bot.constants.routes.USER_DM(), {
recipient_id: userId.toString(),
});
+1 -1
View File
@@ -6,7 +6,7 @@ export async function getMember(bot: Bot, guildId: bigint, id: bigint) {
const data = await bot.rest.runMethod<DiscordMemberWithUser>(
bot.rest,
"get",
bot.constants.endpoints.GUILD_MEMBER(guildId, id),
bot.constants.routes.GUILD_MEMBER(guildId, id),
);
return bot.transformers.member(bot, data, guildId, id);
+2 -5
View File
@@ -2,20 +2,17 @@ import type { Bot } from "../../bot.ts";
import { DiscordMemberWithUser } from "../../types/discord.ts";
import { Collection } from "../../util/collection.ts";
// TODO: make options optional
/**
* Highly recommended to **NOT** use this function to get members instead use fetchMembers().
* REST(this function): 50/s global(across all shards) rate limit with ALL requests this included
* GW(fetchMembers): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is 960/m.
*/
export async function getMembers(bot: Bot, guildId: bigint, options: ListGuildMembers) {
let url = `${bot.constants.endpoints.GUILD_MEMBERS(guildId)}?limit=${options.limit || 1000}`;
if (options.after) url += `&after=${options.after}`;
const result = await bot.rest.runMethod<DiscordMemberWithUser[]>(
bot.rest,
"get",
url,
bot.constants.routes.GUILD_MEMBERS(guildId, options),
);
return new Collection(
+1 -1
View File
@@ -2,7 +2,7 @@ import { Bot } from "../../bot.ts";
/** Kick a member from the server */
export async function kickMember(bot: Bot, guildId: bigint, memberId: bigint, reason?: string) {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.endpoints.GUILD_MEMBER(guildId, memberId), {
await bot.rest.runMethod<undefined>(bot.rest, "delete", bot.constants.routes.GUILD_MEMBER(guildId, memberId), {
reason,
});
}
+1 -1
View File
@@ -12,7 +12,7 @@ export async function pruneMembers(bot: Bot, guildId: bigint, options: BeginGuil
const result = await bot.rest.runMethod<{ pruned: number }>(
bot.rest,
"post",
bot.constants.endpoints.GUILD_PRUNE(guildId),
bot.constants.routes.GUILD_PRUNE(guildId),
{
days: options.days,
compute_prune_count: options.computePruneCount,

Some files were not shown because too many files have changed in this diff Show More