From c9659bca4c62cd2e2a5d8826d6b78d2105868776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Oszcz=C4=99da?= Date: Sun, 28 Aug 2022 12:17:08 +0100 Subject: [PATCH] feat!: Add return types to helper functions. (#2395) * feat: Add return types to forum channel helpers. * fix: Use Discord-defined property names. Add `flags` property. * feat: Add return types to thread channel helpers. * feat: Add return types to channel helpers. * feat: Add return types to server discovery helpers. * misc!: Consistency: 'remove' -> 'delete'. * misc!: Add `get` keyword to function and file name. * feat: Add return types to emoji helpers. * misc!: Remove unused `bot` parameter. Capitalise 'URL'. * feat: Add return types to guild automod helpers. * feat: Add return types to guild scheduled event helpers. * misc!: Consistency: Rename `emojiURL` to `getEmojiURL`. * feat: Add return types to guild helpers. * misc!: Consistency: Add 'get' keyword to function and file names. * feat: Add return types to invite helpers. * feat: Add return types to integration helpers. * feat: Add return types to application command helpers. * feat: Add return types to followup message helpers. * feat: Add return types to interaction response helpers. * feat: Add return type to `createInvite()`. * feat: Add return types to member helpers. * misc!: Consistency: Add 'get' keyword to function and file names. * feat: Add return types to message helpers. * misc!: Consistency: 'remove' -> 'delete'. * fix: Use `slice()` to prevent unwanted side effects. * feat: Add return types to miscellaneous helpers. * misc!: Consistency: Add 'get' keyword to function and file names. * feat: Add return types to role helpers. * feat: Add return types to oauth helpers. * feat: Add return types to template helpers. * misc!: Consistency: Add 'guild' keyword to name. * feat: Add return types to voice helpers. * fix: Name function correctly. * feat: Add return types to webhook helpers. * misc!: Consistency: Rename `sendWebhook` to `sendWebhookMessage`. * misc: Update exports. * fix: Imports. * fix: Change return collection key type from `string` to `bigint`. Remove redundant code. * misc: Remove `undefined` from `runMethod()` return type. * style: Remove redundant types. * style: Rename endpoint call result variables to `result` and `results`. * misc: Reintroduce `bot` as first parameter. * misc: Adapt tests to changes made to helpers. * fix: Object being transformed twice. * style: Improve naming consistency of remaining files. * misc: `bigint` -> `number`. * style: Remove explicit `undefined` return. * style: Remove `void` operator in favour of generic type. * misc: Add missing `await` keyword. Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> * style: Make `reason` property optional instead of `undefined`-able. * misc: Write out properties manually instead of using the spread operator. Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> --- helpers/channels/createChannel.ts | 10 ++- helpers/channels/createStageInstance.ts | 3 +- helpers/channels/deleteChannel.ts | 5 +- helpers/channels/deleteChannelOverwrite.ts | 4 +- helpers/channels/deleteStageInstance.ts | 4 +- helpers/channels/editChannel.ts | 9 +- helpers/channels/editChannelOverwrite.ts | 4 +- helpers/channels/followChannel.ts | 6 +- helpers/channels/forums/createForumPost.ts | 5 +- helpers/channels/getChannel.ts | 3 +- helpers/channels/getChannelWebhooks.ts | 17 ++-- helpers/channels/getChannels.ts | 12 ++- helpers/channels/getPins.ts | 13 ++- helpers/channels/getStageInstance.ts | 3 +- helpers/channels/startTyping.ts | 4 +- helpers/channels/swapChannels.ts | 24 ++--- helpers/channels/threads/addToThread.ts | 4 +- helpers/channels/threads/getActiveThreads.ts | 20 +++-- .../channels/threads/getArchivedThreads.ts | 27 +++--- helpers/channels/threads/getThreadMember.ts | 10 +-- helpers/channels/threads/getThreadMembers.ts | 16 ++-- helpers/channels/threads/joinThread.ts | 4 +- helpers/channels/threads/leaveThread.ts | 4 +- .../channels/threads/removeThreadMember.ts | 8 +- .../threads/startThreadWithMessage.ts | 3 +- .../threads/startThreadWithoutMessage.ts | 7 +- helpers/channels/updateStageInstance.ts | 3 +- helpers/channels/updateVoiceState.ts | 8 +- helpers/discovery/addDiscoverySubcategory.ts | 5 +- .../discovery/deleteDiscoverySubcategory.ts | 10 +++ helpers/discovery/editDiscovery.ts | 9 +- helpers/discovery/getDiscovery.ts | 12 ++- helpers/discovery/getDiscoveryCategories.ts | 24 +++-- ...veryTerm.ts => getIsValidDiscoveryTerm.ts} | 2 +- helpers/discovery/mod.ts | 4 +- .../discovery/removeDiscoverySubcategory.ts | 10 --- helpers/emojis/createEmoji.ts | 7 +- helpers/emojis/deleteEmoji.ts | 4 +- helpers/emojis/editEmoji.ts | 3 +- helpers/emojis/getEmoji.ts | 3 +- .../emojis/{emojiUrl.ts => getEmojiUrl.ts} | 2 +- helpers/emojis/getEmojis.ts | 12 ++- helpers/emojis/mod.ts | 3 +- helpers/guilds/automod/createAutomodRule.ts | 11 ++- helpers/guilds/automod/deleteAutomodRule.ts | 4 +- helpers/guilds/automod/editAutomodRule.ts | 11 ++- helpers/guilds/automod/getAutomodRule.ts | 8 +- helpers/guilds/automod/getAutomodRules.ts | 15 ++-- helpers/guilds/automod/mod.ts | 6 +- helpers/guilds/createGuild.ts | 3 +- helpers/guilds/deleteGuild.ts | 4 +- helpers/guilds/editGuild.ts | 8 +- helpers/guilds/editWelcomeScreen.ts | 7 +- helpers/guilds/editWidget.ts | 8 +- helpers/guilds/getAuditLogs.ts | 50 ++++++++--- helpers/guilds/getAvailableVoiceRegions.ts | 11 +-- helpers/guilds/getBan.ts | 10 ++- helpers/guilds/getBans.ts | 25 +++--- helpers/guilds/getGuild.ts | 3 +- ...guildBannerUrl.ts => getGuildBannerUrl.ts} | 6 +- .../{guildIconUrl.ts => getGuildIconUrl.ts} | 6 +- helpers/guilds/getGuildPreview.ts | 19 +++- ...guildSplashUrl.ts => getGuildSplashUrl.ts} | 6 +- helpers/guilds/getPruneCount.ts | 10 ++- helpers/guilds/getVanityUrl.ts | 15 ++-- helpers/guilds/getVoiceRegions.ts | 11 +-- helpers/guilds/getWelcomeScreen.ts | 3 +- helpers/guilds/getWidget.ts | 3 +- helpers/guilds/getWidgetImageUrl.ts | 2 +- helpers/guilds/getWidgetSettings.ts | 3 +- helpers/guilds/leaveGuild.ts | 4 +- helpers/guilds/mod.ts | 9 +- .../scheduledEvents/createScheduledEvent.ts | 11 ++- .../scheduledEvents/deleteScheduledEvent.ts | 4 +- .../scheduledEvents/editScheduledEvent.ts | 7 +- .../scheduledEvents/getScheduledEvent.ts | 9 +- .../scheduledEvents/getScheduledEventUsers.ts | 12 +-- .../scheduledEvents/getScheduledEvents.ts | 14 +-- helpers/integrations/deleteIntegration.ts | 4 +- helpers/integrations/getIntegrations.ts | 39 +++++---- .../commands/createApplicationCommand.ts | 4 +- .../commands/deleteApplicationCommand.ts | 4 +- .../commands/deleteInteractionResponse.ts | 4 +- .../editApplicationCommandPermissions.ts | 3 +- .../commands/editInteractionResponse.ts | 8 +- .../commands/getApplicationCommand.ts | 3 +- .../getApplicationCommandPermission.ts | 7 +- .../getApplicationCommandPermissions.ts | 14 +-- .../commands/getApplicationCommands.ts | 14 +-- .../commands/upsertApplicationCommand.ts | 9 +- .../commands/upsertApplicationCommands.ts | 12 +-- .../followups/deleteFollowupMessage.ts | 4 +- .../followups/editFollowupMessage.ts | 3 +- .../followups/getFollowupMessage.ts | 3 +- .../getOriginalInteractionResponse.ts | 3 +- .../interactions/sendInteractionResponse.ts | 4 +- helpers/invites/createInvite.ts | 15 ++-- helpers/invites/deleteInvite.ts | 5 +- helpers/invites/getChannelInvites.ts | 46 +++++++--- helpers/invites/getInvite.ts | 29 ++++++- helpers/invites/getInvites.ts | 87 ++++++------------- helpers/members/banMember.ts | 4 +- helpers/members/editBotNickname.ts | 12 ++- helpers/members/editMember.ts | 8 +- helpers/members/fetchMembers.ts | 4 +- .../members/{avatarUrl.ts => getAvatarUrl.ts} | 4 +- helpers/members/getDmChannel.ts | 9 +- helpers/members/getMember.ts | 9 +- helpers/members/getMembers.ts | 13 ++- helpers/members/kickMember.ts | 13 ++- helpers/members/mod.ts | 3 +- helpers/members/pruneMembers.ts | 2 +- helpers/members/searchMembers.ts | 15 ++-- helpers/members/unbanMember.ts | 4 +- helpers/messages/addReaction.ts | 4 +- helpers/messages/addReactions.ts | 13 ++- helpers/messages/deleteAllReactions.ts | 10 +++ helpers/messages/deleteMessage.ts | 4 +- helpers/messages/deleteMessages.ts | 8 +- .../{removeReaction.ts => deleteReaction.ts} | 8 +- ...eactionEmoji.ts => deleteReactionEmoji.ts} | 9 +- helpers/messages/editMessage.ts | 3 +- helpers/messages/getMessage.ts | 6 +- helpers/messages/getMessages.ts | 15 ++-- helpers/messages/getReactions.ts | 17 ++-- helpers/messages/mod.ts | 7 +- helpers/messages/pinMessage.ts | 4 +- helpers/messages/publishMessage.ts | 7 +- helpers/messages/removeAllReactions.ts | 10 --- helpers/messages/sendMessage.ts | 9 +- helpers/messages/unpinMessage.ts | 8 +- helpers/misc/editBotProfile.ts | 6 +- helpers/misc/editBotStatus.ts | 4 +- helpers/misc/editShardStatus.ts | 4 +- helpers/misc/getGatewayBot.ts | 3 +- helpers/misc/getNitroStickerPacks.ts | 20 +++++ helpers/misc/getUser.ts | 5 +- helpers/misc/mod.ts | 3 +- helpers/misc/nitroStickerPacks.ts | 13 --- helpers/oauth/getApplicationInfo.ts | 3 +- helpers/roles/addRole.ts | 10 ++- helpers/roles/createRole.ts | 3 +- helpers/roles/deleteRole.ts | 4 +- helpers/roles/editRole.ts | 3 +- helpers/roles/getRoles.ts | 16 ++-- helpers/roles/modifyRolePositions.ts | 19 ++-- helpers/roles/removeRole.ts | 2 +- helpers/templates/createGuildFromTemplate.ts | 7 +- helpers/templates/createGuildTemplate.ts | 7 +- helpers/templates/deleteGuildTemplate.ts | 5 +- helpers/templates/editGuildTemplate.ts | 12 ++- .../{getTemplate.ts => getGuildTemplate.ts} | 3 +- helpers/templates/getGuildTemplates.ts | 14 ++- helpers/templates/mod.ts | 2 +- helpers/templates/syncGuildTemplate.ts | 7 +- helpers/voice/connectToVoiceChannel.ts | 4 +- helpers/voice/leaveVoiceChannel.ts | 10 +-- helpers/voice/mod.ts | 1 + helpers/webhooks/createWebhook.ts | 3 +- helpers/webhooks/deleteWebhook.ts | 4 +- helpers/webhooks/deleteWebhookMessage.ts | 14 +-- helpers/webhooks/deleteWebhookWithToken.ts | 4 +- helpers/webhooks/editWebhook.ts | 3 +- helpers/webhooks/editWebhookMessage.ts | 9 +- helpers/webhooks/editWebhookWithToken.ts | 3 +- helpers/webhooks/getWebhook.ts | 3 +- helpers/webhooks/getWebhookMessage.ts | 3 +- helpers/webhooks/getWebhookWithToken.ts | 3 +- helpers/webhooks/getWebhooks.ts | 15 ++-- helpers/webhooks/mod.ts | 2 +- .../{sendWebhook.ts => sendWebhookMessage.ts} | 8 +- tests/channels/messages/reactions.test.ts | 6 +- tests/utils.test.ts | 12 +-- tests/webhook/messages.test.ts | 2 +- transformers/threadMember.ts | 5 +- util/utils.ts | 2 +- 176 files changed, 934 insertions(+), 611 deletions(-) create mode 100644 helpers/discovery/deleteDiscoverySubcategory.ts rename helpers/discovery/{validDiscoveryTerm.ts => getIsValidDiscoveryTerm.ts} (77%) delete mode 100644 helpers/discovery/removeDiscoverySubcategory.ts rename helpers/emojis/{emojiUrl.ts => getEmojiUrl.ts} (68%) rename helpers/guilds/{guildBannerUrl.ts => getGuildBannerUrl.ts} (81%) rename helpers/guilds/{guildIconUrl.ts => getGuildIconUrl.ts} (79%) rename helpers/guilds/{guildSplashUrl.ts => getGuildSplashUrl.ts} (80%) rename helpers/members/{avatarUrl.ts => getAvatarUrl.ts} (95%) create mode 100644 helpers/messages/deleteAllReactions.ts rename helpers/messages/{removeReaction.ts => deleteReaction.ts} (82%) rename helpers/messages/{removeReactionEmoji.ts => deleteReactionEmoji.ts} (74%) delete mode 100644 helpers/messages/removeAllReactions.ts create mode 100644 helpers/misc/getNitroStickerPacks.ts delete mode 100644 helpers/misc/nitroStickerPacks.ts rename helpers/templates/{getTemplate.ts => getGuildTemplate.ts} (69%) rename helpers/webhooks/{sendWebhook.ts => sendWebhookMessage.ts} (92%) diff --git a/helpers/channels/createChannel.ts b/helpers/channels/createChannel.ts index 7d9473fb9..2868adc8d 100644 --- a/helpers/channels/createChannel.ts +++ b/helpers/channels/createChannel.ts @@ -1,10 +1,16 @@ import type { Bot } from "../../bot.ts"; -import { ChannelTypes } from "../../types/shared.ts"; +import { Channel } from "../../transformers/channel.ts"; import { DiscordChannel } from "../../types/discord.ts"; +import { ChannelTypes } from "../../types/shared.ts"; import { OverwriteReadable } from "./editChannelOverwrite.ts"; /** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ -export async function createChannel(bot: Bot, guildId: bigint, options?: CreateGuildChannel, reason?: string) { +export async function createChannel( + bot: Bot, + guildId: bigint, + options?: CreateGuildChannel, + reason?: string, +): Promise { // BITRATE IS IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000 if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000; diff --git a/helpers/channels/createStageInstance.ts b/helpers/channels/createStageInstance.ts index 45a284461..98125a20b 100644 --- a/helpers/channels/createStageInstance.ts +++ b/helpers/channels/createStageInstance.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { StageInstance } from "../../transformers/stageInstance.ts"; import { DiscordStageInstance } from "../../types/discord.ts"; /** Creates a new Stage instance associated to a Stage channel. Requires the user to be a moderator of the Stage channel. */ -export async function createStageInstance(bot: Bot, options: CreateStageInstance) { +export async function createStageInstance(bot: Bot, options: CreateStageInstance): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", diff --git a/helpers/channels/deleteChannel.ts b/helpers/channels/deleteChannel.ts index e10bf6acc..335e6c426 100644 --- a/helpers/channels/deleteChannel.ts +++ b/helpers/channels/deleteChannel.ts @@ -1,9 +1,8 @@ import type { Bot } from "../../bot.ts"; -import { DiscordChannel } from "../../types/discord.ts"; /** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. Bot needs MANAGE_THREADS permissions in the server if deleting thread. */ -export async function deleteChannel(bot: Bot, channelId: bigint, reason?: string) { - await bot.rest.runMethod( +export async function deleteChannel(bot: Bot, channelId: bigint, reason?: string): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.CHANNEL(channelId), diff --git a/helpers/channels/deleteChannelOverwrite.ts b/helpers/channels/deleteChannelOverwrite.ts index 2d475b61f..5c5f59ba1 100644 --- a/helpers/channels/deleteChannelOverwrite.ts +++ b/helpers/channels/deleteChannelOverwrite.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../bot.ts"; /** Delete the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */ -export async function deleteChannelOverwrite(bot: Bot, channelId: bigint, overwriteId: bigint) { - await bot.rest.runMethod( +export async function deleteChannelOverwrite(bot: Bot, channelId: bigint, overwriteId: bigint): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.CHANNEL_OVERWRITE(channelId, overwriteId), diff --git a/helpers/channels/deleteStageInstance.ts b/helpers/channels/deleteStageInstance.ts index 8ba1e502a..39bc14fe6 100644 --- a/helpers/channels/deleteStageInstance.ts +++ b/helpers/channels/deleteStageInstance.ts @@ -1,6 +1,6 @@ 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.routes.STAGE_INSTANCE(channelId)); +export async function deleteStageInstance(bot: Bot, channelId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.STAGE_INSTANCE(channelId)); } diff --git a/helpers/channels/editChannel.ts b/helpers/channels/editChannel.ts index f70f780a6..de1afdae6 100644 --- a/helpers/channels/editChannel.ts +++ b/helpers/channels/editChannel.ts @@ -5,7 +5,12 @@ import { ChannelTypes, VideoQualityModes } from "../../types/shared.ts"; import { OverwriteReadable } from "./editChannelOverwrite.ts"; /** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ -export async function editChannel(bot: Bot, channelId: bigint, options: ModifyChannel, reason?: string) { +export async function editChannel( + bot: Bot, + channelId: bigint, + options: ModifyChannel, + reason?: string, +): Promise { if (options.name || options.topic) { const request = editChannelNameTopicQueue.get(channelId); if (!request) { @@ -81,7 +86,7 @@ interface EditChannelRequest { const editChannelNameTopicQueue = new Map(); let editChannelProcessing = false; -function processEditChannelQueue(bot: Bot) { +function processEditChannelQueue(bot: Bot): void { if (!editChannelProcessing) return; const now = Date.now(); diff --git a/helpers/channels/editChannelOverwrite.ts b/helpers/channels/editChannelOverwrite.ts index c5cd60190..341c07014 100644 --- a/helpers/channels/editChannelOverwrite.ts +++ b/helpers/channels/editChannelOverwrite.ts @@ -6,8 +6,8 @@ export async function editChannelOverwrite( bot: Bot, channelId: bigint, overwrite: OverwriteReadable, -) { - await bot.rest.runMethod( +): Promise { + return await bot.rest.runMethod( bot.rest, "PUT", bot.constants.routes.CHANNEL_OVERWRITE(channelId, overwrite.id), diff --git a/helpers/channels/followChannel.ts b/helpers/channels/followChannel.ts index d469a89f7..d4f21ab3c 100644 --- a/helpers/channels/followChannel.ts +++ b/helpers/channels/followChannel.ts @@ -2,8 +2,8 @@ import type { Bot } from "../../bot.ts"; import { DiscordFollowedChannel } from "../../types/discord.ts"; /** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */ -export async function followChannel(bot: Bot, sourceChannelId: bigint, targetChannelId: bigint) { - const data = await bot.rest.runMethod( +export async function followChannel(bot: Bot, sourceChannelId: bigint, targetChannelId: bigint): Promise { + const result = await bot.rest.runMethod( bot.rest, "POST", bot.constants.routes.CHANNEL_FOLLOW(sourceChannelId), @@ -12,5 +12,5 @@ export async function followChannel(bot: Bot, sourceChannelId: bigint, targetCha }, ); - return bot.transformers.snowflake(data.webhook_id); + return bot.transformers.snowflake(result.webhook_id); } diff --git a/helpers/channels/forums/createForumPost.ts b/helpers/channels/forums/createForumPost.ts index 0bafd8110..4d1710aad 100644 --- a/helpers/channels/forums/createForumPost.ts +++ b/helpers/channels/forums/createForumPost.ts @@ -1,14 +1,15 @@ import type { Bot } from "../../../bot.ts"; +import { Channel } from "../../../transformers/channel.ts"; +import { Embed } from "../../../transformers/embed.ts"; import { DiscordChannel } from "../../../types/discord.ts"; import { AllowedMentions, FileContent, MessageComponents } from "../../../types/mod.ts"; -import { Embed } from "../../../transformers/embed.ts"; /** Creates a new public thread from an existing message. Returns a thread channel. */ export async function createForumPost( bot: Bot, channelId: bigint, options: CreateForumPostWithMessage, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", diff --git a/helpers/channels/getChannel.ts b/helpers/channels/getChannel.ts index 89ea49000..c97f4490d 100644 --- a/helpers/channels/getChannel.ts +++ b/helpers/channels/getChannel.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { Channel } from "../../transformers/channel.ts"; import { DiscordChannel } from "../../types/discord.ts"; /** Fetches a single channel object from the api. */ -export async function getChannel(bot: Bot, channelId: bigint) { +export async function getChannel(bot: Bot, channelId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/channels/getChannelWebhooks.ts b/helpers/channels/getChannelWebhooks.ts index 961c40f8a..6fad113a7 100644 --- a/helpers/channels/getChannelWebhooks.ts +++ b/helpers/channels/getChannelWebhooks.ts @@ -1,17 +1,20 @@ -import { Collection } from "../../util/collection.ts"; import type { Bot } from "../../bot.ts"; +import { Webhook } from "../../transformers/webhook.ts"; import { DiscordWebhook } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */ -export async function getChannelWebhooks(bot: Bot, channelId: bigint) { - const result = await bot.rest.runMethod( +export async function getChannelWebhooks(bot: Bot, channelId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.CHANNEL_WEBHOOKS(channelId), ); - return new Collection(result.map((hook) => { - const webhook = bot.transformers.webhook(bot, hook); - return [webhook.id, webhook]; - })); + return new Collection( + results.map((result) => { + const webhook = bot.transformers.webhook(bot, result); + return [webhook.id, webhook]; + }), + ); } diff --git a/helpers/channels/getChannels.ts b/helpers/channels/getChannels.ts index c75419631..eec306919 100644 --- a/helpers/channels/getChannels.ts +++ b/helpers/channels/getChannels.ts @@ -1,16 +1,20 @@ -import { Collection } from "../../util/collection.ts"; import type { Bot } from "../../bot.ts"; +import { Channel } from "../../transformers/channel.ts"; import { DiscordChannel } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Returns a list of guild channel objects. */ -export async function getChannels(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getChannels(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_CHANNELS(guildId), ); return new Collection( - result.map((res) => bot.transformers.channel(bot, { channel: res, guildId })).map((c) => [c.id, c]), + results.map((result) => { + const channel = bot.transformers.channel(bot, { channel: result, guildId }); + return [channel.id, channel]; + }), ); } diff --git a/helpers/channels/getPins.ts b/helpers/channels/getPins.ts index 3da7a30cb..fc93a9aad 100644 --- a/helpers/channels/getPins.ts +++ b/helpers/channels/getPins.ts @@ -1,13 +1,20 @@ import type { Bot } from "../../bot.ts"; +import { Message } from "../../transformers/message.ts"; import { DiscordMessage } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Get pinned messages in this channel. */ -export async function getPins(bot: Bot, channelId: bigint) { - const result = await bot.rest.runMethod( +export async function getPins(bot: Bot, channelId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.CHANNEL_PINS(channelId), ); - return result.map((msg) => bot.transformers.message(bot, msg)); + return new Collection( + results.map((result) => { + const message = bot.transformers.message(bot, result); + return [message.id, message]; + }), + ); } diff --git a/helpers/channels/getStageInstance.ts b/helpers/channels/getStageInstance.ts index d2f6ae5af..e96f10444 100644 --- a/helpers/channels/getStageInstance.ts +++ b/helpers/channels/getStageInstance.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { StageInstance } from "../../transformers/stageInstance.ts"; import { DiscordStageInstance } from "../../types/discord.ts"; /** Gets the stage instance associated with the Stage channel, if it exists. */ -export async function getStageInstance(bot: Bot, channelId: bigint) { +export async function getStageInstance(bot: Bot, channelId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/channels/startTyping.ts b/helpers/channels/startTyping.ts index 75bc313e1..963de045b 100644 --- a/helpers/channels/startTyping.ts +++ b/helpers/channels/startTyping.ts @@ -5,6 +5,6 @@ import type { Bot } from "../../bot.ts"; * However, if a bot is responding to a command and expects the computation to take a few seconds, * 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(bot.rest, "POST", bot.constants.routes.CHANNEL_TYPING(channelId)); +export async function startTyping(bot: Bot, channelId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.CHANNEL_TYPING(channelId)); } diff --git a/helpers/channels/swapChannels.ts b/helpers/channels/swapChannels.ts index a9feef7d8..606986f8a 100644 --- a/helpers/channels/swapChannels.ts +++ b/helpers/channels/swapChannels.ts @@ -1,23 +1,25 @@ import type { Bot } from "../../bot.ts"; /** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permission. Only channels to be modified are required. */ -export async function swapChannels(bot: Bot, guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) { +export async function swapChannels( + bot: Bot, + guildId: bigint, + channelPositions: ModifyGuildChannelPositions[], +): Promise { if (!channelPositions.length) { - throw "You must provide at least one channels to be moved."; + throw new Error("You must provide at least one channels to be moved."); } - await bot.rest.runMethod( + return await bot.rest.runMethod( bot.rest, "PATCH", bot.constants.routes.GUILD_CHANNELS(guildId), - channelPositions.map((channelPosition) => { - return { - id: channelPosition.id, - position: channelPosition.position, - lock_positions: channelPosition.lockPositions, - parent_id: channelPosition.parentId, - }; - }), + channelPositions.map((channelPosition) => ({ + id: channelPosition.id, + position: channelPosition.position, + lock_positions: channelPosition.lockPositions, + parent_id: channelPosition.parentId, + })), ); } diff --git a/helpers/channels/threads/addToThread.ts b/helpers/channels/threads/addToThread.ts index 068159fdd..1f593373d 100644 --- a/helpers/channels/threads/addToThread.ts +++ b/helpers/channels/threads/addToThread.ts @@ -1,6 +1,6 @@ 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(bot.rest, "PUT", bot.constants.routes.THREAD_USER(threadId, userId)); +export async function addToThread(bot: Bot, threadId: bigint, userId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "PUT", bot.constants.routes.THREAD_USER(threadId, userId)); } diff --git a/helpers/channels/threads/getActiveThreads.ts b/helpers/channels/threads/getActiveThreads.ts index aa4a9dd20..f20379076 100644 --- a/helpers/channels/threads/getActiveThreads.ts +++ b/helpers/channels/threads/getActiveThreads.ts @@ -1,10 +1,16 @@ import type { Bot } from "../../../bot.ts"; +import { Channel, ThreadMember } from "../../../mod.ts"; import { DiscordListActiveThreads } from "../../../types/discord.ts"; import { Collection } from "../../../util/collection.ts"; +export type ActiveThreads = { + threads: Collection; + members: Collection; +}; + /** Returns all active threads in the guild, including public and private threads. Threads are ordered by their `id`, in descending order. */ -export async function getActiveThreads(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getActiveThreads(bot: Bot, guildId: bigint): Promise { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.THREAD_ACTIVE(guildId), @@ -12,15 +18,15 @@ export async function getActiveThreads(bot: Bot, guildId: bigint) { return { threads: new Collection( - result.threads.map((t) => { - const thread = bot.transformers.channel(bot, { channel: t }); + results.threads.map((result) => { + const thread = bot.transformers.channel(bot, { channel: result }); return [thread.id, thread]; }), ), members: new Collection( - result.members.map((m) => { - const member = bot.transformers.threadMember(bot, m); - return [member.id, member]; + results.members.map((result) => { + const member = bot.transformers.threadMember(bot, result); + return [member.id!, member]; }), ), }; diff --git a/helpers/channels/threads/getArchivedThreads.ts b/helpers/channels/threads/getArchivedThreads.ts index 1c542a71b..d62cf24f4 100644 --- a/helpers/channels/threads/getArchivedThreads.ts +++ b/helpers/channels/threads/getArchivedThreads.ts @@ -1,6 +1,11 @@ -import { Collection } from "../../../util/collection.ts"; import type { Bot } from "../../../bot.ts"; import { DiscordListArchivedThreads } from "../../../types/discord.ts"; +import { Collection } from "../../../util/collection.ts"; +import { ActiveThreads } from "./getActiveThreads.ts"; + +export type ArchivedThreads = ActiveThreads & { + hasMore: boolean; +}; /** Get the archived threads for this channel, defaults to public */ export async function getArchivedThreads( @@ -9,33 +14,33 @@ export async function getArchivedThreads( options?: ListArchivedThreads & { type?: "public" | "private" | "privateJoinedThreads"; }, -) { - let url = options?.type === "privateJoinedThreads" +): Promise { + const url = options?.type === "privateJoinedThreads" ? bot.constants.routes.THREAD_ARCHIVED_PRIVATE_JOINED(channelId, options) : options?.type === "private" ? bot.constants.routes.THREAD_ARCHIVED_PRIVATE(channelId, options) : bot.constants.routes.THREAD_ARCHIVED_PUBLIC(channelId, options); - const result = (await bot.rest.runMethod( + const results = await bot.rest.runMethod( bot.rest, "GET", url, - )); + ); return { threads: new Collection( - result.threads.map((t) => { - const thread = bot.transformers.channel(bot, { channel: t }); + results.threads.map((result) => { + const thread = bot.transformers.channel(bot, { channel: result }); return [thread.id, thread]; }), ), members: new Collection( - result.members.map((m) => { - const member = bot.transformers.threadMember(bot, m); - return [member.id, member]; + results.members.map((result) => { + const member = bot.transformers.threadMember(bot, result); + return [member.id!, member]; }), ), - hasMore: result.has_more, + hasMore: results.has_more, }; } diff --git a/helpers/channels/threads/getThreadMember.ts b/helpers/channels/threads/getThreadMember.ts index 091b8db9d..8c41eca7e 100644 --- a/helpers/channels/threads/getThreadMember.ts +++ b/helpers/channels/threads/getThreadMember.ts @@ -1,18 +1,14 @@ import type { Bot } from "../../../bot.ts"; +import { ThreadMember } from "../../../transformers/threadMember.ts"; import { DiscordThreadMember } from "../../../types/discord.ts"; /** Returns thread members objects that are members of the thread. */ -export async function getThreadMember(bot: Bot, threadId: bigint, userId: bigint) { +export async function getThreadMember(bot: Bot, threadId: bigint, userId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.THREAD_USER(threadId, userId), ); - return { - id: result.id ? bot.transformers.snowflake(result.id) : undefined, - userId: result.user_id ? bot.transformers.snowflake(result.user_id) : undefined, - joinTimestamp: Date.parse(result.join_timestamp), - flags: result.flags, - }; + return bot.transformers.threadMember(bot, result); } diff --git a/helpers/channels/threads/getThreadMembers.ts b/helpers/channels/threads/getThreadMembers.ts index efd84fbb8..2f07993cc 100644 --- a/helpers/channels/threads/getThreadMembers.ts +++ b/helpers/channels/threads/getThreadMembers.ts @@ -1,18 +1,20 @@ import type { Bot } from "../../../bot.ts"; +import { ThreadMember } from "../../../transformers/threadMember.ts"; import { DiscordThreadMember } from "../../../types/discord.ts"; import { Collection } from "../../../util/collection.ts"; /** Returns thread members objects that are members of the thread. */ -export async function getThreadMembers(bot: Bot, threadId: bigint) { - const result = await bot.rest.runMethod( +export async function getThreadMembers(bot: Bot, threadId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.THREAD_MEMBERS(threadId), ); - // return result; - return new Collection(result.map((res) => { - const member = bot.transformers.threadMember(bot, res); - return [member.id, member]; - })); + return new Collection( + results.map((result) => { + const member = bot.transformers.threadMember(bot, result); + return [member.id!, member]; + }), + ); } diff --git a/helpers/channels/threads/joinThread.ts b/helpers/channels/threads/joinThread.ts index 785444196..3b4053d9c 100644 --- a/helpers/channels/threads/joinThread.ts +++ b/helpers/channels/threads/joinThread.ts @@ -1,6 +1,6 @@ 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(bot.rest, "PUT", bot.constants.routes.THREAD_ME(threadId)); +export async function joinThread(bot: Bot, threadId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "PUT", bot.constants.routes.THREAD_ME(threadId)); } diff --git a/helpers/channels/threads/leaveThread.ts b/helpers/channels/threads/leaveThread.ts index f4ed15f6a..4c506618e 100644 --- a/helpers/channels/threads/leaveThread.ts +++ b/helpers/channels/threads/leaveThread.ts @@ -1,6 +1,6 @@ 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(bot.rest, "DELETE", bot.constants.routes.THREAD_ME(threadId)); +export async function leaveThread(bot: Bot, threadId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.THREAD_ME(threadId)); } diff --git a/helpers/channels/threads/removeThreadMember.ts b/helpers/channels/threads/removeThreadMember.ts index 7359a9cb9..0afa3e894 100644 --- a/helpers/channels/threads/removeThreadMember.ts +++ b/helpers/channels/threads/removeThreadMember.ts @@ -1,6 +1,10 @@ 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(bot.rest, "DELETE", bot.constants.routes.THREAD_USER(threadId, userId)); +export async function removeThreadMember(bot: Bot, threadId: bigint, userId: bigint): Promise { + return await bot.rest.runMethod( + bot.rest, + "DELETE", + bot.constants.routes.THREAD_USER(threadId, userId), + ); } diff --git a/helpers/channels/threads/startThreadWithMessage.ts b/helpers/channels/threads/startThreadWithMessage.ts index 7dcaf52e2..1b29b921e 100644 --- a/helpers/channels/threads/startThreadWithMessage.ts +++ b/helpers/channels/threads/startThreadWithMessage.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../../bot.ts"; +import { Channel } from "../../../transformers/channel.ts"; import { DiscordChannel } from "../../../types/discord.ts"; /** Creates a new public thread from an existing message. Returns a thread channel. */ @@ -7,7 +8,7 @@ export async function startThreadWithMessage( channelId: bigint, messageId: bigint, options: StartThreadWithMessage, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", diff --git a/helpers/channels/threads/startThreadWithoutMessage.ts b/helpers/channels/threads/startThreadWithoutMessage.ts index 4b54a6888..a404e8b12 100644 --- a/helpers/channels/threads/startThreadWithoutMessage.ts +++ b/helpers/channels/threads/startThreadWithoutMessage.ts @@ -1,9 +1,14 @@ import type { Bot } from "../../../bot.ts"; +import { Channel } from "../../../transformers/channel.ts"; import { DiscordChannel } from "../../../types/discord.ts"; import { ChannelTypes } from "../../../types/shared.ts"; /** Creates a new private thread. Returns a thread channel. */ -export async function startThreadWithoutMessage(bot: Bot, channelId: bigint, options: StartThreadWithoutMessage) { +export async function startThreadWithoutMessage( + bot: Bot, + channelId: bigint, + options: StartThreadWithoutMessage, +): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", diff --git a/helpers/channels/updateStageInstance.ts b/helpers/channels/updateStageInstance.ts index e283ef192..9621ed2fd 100644 --- a/helpers/channels/updateStageInstance.ts +++ b/helpers/channels/updateStageInstance.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../bot.ts"; +import { StageInstance } from "../../transformers/stageInstance.ts"; import { DiscordStageInstance } from "../../types/discord.ts"; import { AtLeastOne } from "../../types/shared.ts"; @@ -7,7 +8,7 @@ export async function updateStageInstance( bot: Bot, channelId: bigint, data: AtLeastOne>, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/channels/updateVoiceState.ts b/helpers/channels/updateVoiceState.ts index 01f952473..261f29263 100644 --- a/helpers/channels/updateVoiceState.ts +++ b/helpers/channels/updateVoiceState.ts @@ -10,8 +10,8 @@ import type { Bot } from "../../bot.ts"; * - You are able to set `request_to_speak_timestamp` to any present or future time. * - 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.routes.UPDATE_VOICE_STATE(guildId), { +export async function updateBotVoiceState(bot: Bot, guildId: bigint, options: UpdateSelfVoiceState): Promise { + return 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 @@ -31,8 +31,8 @@ export async function updateBotVoiceState(bot: Bot, guildId: bigint, options: Up * - You are able to set `request_to_speak_timestamp` to any present or future time. * - When suppressed, the user will have their `request_to_speak_timestamp` removed. */ -export async function updateUserVoiceState(bot: Bot, guildId: bigint, options: UpdateOthersVoiceState) { - await bot.rest.runMethod( +export async function updateUserVoiceState(bot: Bot, guildId: bigint, options: UpdateOthersVoiceState): Promise { + return await bot.rest.runMethod( bot.rest, "PATCH", bot.constants.routes.UPDATE_VOICE_STATE(guildId, options.userId), diff --git a/helpers/discovery/addDiscoverySubcategory.ts b/helpers/discovery/addDiscoverySubcategory.ts index 858ac2cab..bfbcbfa6d 100644 --- a/helpers/discovery/addDiscoverySubcategory.ts +++ b/helpers/discovery/addDiscoverySubcategory.ts @@ -1,9 +1,8 @@ import type { Bot } from "../../bot.ts"; -import { DiscordAddGuildDiscoverySubcategory } from "../../types/discord.ts"; /** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */ -export async function addDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number) { - await bot.rest.runMethod( +export async function addDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number): Promise { + return await bot.rest.runMethod( bot.rest, "POST", bot.constants.routes.DISCOVERY_SUBCATEGORY(guildId, categoryId), diff --git a/helpers/discovery/deleteDiscoverySubcategory.ts b/helpers/discovery/deleteDiscoverySubcategory.ts new file mode 100644 index 000000000..cfa6c6153 --- /dev/null +++ b/helpers/discovery/deleteDiscoverySubcategory.ts @@ -0,0 +1,10 @@ +import type { Bot } from "../../bot.ts"; + +/** Deletes a discovery subcategory from the guild. Requires the MANAGE_GUILD permission. Returns a 204 No Content on success. */ +export async function deleteDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number): Promise { + return await bot.rest.runMethod( + bot.rest, + "DELETE", + bot.constants.routes.DISCOVERY_SUBCATEGORY(guildId, categoryId), + ); +} diff --git a/helpers/discovery/editDiscovery.ts b/helpers/discovery/editDiscovery.ts index 65d679546..8adefcea7 100644 --- a/helpers/discovery/editDiscovery.ts +++ b/helpers/discovery/editDiscovery.ts @@ -1,8 +1,13 @@ import type { Bot } from "../../bot.ts"; import { DiscordDiscoveryMetadata } from "../../types/discord.ts"; +import { DiscoveryMetadata } from "./getDiscovery.ts"; /** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */ -export async function editDiscovery(bot: Bot, guildId: bigint, data: ModifyGuildDiscoveryMetadata) { +export async function editDiscovery( + bot: Bot, + guildId: bigint, + data: ModifyGuildDiscoveryMetadata, +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", @@ -16,7 +21,7 @@ export async function editDiscovery(bot: Bot, guildId: bigint, data: ModifyGuild return { guildId, - primaryCategoryId: result.primary_category_id, + primaryCategoryId: BigInt(result.primary_category_id), keywords: result.keywords ?? undefined, emojiDiscoverabilityEnabled: result.emoji_discoverability_enabled, partnerActionedTimestamp: result.partner_actioned_timestamp diff --git a/helpers/discovery/getDiscovery.ts b/helpers/discovery/getDiscovery.ts index 38d2c8fa1..868aa5017 100644 --- a/helpers/discovery/getDiscovery.ts +++ b/helpers/discovery/getDiscovery.ts @@ -1,8 +1,18 @@ import type { Bot } from "../../bot.ts"; import { DiscordDiscoveryMetadata } from "../../types/discord.ts"; +export type DiscoveryMetadata = { + guildId: bigint; + primaryCategoryId: number; + keywords?: string[]; + emojiDiscoverabilityEnabled: boolean; + partnerActionedTimestamp?: number; + partnerApplicationTimestamp?: number; + categoryIds: number[]; +}; + /** Returns the discovery metadata object for the guild. Requires the `MANAGE_GUILD` permission. */ -export async function getDiscovery(bot: Bot, guildId: bigint) { +export async function getDiscovery(bot: Bot, guildId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/discovery/getDiscoveryCategories.ts b/helpers/discovery/getDiscoveryCategories.ts index 8bc2ac5e3..3f359edf7 100644 --- a/helpers/discovery/getDiscoveryCategories.ts +++ b/helpers/discovery/getDiscoveryCategories.ts @@ -1,16 +1,30 @@ -import { Collection } from "../../util/collection.ts"; import type { Bot } from "../../bot.ts"; import { DiscordDiscoveryCategory } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; + +export type DiscoveryCategory = { + id: bigint; + name: DiscoveryName; + isPrimary: boolean; +}; + +export type DiscoveryName = { + default: string; + localizations?: Record; +}; /** Returns a Collection (mapped by Id of the discovery category object) of discovery category objects that can be used when editing guilds */ -export async function getDiscoveryCategories(bot: Bot) { - const result = await bot.rest.runMethod( +export async function getDiscoveryCategories(bot: Bot): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.DISCOVERY_CATEGORIES(), ); - return new Collection( - result.map((category) => [category.id, category]), + return new Collection( + results.map((result) => { + const category = { id: BigInt(result.id), name: result.name, isPrimary: result.is_primary }; + return [category.id, category]; + }), ); } diff --git a/helpers/discovery/validDiscoveryTerm.ts b/helpers/discovery/getIsValidDiscoveryTerm.ts similarity index 77% rename from helpers/discovery/validDiscoveryTerm.ts rename to helpers/discovery/getIsValidDiscoveryTerm.ts index f34833809..260107295 100644 --- a/helpers/discovery/validDiscoveryTerm.ts +++ b/helpers/discovery/getIsValidDiscoveryTerm.ts @@ -1,7 +1,7 @@ import type { Bot } from "../../bot.ts"; import { DiscordValidateDiscoverySearchTerm } from "../../types/discord.ts"; -export async function validDiscoveryTerm(bot: Bot, term: string) { +export async function getIsValidDiscoveryTerm(bot: Bot, term: string): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/discovery/mod.ts b/helpers/discovery/mod.ts index bca038f39..af88f4fb6 100644 --- a/helpers/discovery/mod.ts +++ b/helpers/discovery/mod.ts @@ -1,6 +1,6 @@ export * from "./addDiscoverySubcategory.ts"; +export * from "./deleteDiscoverySubcategory.ts"; export * from "./editDiscovery.ts"; export * from "./getDiscovery.ts"; export * from "./getDiscoveryCategories.ts"; -export * from "./removeDiscoverySubcategory.ts"; -export * from "./validDiscoveryTerm.ts"; +export * from "./getIsValidDiscoveryTerm.ts"; diff --git a/helpers/discovery/removeDiscoverySubcategory.ts b/helpers/discovery/removeDiscoverySubcategory.ts deleted file mode 100644 index 6837ed79e..000000000 --- a/helpers/discovery/removeDiscoverySubcategory.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { Bot } from "../../bot.ts"; - -/** Removes a discovery subcategory from the guild. Requires the MANAGE_GUILD permission. Returns a 204 No Content on success. */ -export async function removeDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number) { - await bot.rest.runMethod( - bot.rest, - "DELETE", - bot.constants.routes.DISCOVERY_SUBCATEGORY(guildId, categoryId), - ); -} diff --git a/helpers/emojis/createEmoji.ts b/helpers/emojis/createEmoji.ts index a94d56d5f..46ab09787 100644 --- a/helpers/emojis/createEmoji.ts +++ b/helpers/emojis/createEmoji.ts @@ -1,13 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { Emoji } from "../../transformers/emoji.ts"; import { DiscordEmoji } from "../../types/discord.ts"; /** Create an emoji in the server */ -export async function createEmoji(bot: Bot, guildId: bigint, options: CreateGuildEmoji) { +export async function createEmoji(bot: Bot, guildId: bigint, options: CreateGuildEmoji): Promise { if (options.image && !options.image.startsWith("data:image/")) { options.image = await bot.utils.urlToBase64(options.image); } - const emoji = await bot.rest.runMethod( + const result = await bot.rest.runMethod( bot.rest, "POST", bot.constants.routes.GUILD_EMOJIS(guildId), @@ -19,7 +20,7 @@ export async function createEmoji(bot: Bot, guildId: bigint, options: CreateGuil }, ); - return bot.transformers.emoji(bot, emoji); + return bot.transformers.emoji(bot, result); } /** https://discord.com/developers/docs/resources/emoji#create-guild-emoji */ diff --git a/helpers/emojis/deleteEmoji.ts b/helpers/emojis/deleteEmoji.ts index c03982adc..617952bcf 100644 --- a/helpers/emojis/deleteEmoji.ts +++ b/helpers/emojis/deleteEmoji.ts @@ -1,8 +1,8 @@ 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(bot.rest, "DELETE", bot.constants.routes.GUILD_EMOJI(guildId, id), { +export async function deleteEmoji(bot: Bot, guildId: bigint, id: bigint, reason?: string): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_EMOJI(guildId, id), { reason, }); } diff --git a/helpers/emojis/editEmoji.ts b/helpers/emojis/editEmoji.ts index 6bee65478..7fe5b5d7f 100644 --- a/helpers/emojis/editEmoji.ts +++ b/helpers/emojis/editEmoji.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { Emoji } from "../../transformers/emoji.ts"; import { DiscordEmoji } from "../../types/discord.ts"; /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */ -export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options: ModifyGuildEmoji) { +export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options: ModifyGuildEmoji): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/emojis/getEmoji.ts b/helpers/emojis/getEmoji.ts index f96c1cecc..d40fbc705 100644 --- a/helpers/emojis/getEmoji.ts +++ b/helpers/emojis/getEmoji.ts @@ -1,10 +1,11 @@ import type { Bot } from "../../bot.ts"; +import { Emoji } from "../../transformers/emoji.ts"; import { DiscordEmoji } from "../../types/discord.ts"; /** * Returns an emoji for the given guild and emoji Id. */ -export async function getEmoji(bot: Bot, guildId: bigint, emojiId: bigint) { +export async function getEmoji(bot: Bot, guildId: bigint, emojiId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/emojis/emojiUrl.ts b/helpers/emojis/getEmojiUrl.ts similarity index 68% rename from helpers/emojis/emojiUrl.ts rename to helpers/emojis/getEmojiUrl.ts index aa52f58c6..5c40f6c45 100644 --- a/helpers/emojis/emojiUrl.ts +++ b/helpers/emojis/getEmojiUrl.ts @@ -1,6 +1,6 @@ import { Bot } from "../../bot.ts"; /** Creates a url to the emoji from the Discord CDN. */ -export function emojiUrl(bot: Bot, id: bigint, animated = false) { +export function getEmojiURL(_bot: Bot, id: bigint, animated = false): string { return `https://cdn.discordapp.com/emojis/${id}.${animated ? "gif" : "png"}`; } diff --git a/helpers/emojis/getEmojis.ts b/helpers/emojis/getEmojis.ts index c51529cc1..518072c42 100644 --- a/helpers/emojis/getEmojis.ts +++ b/helpers/emojis/getEmojis.ts @@ -1,16 +1,22 @@ import type { Bot } from "../../bot.ts"; +import { Emoji } from "../../transformers/emoji.ts"; import { DiscordEmoji } from "../../types/discord.ts"; import { Collection } from "../../util/collection.ts"; /** * Returns a list of emojis for the given guild. */ -export async function getEmojis(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getEmojis(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_EMOJIS(guildId), ); - return new Collection(result.map((e) => [bot.transformers.snowflake(e.id!), bot.transformers.emoji(bot, e)])); + return new Collection( + results.map((result) => { + const emoji = bot.transformers.emoji(bot, result); + return [emoji.id!, emoji]; + }), + ); } diff --git a/helpers/emojis/mod.ts b/helpers/emojis/mod.ts index 1fc22cf05..215808ad7 100644 --- a/helpers/emojis/mod.ts +++ b/helpers/emojis/mod.ts @@ -1,6 +1,7 @@ export * from "./createEmoji.ts"; export * from "./deleteEmoji.ts"; export * from "./editEmoji.ts"; -export * from "./emojiUrl.ts"; export * from "./getEmoji.ts"; export * from "./getEmojis.ts"; +export * from "./getEmojiUrl.ts"; + diff --git a/helpers/guilds/automod/createAutomodRule.ts b/helpers/guilds/automod/createAutomodRule.ts index 5bd2b7d09..ce2804a6c 100644 --- a/helpers/guilds/automod/createAutomodRule.ts +++ b/helpers/guilds/automod/createAutomodRule.ts @@ -1,4 +1,5 @@ import { Bot } from "../../../bot.ts"; +import { AutoModerationRule } from "../../../transformers/automodRule.ts"; import { AutoModerationActionType, AutoModerationEventTypes, @@ -8,8 +9,12 @@ import { } from "../../../types/discord.ts"; /** Get a rule currently configured for guild. */ -export async function createAutomodRule(bot: Bot, guildId: bigint, options: CreateAutoModerationRuleOptions) { - const rule = await bot.rest.runMethod( +export async function createAutomodRule( + bot: Bot, + guildId: bigint, + options: CreateAutoModerationRuleOptions, +): Promise { + const result = await bot.rest.runMethod( bot.rest, "POST", bot.constants.routes.AUTOMOD_RULES(guildId), @@ -38,7 +43,7 @@ export async function createAutomodRule(bot: Bot, guildId: bigint, options: Crea }, ); - return bot.transformers.automodRule(bot, rule); + return bot.transformers.automodRule(bot, result); } export interface CreateAutoModerationRuleOptions { diff --git a/helpers/guilds/automod/deleteAutomodRule.ts b/helpers/guilds/automod/deleteAutomodRule.ts index 6eb676eef..fbd2db336 100644 --- a/helpers/guilds/automod/deleteAutomodRule.ts +++ b/helpers/guilds/automod/deleteAutomodRule.ts @@ -1,8 +1,8 @@ import { Bot } from "../../../bot.ts"; /** Delete a rule currently configured for guild. */ -export async function deleteAutomodRule(bot: Bot, guildId: bigint, ruleId: bigint, reason?: string) { - await bot.rest.runMethod( +export async function deleteAutomodRule(bot: Bot, guildId: bigint, ruleId: bigint, reason?: string): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.AUTOMOD_RULE(guildId, ruleId), diff --git a/helpers/guilds/automod/editAutomodRule.ts b/helpers/guilds/automod/editAutomodRule.ts index c94634579..928186a19 100644 --- a/helpers/guilds/automod/editAutomodRule.ts +++ b/helpers/guilds/automod/editAutomodRule.ts @@ -1,4 +1,5 @@ import { Bot } from "../../../bot.ts"; +import { AutoModerationRule } from "../../../transformers/automodRule.ts"; import { AutoModerationActionType, AutoModerationEventTypes, @@ -7,8 +8,12 @@ import { } from "../../../types/discord.ts"; /** Edit a rule currently configured for guild. */ -export async function editAutomodRule(bot: Bot, guildId: bigint, options: Partial) { - const rule = await bot.rest.runMethod( +export async function editAutomodRule( + bot: Bot, + guildId: bigint, + options: Partial, +): Promise { + const result = await bot.rest.runMethod( bot.rest, "PATCH", bot.constants.routes.AUTOMOD_RULES(guildId), @@ -36,7 +41,7 @@ export async function editAutomodRule(bot: Bot, guildId: bigint, options: Partia }, ); - return bot.transformers.automodRule(bot, rule); + return bot.transformers.automodRule(bot, result); } export interface EditAutoModerationRuleOptions { diff --git a/helpers/guilds/automod/getAutomodRule.ts b/helpers/guilds/automod/getAutomodRule.ts index 51aecf9a6..e1f2e9749 100644 --- a/helpers/guilds/automod/getAutomodRule.ts +++ b/helpers/guilds/automod/getAutomodRule.ts @@ -1,14 +1,14 @@ import { Bot } from "../../../bot.ts"; +import { AutoModerationRule } from "../../../transformers/automodRule.ts"; import { DiscordAutoModerationRule } from "../../../types/discord.ts"; -import { Collection } from "../../../util/collection.ts"; /** Get a rule currently configured for guild. */ -export async function getAutomodRule(bot: Bot, guildId: bigint, ruleId: bigint) { - const rule = await bot.rest.runMethod( +export async function getAutomodRule(bot: Bot, guildId: bigint, ruleId: bigint): Promise { + const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.AUTOMOD_RULE(guildId, ruleId), ); - return bot.transformers.automodRule(bot, rule); + return bot.transformers.automodRule(bot, result); } diff --git a/helpers/guilds/automod/getAutomodRules.ts b/helpers/guilds/automod/getAutomodRules.ts index 882d504e3..fcbbcf12f 100644 --- a/helpers/guilds/automod/getAutomodRules.ts +++ b/helpers/guilds/automod/getAutomodRules.ts @@ -1,17 +1,20 @@ import { Bot } from "../../../bot.ts"; +import { AutoModerationRule } from "../../../transformers/automodRule.ts"; import { DiscordAutoModerationRule } from "../../../types/discord.ts"; import { Collection } from "../../../util/collection.ts"; /** Get a list of all rules currently configured for guild. */ -export async function getAutomodRules(bot: Bot, guildId: bigint) { - const rules = await bot.rest.runMethod( +export async function getAutomodRules(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.AUTOMOD_RULES(guildId), ); - return new Collection(rules.map((r) => { - const rule = bot.transformers.automodRule(bot, r); - return [rule.id, rule]; - })); + return new Collection( + results.map((result) => { + const rule = bot.transformers.automodRule(bot, result); + return [rule.id, rule]; + }), + ); } diff --git a/helpers/guilds/automod/mod.ts b/helpers/guilds/automod/mod.ts index bbf671ea0..677e29c70 100644 --- a/helpers/guilds/automod/mod.ts +++ b/helpers/guilds/automod/mod.ts @@ -1,5 +1,5 @@ +export * from "./createAutomodRule.ts"; +export * from "./deleteAutomodRule.ts"; +export * from "./editAutomodRule.ts"; export * from "./getAutomodRule.ts"; export * from "./getAutomodRules.ts"; -export * from "./createAutomodRule.ts"; -export * from "./editAutomodRule.ts"; -export * from "./deleteAutomodRule.ts"; diff --git a/helpers/guilds/createGuild.ts b/helpers/guilds/createGuild.ts index 39084574c..cabf641f1 100644 --- a/helpers/guilds/createGuild.ts +++ b/helpers/guilds/createGuild.ts @@ -1,5 +1,6 @@ import type { Bot } from "../../bot.ts"; import { Channel } from "../../transformers/channel.ts"; +import { Guild } from "../../transformers/guild.ts"; import { Role } from "../../transformers/role.ts"; import { DiscordGuild } from "../../types/discord.ts"; import { @@ -10,7 +11,7 @@ import { } from "../../types/shared.ts"; /** 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) { +export async function createGuild(bot: Bot, options: CreateGuild): Promise { const result = await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.GUILDS(), { name: options.name, afk_channel_id: options.afkChannelId, diff --git a/helpers/guilds/deleteGuild.ts b/helpers/guilds/deleteGuild.ts index 98b083815..baad7d28d 100644 --- a/helpers/guilds/deleteGuild.ts +++ b/helpers/guilds/deleteGuild.ts @@ -1,6 +1,6 @@ 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(bot.rest, "DELETE", bot.constants.routes.GUILD(guildId)); +export async function deleteGuild(bot: Bot, guildId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD(guildId)); } diff --git a/helpers/guilds/editGuild.ts b/helpers/guilds/editGuild.ts index 8017b6c0e..a699170af 100644 --- a/helpers/guilds/editGuild.ts +++ b/helpers/guilds/editGuild.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../bot.ts"; +import { Guild } from "../../transformers/guild.ts"; import { DiscordGuild } from "../../types/discord.ts"; import { DefaultMessageNotificationLevels, @@ -9,7 +10,7 @@ import { } from "../../types/shared.ts"; /** Modify a guilds settings. Requires the MANAGE_GUILD permission. */ -export async function editGuild(bot: Bot, guildId: bigint, options: ModifyGuild, shardId: number) { +export async function editGuild(bot: Bot, guildId: bigint, options: ModifyGuild, shardId: number): Promise { if (options.icon && !options.icon.startsWith("data:image/")) { options.icon = await bot.utils.urlToBase64(options.icon); } @@ -50,10 +51,7 @@ export async function editGuild(bot: Bot, guildId: bigint, options: ModifyGuild, }, ); - return bot.transformers.guild(bot, { - guild: result, - shardId, - }); + return bot.transformers.guild(bot, { guild: result, shardId }); } /** https://discord.com/developers/docs/resources/guild#modify-guild */ diff --git a/helpers/guilds/editWelcomeScreen.ts b/helpers/guilds/editWelcomeScreen.ts index aabb30645..4d4e15bcb 100644 --- a/helpers/guilds/editWelcomeScreen.ts +++ b/helpers/guilds/editWelcomeScreen.ts @@ -1,7 +1,12 @@ import type { Bot } from "../../bot.ts"; +import { WelcomeScreen } from "../../transformers/welcomeScreen.ts"; import { DiscordWelcomeScreen } from "../../types/discord.ts"; -export async function editWelcomeScreen(bot: Bot, guildId: bigint, options: ModifyGuildWelcomeScreen) { +export async function editWelcomeScreen( + bot: Bot, + guildId: bigint, + options: ModifyGuildWelcomeScreen, +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/guilds/editWidget.ts b/helpers/guilds/editWidget.ts index dea1df89b..5f7725f46 100644 --- a/helpers/guilds/editWidget.ts +++ b/helpers/guilds/editWidget.ts @@ -1,8 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { GuildWidgetSettings } from "../../transformers/widgetSettings.ts"; import { DiscordGuildWidgetSettings } from "../../types/discord.ts"; /** Modify a guild widget object for the guild. Requires the MANAGE_GUILD permission. */ -export async function editWidget(bot: Bot, guildId: bigint, enabled: boolean, channelId?: string | null) { +export async function editWidget( + bot: Bot, + guildId: bigint, + enabled: boolean, + channelId?: string | null, +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/guilds/getAuditLogs.ts b/helpers/guilds/getAuditLogs.ts index 157b69271..b9a767706 100644 --- a/helpers/guilds/getAuditLogs.ts +++ b/helpers/guilds/getAuditLogs.ts @@ -1,22 +1,41 @@ import type { Bot } from "../../bot.ts"; +import { AuditLogEntry } from "../../transformers/auditLogEntry.ts"; +import { AutoModerationRule } from "../../transformers/automodRule.ts"; +import { Channel } from "../../transformers/channel.ts"; +import { Integration } from "../../transformers/integration.ts"; +import { User } from "../../transformers/member.ts"; +import { ScheduledEvent } from "../../transformers/scheduledEvent.ts"; +import { Webhook } from "../../transformers/webhook.ts"; import { DiscordAuditLog } from "../../types/discord.ts"; import { AuditLogEvents } from "../../types/shared.ts"; -/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */ -export async function getAuditLogs(bot: Bot, guildId: bigint, options?: GetGuildAuditLog) { - if (options?.limit) options.limit = options.limit >= 1 && options.limit <= 100 ? options.limit : 50; +export type AuditLog = { + auditLogEntries: AuditLogEntry[]; + autoModerationRules?: AutoModerationRule[]; + guildScheduledEvents?: ScheduledEvent[]; + integrations: Partial>[]; + threads: Channel[]; + users: User[]; + webhooks: Webhook[]; +}; - const auditlog = await bot.rest.runMethod( +/** Returns the audit logs for the guild. Requires VIEW_AUDIT_LOGS permission */ +export async function getAuditLogs(bot: Bot, guildId: bigint, options?: GetGuildAuditLog): Promise { + if (options?.limit) { + options.limit = options.limit >= 1 && options.limit <= 100 ? options.limit : 50; + } + + const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_AUDIT_LOGS(guildId, options), ); return { - users: auditlog.users.map((user) => bot.transformers.user(bot, user)), - webhook: auditlog.webhooks.map((hook) => bot.transformers.webhook(bot, hook)), - auditLogEntries: auditlog.audit_log_entries.map((entry) => bot.transformers.auditLogEntry(bot, entry)), - integrations: auditlog.integrations.map((integration) => ({ + auditLogEntries: result.audit_log_entries.map((entry) => bot.transformers.auditLogEntry(bot, entry)), + autoModerationRules: result.auto_moderation_rules?.map((rule) => bot.transformers.automodRule(bot, rule)), + guildScheduledEvents: result.guild_scheduled_events?.map((event) => bot.transformers.scheduledEvent(bot, event)), + integrations: result.integrations.map((integration) => ({ id: integration.id ? bot.transformers.snowflake(integration.id) : undefined, name: integration.name, type: integration.type, @@ -27,10 +46,12 @@ export async function getAuditLogs(bot: Bot, guildId: bigint, options?: GetGuild expireBehavior: integration.expire_behavior, expireGracePeriod: integration.expire_grace_period, user: integration.user ? bot.transformers.user(bot, integration.user) : undefined, - account: { - id: integration.account?.id ? bot.transformers.snowflake(integration.account.id) : undefined, - name: integration.account?.name, - }, + account: integration.account + ? { + id: bot.transformers.snowflake(integration.account.id), + name: integration.account.name, + } + : undefined, syncedAt: integration.synced_at ? Date.parse(integration.synced_at) : undefined, subscriberCount: integration.subscriber_count, revoked: integration.revoked, @@ -44,8 +65,9 @@ export async function getAuditLogs(bot: Bot, guildId: bigint, options?: GetGuild } : undefined, })), - threads: auditlog.threads.map((thread) => bot.transformers.channel(bot, { channel: thread, guildId })), - scheduledEvents: auditlog.guild_scheduled_events?.map((event) => bot.transformers.scheduledEvent(bot, event)), + threads: result.threads.map((thread) => bot.transformers.channel(bot, { channel: thread, guildId })), + users: result.users.map((user) => bot.transformers.user(bot, user)), + webhooks: result.webhooks.map((hook) => bot.transformers.webhook(bot, hook)), }; } diff --git a/helpers/guilds/getAvailableVoiceRegions.ts b/helpers/guilds/getAvailableVoiceRegions.ts index 91a6a1cf2..1605d7c79 100644 --- a/helpers/guilds/getAvailableVoiceRegions.ts +++ b/helpers/guilds/getAvailableVoiceRegions.ts @@ -1,19 +1,20 @@ import type { Bot } from "../../bot.ts"; +import { VoiceRegions } from "../../transformers/voiceRegion.ts"; import { DiscordVoiceRegion } from "../../types/discord.ts"; import { Collection } from "../../util/collection.ts"; /** Returns an array of voice regions that can be used when creating servers. */ -export async function getAvailableVoiceRegions(bot: Bot) { - const result = await bot.rest.runMethod( +export async function getAvailableVoiceRegions(bot: Bot): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.VOICE_REGIONS(), ); return new Collection( - result.map((region) => { - const voiceRegion = bot.transformers.voiceRegion(bot, region); - return [voiceRegion.id, voiceRegion]; + results.map((result) => { + const region = bot.transformers.voiceRegion(bot, result); + return [region.id, region]; }), ); } diff --git a/helpers/guilds/getBan.ts b/helpers/guilds/getBan.ts index b2ab5e0bf..948e07c2f 100644 --- a/helpers/guilds/getBan.ts +++ b/helpers/guilds/getBan.ts @@ -1,8 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { User } from "../../transformers/member.ts"; import { DiscordBan } from "../../types/discord.ts"; +export type Ban = { + reason?: string; + user: User; +}; + /** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */ -export async function getBan(bot: Bot, guildId: bigint, memberId: bigint) { +export async function getBan(bot: Bot, guildId: bigint, memberId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", @@ -10,7 +16,7 @@ export async function getBan(bot: Bot, guildId: bigint, memberId: bigint) { ); return { - reason: result.reason, + reason: result.reason ?? undefined, user: bot.transformers.user(bot, result.user), }; } diff --git a/helpers/guilds/getBans.ts b/helpers/guilds/getBans.ts index 9c3268e8f..6c7e785fe 100644 --- a/helpers/guilds/getBans.ts +++ b/helpers/guilds/getBans.ts @@ -1,24 +1,27 @@ import type { Bot } from "../../bot.ts"; -import { Collection } from "../../util/collection.ts"; import { DiscordBan } from "../../types/discord.ts"; -import { User } from "../../transformers/member.ts"; +import { Collection } from "../../util/collection.ts"; +import { Ban } from "./getBan.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) { +export async function getBans(bot: Bot, guildId: bigint, options?: GetBans): Promise> { const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_BANS(guildId, options), ); - return new Collection( - results.map((res) => [ - bot.transformers.snowflake(res.user.id), - { - reason: res.reason ?? undefined, - user: bot.transformers.user(bot, res.user), - }, - ]), + return new Collection( + results.map<[bigint, Ban]>((result) => { + const user = bot.transformers.user(bot, result.user); + return [ + user.id, + { + reason: result.reason ?? undefined, + user: user, + }, + ]; + }), ); } diff --git a/helpers/guilds/getGuild.ts b/helpers/guilds/getGuild.ts index 9b544057d..211658388 100644 --- a/helpers/guilds/getGuild.ts +++ b/helpers/guilds/getGuild.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../bot.ts"; +import { Guild } from "../../transformers/guild.ts"; import { DiscordGuild } from "../../types/discord.ts"; /** @@ -11,7 +12,7 @@ export async function getGuild( options: { counts?: boolean } = { counts: true, }, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/guilds/guildBannerUrl.ts b/helpers/guilds/getGuildBannerUrl.ts similarity index 81% rename from helpers/guilds/guildBannerUrl.ts rename to helpers/guilds/getGuildBannerUrl.ts index b00340dd3..f81fab2c2 100644 --- a/helpers/guilds/guildBannerUrl.ts +++ b/helpers/guilds/getGuildBannerUrl.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../bot.ts"; -import { ImageFormat, ImageSize } from "../members/avatarUrl.ts"; +import { ImageFormat, ImageSize } from "../members/getAvatarUrl.ts"; /** The full URL of the banner from Discords CDN. Undefined if no banner is set. */ -export function guildBannerURL( +export function getGuildBannerURL( bot: Bot, id: bigint, options: { @@ -10,7 +10,7 @@ export function guildBannerURL( size?: ImageSize; format?: ImageFormat; }, -) { +): string | undefined { return options.banner ? bot.utils.formatImageURL( bot.constants.routes.GUILD_BANNER( diff --git a/helpers/guilds/guildIconUrl.ts b/helpers/guilds/getGuildIconUrl.ts similarity index 79% rename from helpers/guilds/guildIconUrl.ts rename to helpers/guilds/getGuildIconUrl.ts index 16d7686ad..60e62fdb4 100644 --- a/helpers/guilds/guildIconUrl.ts +++ b/helpers/guilds/getGuildIconUrl.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../bot.ts"; -import { ImageFormat, ImageSize } from "../members/avatarUrl.ts"; +import { ImageFormat, ImageSize } from "../members/getAvatarUrl.ts"; /** The full URL of the icon from Discords CDN. Undefined when no icon is set. */ -export function guildIconURL( +export function getGuildIconURL( bot: Bot, id: bigint, icon: bigint | undefined, @@ -10,7 +10,7 @@ export function guildIconURL( size?: ImageSize; format?: ImageFormat; }, -) { +): string | undefined { return icon ? bot.utils.formatImageURL( bot.constants.routes.GUILD_ICON( diff --git a/helpers/guilds/getGuildPreview.ts b/helpers/guilds/getGuildPreview.ts index 3d84abce9..e52a0e2a4 100644 --- a/helpers/guilds/getGuildPreview.ts +++ b/helpers/guilds/getGuildPreview.ts @@ -1,8 +1,25 @@ import type { Bot } from "../../bot.ts"; +import { Emoji } from "../../transformers/emoji.ts"; +import { Sticker } from "../../transformers/sticker.ts"; import { DiscordGuildPreview } from "../../types/discord.ts"; +import { GuildFeatures } from "../../types/shared.ts"; + +export type GuildPreview = { + id: bigint; + name?: string; + icon?: string; + splash?: string; + discoverySplash?: string; + emojis?: Emoji[]; + features: GuildFeatures[]; + approximateMemberCount: number; + approximatePresenceCount: number; + description?: string; + stickers: Sticker[]; +}; /** Returns the guild preview object for the given id. If the bot is not in the guild, then the guild must be Discoverable. */ -export async function getGuildPreview(bot: Bot, guildId: bigint) { +export async function getGuildPreview(bot: Bot, guildId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/guilds/guildSplashUrl.ts b/helpers/guilds/getGuildSplashUrl.ts similarity index 80% rename from helpers/guilds/guildSplashUrl.ts rename to helpers/guilds/getGuildSplashUrl.ts index aaa9114be..83885a38d 100644 --- a/helpers/guilds/guildSplashUrl.ts +++ b/helpers/guilds/getGuildSplashUrl.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../bot.ts"; -import { ImageFormat, ImageSize } from "../members/avatarUrl.ts"; +import { ImageFormat, ImageSize } from "../members/getAvatarUrl.ts"; /** The full URL of the splash from Discords CDN. Undefined if no splash is set. */ -export function guildSplashURL( +export function getGuildSplashURL( bot: Bot, id: bigint, splash: bigint | undefined, @@ -10,7 +10,7 @@ export function guildSplashURL( size?: ImageSize; format?: ImageFormat; }, -) { +): string | undefined { return splash ? bot.utils.formatImageURL( bot.constants.routes.GUILD_SPLASH( diff --git a/helpers/guilds/getPruneCount.ts b/helpers/guilds/getPruneCount.ts index e22901124..dcbd6149c 100644 --- a/helpers/guilds/getPruneCount.ts +++ b/helpers/guilds/getPruneCount.ts @@ -1,17 +1,21 @@ import type { Bot } from "../../bot.ts"; +interface DiscordPrunedCount { + pruned: number; +} + /** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */ -export async function getPruneCount(bot: Bot, guildId: bigint, options?: GetGuildPruneCountQuery) { +export async function getPruneCount(bot: Bot, guildId: bigint, options?: GetGuildPruneCountQuery): Promise { 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); - const result = await bot.rest.runMethod( + const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_PRUNE(guildId), ); - return result.pruned as number; + return result.pruned; } /** https://discord.com/developers/docs/resources/guild#get-guild-prune-count */ diff --git a/helpers/guilds/getVanityUrl.ts b/helpers/guilds/getVanityUrl.ts index faeb02bed..e3096bc6e 100644 --- a/helpers/guilds/getVanityUrl.ts +++ b/helpers/guilds/getVanityUrl.ts @@ -1,16 +1,15 @@ import type { Bot } from "../../bot.ts"; -import { DiscordInviteMetadata } from "../../types/discord.ts"; + +export type VanityUrl = { + code?: string; + uses: number; +}; /** Returns the code and uses of the vanity url for this server if it is enabled else `code` will be null. Requires the `MANAGE_GUILD` permission. */ -export async function getVanityUrl(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod>( +export async function getVanityUrl(bot: Bot, guildId: bigint): Promise { + return await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_VANITY_URL(guildId), ); - - return { - uses: result.uses, - code: result.code, - }; } diff --git a/helpers/guilds/getVoiceRegions.ts b/helpers/guilds/getVoiceRegions.ts index 490cb91af..b73bd5554 100644 --- a/helpers/guilds/getVoiceRegions.ts +++ b/helpers/guilds/getVoiceRegions.ts @@ -1,18 +1,19 @@ -import { Collection } from "../../util/collection.ts"; import type { Bot } from "../../bot.ts"; +import { VoiceRegions } from "../../transformers/voiceRegion.ts"; import { DiscordVoiceRegion } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */ -export async function getVoiceRegions(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getVoiceRegions(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_REGIONS(guildId), ); return new Collection( - result.map((reg) => { - const region = bot.transformers.voiceRegion(bot, reg); + results.map((result) => { + const region = bot.transformers.voiceRegion(bot, result); return [region.id, region]; }), ); diff --git a/helpers/guilds/getWelcomeScreen.ts b/helpers/guilds/getWelcomeScreen.ts index d6c7aecd7..cb450de1e 100644 --- a/helpers/guilds/getWelcomeScreen.ts +++ b/helpers/guilds/getWelcomeScreen.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { WelcomeScreen } from "../../transformers/welcomeScreen.ts"; import { DiscordWelcomeScreen } from "../../types/discord.ts"; /** Returns the Welcome Screen object for the guild. Requires the `MANAGE_GUILD` permission. */ -export async function getWelcomeScreen(bot: Bot, guildId: bigint) { +export async function getWelcomeScreen(bot: Bot, guildId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/guilds/getWidget.ts b/helpers/guilds/getWidget.ts index e96bc7fad..d329a1fe3 100644 --- a/helpers/guilds/getWidget.ts +++ b/helpers/guilds/getWidget.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { GuildWidget } from "../../transformers/widget.ts"; import { DiscordGuildWidget } from "../../types/discord.ts"; /** Returns the widget for the guild. */ -export async function getWidget(bot: Bot, guildId: bigint) { +export async function getWidget(bot: Bot, guildId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/guilds/getWidgetImageUrl.ts b/helpers/guilds/getWidgetImageUrl.ts index 75dd6e385..0f2b986d8 100644 --- a/helpers/guilds/getWidgetImageUrl.ts +++ b/helpers/guilds/getWidgetImageUrl.ts @@ -1,7 +1,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) { +export function getWidgetImageURL(bot: Bot, guildId: bigint, options?: GetGuildWidgetImageQuery): string { return bot.constants.routes.GUILD_WIDGET_IMAGE(guildId, options?.style); } diff --git a/helpers/guilds/getWidgetSettings.ts b/helpers/guilds/getWidgetSettings.ts index caacdadb2..194f1386f 100644 --- a/helpers/guilds/getWidgetSettings.ts +++ b/helpers/guilds/getWidgetSettings.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { GuildWidgetSettings } from "../../transformers/widgetSettings.ts"; import { DiscordGuildWidgetSettings } from "../../types/discord.ts"; /** Returns a guild widget settings object. Requires the MANAGE_GUILD permission. */ -export async function getWidgetSettings(bot: Bot, guildId: bigint) { +export async function getWidgetSettings(bot: Bot, guildId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/guilds/leaveGuild.ts b/helpers/guilds/leaveGuild.ts index 1d998af1b..de7495a71 100644 --- a/helpers/guilds/leaveGuild.ts +++ b/helpers/guilds/leaveGuild.ts @@ -1,6 +1,6 @@ import type { Bot } from "../../bot.ts"; /** Leave a guild */ -export async function leaveGuild(bot: Bot, guildId: bigint) { - await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_LEAVE(guildId)); +export async function leaveGuild(bot: Bot, guildId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_LEAVE(guildId)); } diff --git a/helpers/guilds/mod.ts b/helpers/guilds/mod.ts index f1accd7ac..9095379c4 100644 --- a/helpers/guilds/mod.ts +++ b/helpers/guilds/mod.ts @@ -1,5 +1,5 @@ -export * from "./scheduledEvents/mod.ts"; export * from "./automod/mod.ts"; +export * from "./scheduledEvents/mod.ts"; export * from "./createGuild.ts"; export * from "./deleteGuild.ts"; @@ -11,7 +11,10 @@ export * from "./getAvailableVoiceRegions.ts"; export * from "./getBan.ts"; export * from "./getBans.ts"; export * from "./getGuild.ts"; +export * from "./getGuildBannerUrl.ts"; +export * from "./getGuildIconUrl.ts"; export * from "./getGuildPreview.ts"; +export * from "./getGuildSplashUrl.ts"; export * from "./getPruneCount.ts"; export * from "./getVanityUrl.ts"; export * from "./getVoiceRegions.ts"; @@ -19,7 +22,5 @@ export * from "./getWelcomeScreen.ts"; export * from "./getWidget.ts"; export * from "./getWidgetImageUrl.ts"; export * from "./getWidgetSettings.ts"; -export * from "./guildBannerUrl.ts"; -export * from "./guildIconUrl.ts"; -export * from "./guildSplashUrl.ts"; export * from "./leaveGuild.ts"; + diff --git a/helpers/guilds/scheduledEvents/createScheduledEvent.ts b/helpers/guilds/scheduledEvents/createScheduledEvent.ts index 2407f6a0c..b445003ca 100644 --- a/helpers/guilds/scheduledEvents/createScheduledEvent.ts +++ b/helpers/guilds/scheduledEvents/createScheduledEvent.ts @@ -1,9 +1,14 @@ import { Bot } from "../../../bot.ts"; +import { ScheduledEvent } from "../../../transformers/scheduledEvent.ts"; import { DiscordScheduledEvent } from "../../../types/discord.ts"; import { ScheduledEventEntityType, ScheduledEventPrivacyLevel } from "../../../types/shared.ts"; /** Create a guild scheduled event in the guild. A guild can have a maximum of 100 events with `SCHEDULED` or `ACTIVE` status at any time. */ -export async function createScheduledEvent(bot: Bot, guildId: bigint, options: CreateScheduledEvent) { +export async function createScheduledEvent( + bot: Bot, + guildId: bigint, + options: CreateScheduledEvent, +): Promise { if (!bot.utils.validateLength(options.name, { min: 1, max: 100 })) { throw new Error("Name must be between 1-100 characters."); } @@ -26,7 +31,7 @@ export async function createScheduledEvent(bot: Bot, guildId: bigint, options: C throw new Error("Cannot schedule event to end before starting."); } - const event = await bot.rest.runMethod( + const result = await bot.rest.runMethod( bot.rest, "POST", bot.constants.routes.GUILD_SCHEDULED_EVENTS(guildId), @@ -43,7 +48,7 @@ export async function createScheduledEvent(bot: Bot, guildId: bigint, options: C }, ); - return bot.transformers.scheduledEvent(bot, event); + return bot.transformers.scheduledEvent(bot, result); } export interface CreateScheduledEvent { diff --git a/helpers/guilds/scheduledEvents/deleteScheduledEvent.ts b/helpers/guilds/scheduledEvents/deleteScheduledEvent.ts index c8f6dec82..ca99eed7f 100644 --- a/helpers/guilds/scheduledEvents/deleteScheduledEvent.ts +++ b/helpers/guilds/scheduledEvents/deleteScheduledEvent.ts @@ -1,8 +1,8 @@ import { Bot } from "../../../bot.ts"; /** Delete a scheduled event. */ -export async function deleteScheduledEvent(bot: Bot, guildId: bigint, eventId: bigint) { - await bot.rest.runMethod( +export async function deleteScheduledEvent(bot: Bot, guildId: bigint, eventId: bigint): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.GUILD_SCHEDULED_EVENT(guildId, eventId), diff --git a/helpers/guilds/scheduledEvents/editScheduledEvent.ts b/helpers/guilds/scheduledEvents/editScheduledEvent.ts index 405d6034c..28972e8f9 100644 --- a/helpers/guilds/scheduledEvents/editScheduledEvent.ts +++ b/helpers/guilds/scheduledEvents/editScheduledEvent.ts @@ -1,4 +1,5 @@ import { Bot } from "../../../bot.ts"; +import { ScheduledEvent } from "../../../transformers/scheduledEvent.ts"; import { DiscordScheduledEvent } from "../../../types/discord.ts"; import { ScheduledEventEntityType, ScheduledEventPrivacyLevel, ScheduledEventStatus } from "../../../types/shared.ts"; @@ -8,7 +9,7 @@ export async function editScheduledEvent( guildId: bigint, eventId: bigint, options: Partial, -) { +): Promise { if (options.name && !bot.utils.validateLength(options.name, { min: 1, max: 100 })) { throw new Error("Name must be between 1-100 characters."); } @@ -22,7 +23,7 @@ export async function editScheduledEvent( throw new Error("Cannot schedule event to end before starting."); } - const event = await bot.rest.runMethod( + const result = await bot.rest.runMethod( bot.rest, "PATCH", bot.constants.routes.GUILD_SCHEDULED_EVENT(guildId, eventId), @@ -40,7 +41,7 @@ export async function editScheduledEvent( }, ); - return bot.transformers.scheduledEvent(bot, event); + return bot.transformers.scheduledEvent(bot, result); } export interface EditScheduledEvent { diff --git a/helpers/guilds/scheduledEvents/getScheduledEvent.ts b/helpers/guilds/scheduledEvents/getScheduledEvent.ts index ba43b6b89..fee78a390 100644 --- a/helpers/guilds/scheduledEvents/getScheduledEvent.ts +++ b/helpers/guilds/scheduledEvents/getScheduledEvent.ts @@ -1,4 +1,5 @@ import { Bot } from "../../../bot.ts"; +import { ScheduledEvent } from "../../../transformers/scheduledEvent.ts"; import { DiscordScheduledEvent } from "../../../types/discord.ts"; /** Get a guild scheduled event. */ @@ -7,14 +8,12 @@ export async function getScheduledEvent( guildId: bigint, eventId: bigint, options?: { withUserCount?: boolean }, -) { - const event = await bot.rest.runMethod( +): Promise { + const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_SCHEDULED_EVENT(guildId, eventId, options?.withUserCount), ); - if (!event?.id) return; - - return bot.transformers.scheduledEvent(bot, event); + return bot.transformers.scheduledEvent(bot, result); } diff --git a/helpers/guilds/scheduledEvents/getScheduledEventUsers.ts b/helpers/guilds/scheduledEvents/getScheduledEventUsers.ts index 6fdb1c202..9e71ff184 100644 --- a/helpers/guilds/scheduledEvents/getScheduledEventUsers.ts +++ b/helpers/guilds/scheduledEvents/getScheduledEventUsers.ts @@ -34,7 +34,7 @@ export async function getScheduledEventUsers( if (options.before) url += `&before=${options.before}`; } - const result = await bot.rest.runMethod<{ user: DiscordUser; member?: DiscordMember }[]>( + const results = await bot.rest.runMethod<{ user: DiscordUser; member?: DiscordMember }[]>( bot.rest, "GET", url, @@ -42,17 +42,17 @@ export async function getScheduledEventUsers( if (!options?.withMember) { return new Collection( - result.map((res) => { - const user = bot.transformers.user(bot, res.user); + results.map((result) => { + const user = bot.transformers.user(bot, result.user); return [user.id, user]; }), ); } return new Collection( - result.map((res) => { - const user = bot.transformers.user(bot, res.user); - const member: Member = bot.transformers.member(bot, res.member!, guildId, user.id); + results.map((result) => { + const user = bot.transformers.user(bot, result.user); + const member = bot.transformers.member(bot, result.member!, guildId, user.id); return [user.id, { member, user }]; }), diff --git a/helpers/guilds/scheduledEvents/getScheduledEvents.ts b/helpers/guilds/scheduledEvents/getScheduledEvents.ts index 69d35bc47..d06522296 100644 --- a/helpers/guilds/scheduledEvents/getScheduledEvents.ts +++ b/helpers/guilds/scheduledEvents/getScheduledEvents.ts @@ -4,16 +4,20 @@ import { DiscordScheduledEvent } from "../../../types/discord.ts"; import { Collection } from "../../../util/collection.ts"; /** Get a list of guild scheduled event for the given guild. */ -export async function getScheduledEvents(bot: Bot, guildId: bigint, options?: GetScheduledEvents) { - const events = await bot.rest.runMethod( +export async function getScheduledEvents( + bot: Bot, + guildId: bigint, + options?: GetScheduledEvents, +): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_SCHEDULED_EVENTS(guildId, options?.withUserCount), ); - return new Collection( - events.map((e) => { - const event = bot.transformers.scheduledEvent(bot, e); + return new Collection( + results.map((result) => { + const event = bot.transformers.scheduledEvent(bot, result); return [event.id, event]; }), ); diff --git a/helpers/integrations/deleteIntegration.ts b/helpers/integrations/deleteIntegration.ts index 19d0cb431..8fa8af047 100644 --- a/helpers/integrations/deleteIntegration.ts +++ b/helpers/integrations/deleteIntegration.ts @@ -1,6 +1,6 @@ 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(bot.rest, "DELETE", bot.constants.routes.GUILD_INTEGRATION(guildId, id)); +export async function deleteIntegration(bot: Bot, guildId: bigint, id: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_INTEGRATION(guildId, id)); } diff --git a/helpers/integrations/getIntegrations.ts b/helpers/integrations/getIntegrations.ts index b244a347a..1a43de796 100644 --- a/helpers/integrations/getIntegrations.ts +++ b/helpers/integrations/getIntegrations.ts @@ -1,34 +1,35 @@ import type { Bot } from "../../bot.ts"; -import { Collection } from "../../util/collection.ts"; +import { Integration } from "../../transformers/integration.ts"; import { DiscordIntegration } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */ -export async function getIntegrations(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getIntegrations(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_INTEGRATIONS(guildId), ); return new Collection( - result.map((res) => { + results.map((result) => { const integration = bot.transformers.integration(bot, { guild_id: guildId.toString(), - id: res.id, - name: res.name, - type: res.type, - enabled: res.enabled, - syncing: res.syncing, - role_id: res.role_id, - enable_emoticons: res.enable_emoticons, - expire_behavior: res.expire_behavior, - expire_grace_period: res.expire_grace_period, - user: res.user, - account: res.account, - synced_at: res.synced_at, - subscriber_count: res.subscriber_count, - revoked: res.revoked, - application: res.application, + id: result.id, + name: result.name, + type: result.type, + enabled: result.enabled, + syncing: result.syncing, + role_id: result.role_id, + enable_emoticons: result.enable_emoticons, + expire_behavior: result.expire_behavior, + expire_grace_period: result.expire_grace_period, + user: result.user, + account: result.account, + synced_at: result.synced_at, + subscriber_count: result.subscriber_count, + revoked: result.revoked, + application: result.application, }); return [integration.id, integration]; }), diff --git a/helpers/interactions/commands/createApplicationCommand.ts b/helpers/interactions/commands/createApplicationCommand.ts index 70a1077b1..b214e2e45 100644 --- a/helpers/interactions/commands/createApplicationCommand.ts +++ b/helpers/interactions/commands/createApplicationCommand.ts @@ -1,5 +1,5 @@ import type { Bot } from "../../../bot.ts"; -import { ApplicationCommandOption, ApplicationCommandTypes, Localization } from "../../../mod.ts"; +import { ApplicationCommand, ApplicationCommandOption, ApplicationCommandTypes, Localization } from "../../../mod.ts"; import { DiscordApplicationCommand, DiscordApplicationCommandOption } from "../../../types/discord.ts"; import { AtLeastOne, PermissionStrings } from "../../../types/shared.ts"; @@ -18,7 +18,7 @@ export async function createApplicationCommand( bot: Bot, options: CreateApplicationCommand | CreateContextApplicationCommand, guildId?: bigint, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", diff --git a/helpers/interactions/commands/deleteApplicationCommand.ts b/helpers/interactions/commands/deleteApplicationCommand.ts index ea4fe49c4..e9392197b 100644 --- a/helpers/interactions/commands/deleteApplicationCommand.ts +++ b/helpers/interactions/commands/deleteApplicationCommand.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../../bot.ts"; /** Deletes a application command. */ -export async function deleteApplicationCommand(bot: Bot, id: bigint, guildId?: bigint) { - await bot.rest.runMethod( +export async function deleteApplicationCommand(bot: Bot, id: bigint, guildId?: bigint): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", guildId diff --git a/helpers/interactions/commands/deleteInteractionResponse.ts b/helpers/interactions/commands/deleteInteractionResponse.ts index 7bdcb5808..48face800 100644 --- a/helpers/interactions/commands/deleteInteractionResponse.ts +++ b/helpers/interactions/commands/deleteInteractionResponse.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../../bot.ts"; /** To delete your response to a application command. If a message id is not provided, it will default to deleting the original response. */ -export async function deleteInteractionResponse(bot: Bot, token: string, messageId?: bigint) { - await bot.rest.runMethod( +export async function deleteInteractionResponse(bot: Bot, token: string, messageId?: bigint): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", messageId diff --git a/helpers/interactions/commands/editApplicationCommandPermissions.ts b/helpers/interactions/commands/editApplicationCommandPermissions.ts index 598327e78..a2c0c6683 100644 --- a/helpers/interactions/commands/editApplicationCommandPermissions.ts +++ b/helpers/interactions/commands/editApplicationCommandPermissions.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../../bot.ts"; +import { ApplicationCommandPermission } from "../../../transformers/applicationCommandPermission.ts"; import { DiscordGuildApplicationCommandPermissions } from "../../../types/discord.ts"; import { ApplicationCommandPermissionTypes } from "../../../types/shared.ts"; @@ -10,7 +11,7 @@ export async function editApplicationCommandPermissions( /** Bearer token which has the `applications.commands.permissions.update` scope and also access to this guild. */ bearerToken: string, options: ApplicationCommandPermissions[], -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "PUT", diff --git a/helpers/interactions/commands/editInteractionResponse.ts b/helpers/interactions/commands/editInteractionResponse.ts index 2fdafbe12..a46e9db0d 100644 --- a/helpers/interactions/commands/editInteractionResponse.ts +++ b/helpers/interactions/commands/editInteractionResponse.ts @@ -1,4 +1,6 @@ import type { Bot } from "../../../bot.ts"; +import { Message } from "../../../transformers/message.ts"; +import { DiscordMessage } from "../../../types/discord.ts"; import { MessageComponentTypes } from "../../../types/shared.ts"; import { EditWebhookMessage } from "../../webhooks/editWebhookMessage.ts"; @@ -10,8 +12,8 @@ export async function editInteractionResponse( /** Id of the message you want to edit if undefined the initial response message will be edited */ messageId?: bigint; }, -) { - const result = await bot.rest.runMethod( +): Promise { + const result = await bot.rest.runMethod( bot.rest, "PATCH", options.messageId @@ -101,7 +103,7 @@ export async function editInteractionResponse( ); // If the original message was edited, this will not return a message - if (!options.messageId) return result as undefined; + if (!options.messageId) return; return bot.transformers.message(bot, result); } diff --git a/helpers/interactions/commands/getApplicationCommand.ts b/helpers/interactions/commands/getApplicationCommand.ts index 8f4f4d10c..8a28d0017 100644 --- a/helpers/interactions/commands/getApplicationCommand.ts +++ b/helpers/interactions/commands/getApplicationCommand.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../../bot.ts"; +import { ApplicationCommand } from "../../../transformers/applicationCommand.ts"; 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) { +export async function getApplicationCommand(bot: Bot, commandId: bigint, options?: GetApplicationCommand): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/interactions/commands/getApplicationCommandPermission.ts b/helpers/interactions/commands/getApplicationCommandPermission.ts index db51ecb53..24a798c10 100644 --- a/helpers/interactions/commands/getApplicationCommandPermission.ts +++ b/helpers/interactions/commands/getApplicationCommandPermission.ts @@ -1,8 +1,13 @@ import type { Bot } from "../../../bot.ts"; +import { ApplicationCommandPermission } from "../../../transformers/applicationCommandPermission.ts"; import { DiscordGuildApplicationCommandPermissions } from "../../../types/discord.ts"; /** Fetches command permissions for a specific command for your application in a guild. Returns a GuildApplicationCommandPermissions object. */ -export async function getApplicationCommandPermission(bot: Bot, guildId: bigint, commandId: bigint) { +export async function getApplicationCommandPermission( + bot: Bot, + guildId: bigint, + commandId: bigint, +): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/interactions/commands/getApplicationCommandPermissions.ts b/helpers/interactions/commands/getApplicationCommandPermissions.ts index b3e1d81af..6ab44fcab 100644 --- a/helpers/interactions/commands/getApplicationCommandPermissions.ts +++ b/helpers/interactions/commands/getApplicationCommandPermissions.ts @@ -1,19 +1,23 @@ import type { Bot } from "../../../bot.ts"; +import { ApplicationCommandPermission } from "../../../transformers/applicationCommandPermission.ts"; import { DiscordGuildApplicationCommandPermissions } from "../../../types/discord.ts"; import { Collection } from "../../../util/collection.ts"; /** Fetches command permissions for all commands for your application in a guild. Returns an array of GuildApplicationCommandPermissions objects. */ -export async function getApplicationCommandPermissions(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getApplicationCommandPermissions( + bot: Bot, + guildId: bigint, +): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.COMMANDS_PERMISSIONS(bot.applicationId, guildId), ); return new Collection( - result.map((res) => { - const perms = bot.transformers.applicationCommandPermission(bot, res); - return [perms.id, perms]; + results.map((result) => { + const permission = bot.transformers.applicationCommandPermission(bot, result); + return [permission.id, permission]; }), ); } diff --git a/helpers/interactions/commands/getApplicationCommands.ts b/helpers/interactions/commands/getApplicationCommands.ts index 65022d09b..821e0be07 100644 --- a/helpers/interactions/commands/getApplicationCommands.ts +++ b/helpers/interactions/commands/getApplicationCommands.ts @@ -1,10 +1,14 @@ -import { Collection } from "../../../util/collection.ts"; import type { Bot } from "../../../bot.ts"; +import { ApplicationCommand } from "../../../transformers/applicationCommand.ts"; import { DiscordApplicationCommand } from "../../../types/discord.ts"; +import { Collection } from "../../../util/collection.ts"; /** Fetch all the commands for your application. If a guild id is not provided, it will fetch global commands. */ -export async function getApplicationCommands(bot: Bot, guildId?: bigint) { - const result = await bot.rest.runMethod( +export async function getApplicationCommands( + bot: Bot, + guildId?: bigint, +): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", guildId @@ -13,8 +17,8 @@ export async function getApplicationCommands(bot: Bot, guildId?: bigint) { ); return new Collection( - result.map((res) => { - const command = bot.transformers.applicationCommand(bot, res); + results.map((result) => { + const command = bot.transformers.applicationCommand(bot, result); return [command.id, command]; }), ); diff --git a/helpers/interactions/commands/upsertApplicationCommand.ts b/helpers/interactions/commands/upsertApplicationCommand.ts index c04f6cad5..5256ddc8f 100644 --- a/helpers/interactions/commands/upsertApplicationCommand.ts +++ b/helpers/interactions/commands/upsertApplicationCommand.ts @@ -1,12 +1,13 @@ import type { Bot } from "../../../bot.ts"; +import { ApplicationCommand } from "../../../transformers/applicationCommand.ts"; +import { DiscordApplicationCommand } from "../../../types/discord.ts"; +import { AtLeastOne } from "../../../types/shared.ts"; import { CreateApplicationCommand, CreateContextApplicationCommand, isContextApplicationCommand, - makeOptionsForCommand, + makeOptionsForCommand } from "./createApplicationCommand.ts"; -import { DiscordApplicationCommand } from "../../../types/discord.ts"; -import { AtLeastOne } from "../../../types/shared.ts"; /** * Edit an existing application command. If this command did not exist, it will create it. @@ -16,7 +17,7 @@ export async function upsertApplicationCommand( commandId: bigint, options: AtLeastOne | AtLeastOne, guildId?: bigint, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/interactions/commands/upsertApplicationCommands.ts b/helpers/interactions/commands/upsertApplicationCommands.ts index c8976b991..9d06e60db 100644 --- a/helpers/interactions/commands/upsertApplicationCommands.ts +++ b/helpers/interactions/commands/upsertApplicationCommands.ts @@ -1,4 +1,6 @@ import type { Bot } from "../../../bot.ts"; +import { ApplicationCommand } from "../../../transformers/applicationCommand.ts"; +import { DiscordApplicationCommand } from "../../../types/discord.ts"; import { Collection } from "../../../util/collection.ts"; import { CreateApplicationCommand, @@ -6,8 +8,6 @@ import { isContextApplicationCommand, makeOptionsForCommand, } from "./createApplicationCommand.ts"; -import { DiscordApplicationCommand } from "../../../types/discord.ts"; -import { MakeRequired } from "../../../types/shared.ts"; /** * Bulk edit existing application commands. If a command does not exist, it will create it. @@ -18,8 +18,8 @@ export async function upsertApplicationCommands( bot: Bot, options: (UpsertApplicationCommands | CreateContextApplicationCommand)[], guildId?: bigint, -) { - const result = await bot.rest.runMethod( +): Promise> { + const results = await bot.rest.runMethod( bot.rest, "PUT", guildId @@ -40,8 +40,8 @@ export async function upsertApplicationCommands( ); return new Collection( - result.map((res) => { - const command = bot.transformers.applicationCommand(bot, res); + results.map((result) => { + const command = bot.transformers.applicationCommand(bot, result); return [command.id, command]; }), ); diff --git a/helpers/interactions/followups/deleteFollowupMessage.ts b/helpers/interactions/followups/deleteFollowupMessage.ts index 3d084441d..f4b38c9c3 100644 --- a/helpers/interactions/followups/deleteFollowupMessage.ts +++ b/helpers/interactions/followups/deleteFollowupMessage.ts @@ -1,8 +1,8 @@ import { Bot } from "../../../bot.ts"; /** Deletes a followup message for an Interaction. Functions the same as delete webhook message, however this uses your interaction token instead of bot token. Does not support ephemeral followups. */ -export async function deleteFollowupMessage(bot: Bot, interactionToken: string, messageId: bigint) { - await bot.rest.runMethod( +export async function deleteFollowupMessage(bot: Bot, interactionToken: string, messageId: bigint): Promise { + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.WEBHOOK_MESSAGE(bot.applicationId, interactionToken, messageId), diff --git a/helpers/interactions/followups/editFollowupMessage.ts b/helpers/interactions/followups/editFollowupMessage.ts index e0b304d7b..5dcb8eee7 100644 --- a/helpers/interactions/followups/editFollowupMessage.ts +++ b/helpers/interactions/followups/editFollowupMessage.ts @@ -1,4 +1,5 @@ import { Bot } from "../../../bot.ts"; +import { Message } from "../../../transformers/message.ts"; import { DiscordMessage } from "../../../types/discord.ts"; import { MessageComponentTypes } from "../../../types/shared.ts"; import { EditWebhookMessage } from "../../webhooks/editWebhookMessage.ts"; @@ -9,7 +10,7 @@ export async function editFollowupMessage( interactionToken: string, messageId: bigint, options: EditWebhookMessage, -) { +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/interactions/followups/getFollowupMessage.ts b/helpers/interactions/followups/getFollowupMessage.ts index 0cc79d125..8fdc7573f 100644 --- a/helpers/interactions/followups/getFollowupMessage.ts +++ b/helpers/interactions/followups/getFollowupMessage.ts @@ -1,8 +1,9 @@ import { Bot } from "../../../bot.ts"; +import { Message } from "../../../transformers/message.ts"; import { DiscordMessage } from "../../../types/discord.ts"; /** Returns a followup message for an Interaction. Functions the same as get webhook message, however this uses your interaction token instead of bot token. Does not support ephemeral followups. */ -export async function getFollowupMessage(bot: Bot, interactionToken: string, messageId: bigint) { +export async function getFollowupMessage(bot: Bot, interactionToken: string, messageId: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/interactions/getOriginalInteractionResponse.ts b/helpers/interactions/getOriginalInteractionResponse.ts index e6f5ca1e2..290417049 100644 --- a/helpers/interactions/getOriginalInteractionResponse.ts +++ b/helpers/interactions/getOriginalInteractionResponse.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { Message } from "../../transformers/message.ts"; import { DiscordMessage } from "../../types/discord.ts"; /** Returns the initial Interaction response. Functions the same as Get Webhook Message */ -export async function getOriginalInteractionResponse(bot: Bot, token: string) { +export async function getOriginalInteractionResponse(bot: Bot, token: string): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/interactions/sendInteractionResponse.ts b/helpers/interactions/sendInteractionResponse.ts index 97392cd03..ffce28f0b 100644 --- a/helpers/interactions/sendInteractionResponse.ts +++ b/helpers/interactions/sendInteractionResponse.ts @@ -1,5 +1,5 @@ import type { Bot } from "../../bot.ts"; -import { Embed } from "../../mod.ts"; +import { Embed, Message } from "../../mod.ts"; import { DiscordMessage } from "../../types/discord.ts"; import { AllowedMentions, FileContent, MessageComponents } from "../../types/discordeno.ts"; import { InteractionResponseTypes } from "../../types/shared.ts"; @@ -15,7 +15,7 @@ export async function sendInteractionResponse( id: bigint, token: string, options: InteractionResponse, -) { +): Promise { // If no mentions are provided, force disable mentions if (!options.data?.allowedMentions) { options.data = { ...options.data, allowedMentions: { parse: [] } }; diff --git a/helpers/invites/createInvite.ts b/helpers/invites/createInvite.ts index 498baa216..60a98b829 100644 --- a/helpers/invites/createInvite.ts +++ b/helpers/invites/createInvite.ts @@ -1,9 +1,10 @@ import type { Bot } from "../../bot.ts"; import { DiscordInvite } from "../../types/discord.ts"; -import { InviteTargetTypes } from "../../types/shared.ts"; +import { TargetTypes } from "../../types/shared.ts"; +import { Invite } from "./getInvite.ts"; /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ -export async function createInvite(bot: Bot, channelId: bigint, options: CreateChannelInvite = {}) { +export async function createInvite(bot: Bot, channelId: bigint, options: CreateChannelInvite = {}): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", @@ -14,8 +15,8 @@ export async function createInvite(bot: Bot, channelId: bigint, options: CreateC temporary: options.temporary, unique: options.unique, target_type: options.targetType, - target_user_id: options.targetUserId, - target_application_id: options.targetApplicationId, + target_user_id: options.targetUserId?.toString(), + target_application_id: options.targetApplicationId?.toString(), }, ); @@ -45,9 +46,9 @@ export interface CreateChannelInvite { /** If true, don't try to reuse similar invite (useful for creating many unique one time use invites). Default: false */ unique?: boolean; /** The type of target for this voice channel invite */ - targetType?: InviteTargetTypes; + targetType?: TargetTypes; /** The id of the user whose stream to display for this invite, required if `target_type` is 1, the user must be streaming in the channel */ - targetUserId?: string; + targetUserId?: bigint; /** The id of the embedded application to open for this invite, required if `target_type` is 2, the application must have the `EMBEDDED` flag */ - targetApplicationId?: string; + targetApplicationId?: bigint; } diff --git a/helpers/invites/deleteInvite.ts b/helpers/invites/deleteInvite.ts index 21f7ee839..b0c148870 100644 --- a/helpers/invites/deleteInvite.ts +++ b/helpers/invites/deleteInvite.ts @@ -1,7 +1,6 @@ import type { Bot } from "../../bot.ts"; -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(bot.rest, "DELETE", bot.constants.routes.INVITE(inviteCode)); +export async function deleteInvite(bot: Bot, inviteCode: string): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.INVITE(inviteCode)); } diff --git a/helpers/invites/getChannelInvites.ts b/helpers/invites/getChannelInvites.ts index becc88e58..0b212704f 100644 --- a/helpers/invites/getChannelInvites.ts +++ b/helpers/invites/getChannelInvites.ts @@ -1,25 +1,45 @@ -import { Collection } from "../../util/collection.ts"; import type { Bot } from "../../bot.ts"; import { DiscordInviteMetadata } from "../../types/discord.ts"; +import { TargetTypes } from "../../types/shared.ts"; +import { Collection } from "../../util/collection.ts"; +import { InviteMetadata } from "./getInvite.ts"; /** Gets the invites for this channel. Requires MANAGE_CHANNEL */ -export async function getChannelInvites(bot: Bot, channelId: bigint) { - const result = await bot.rest.runMethod( +export async function getChannelInvites(bot: Bot, channelId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.CHANNEL_INVITES(channelId), ); return new Collection( - result.map((invite) => [ - invite.code, - { - uses: invite.uses, - maxUses: invite.max_uses, - maxAge: invite.max_age, - temporary: invite.temporary, - createdAt: Date.parse(invite.created_at), - }, - ]), + results.map<[string, InviteMetadata]>((result) => { + const invite = { + code: result.code, + guildId: result.guild?.id ? bot.transformers.snowflake(result.guild.id) : undefined, + channelId: result.channel?.id ? bot.transformers.snowflake(result.channel.id) : undefined, + inviter: result.inviter ? bot.transformers.user(bot, result.inviter) : undefined, + targetType: result.target_type + ? (result.target_type === 1 ? TargetTypes.Stream : TargetTypes.EmbeddedApplication) + : undefined, + targetUser: result.target_user ? bot.transformers.user(bot, result.target_user) : undefined, + targetApplicationId: result.target_application?.id + ? bot.transformers.snowflake(result.target_application.id) + : undefined, + approximatePresenceCount: result.approximate_presence_count, + approximateMemberCount: result.approximate_member_count, + expiresAt: result.expires_at ? Date.parse(result.expires_at) : undefined, + guildScheduledEvent: result.guild_scheduled_event + ? bot.transformers.scheduledEvent(bot, result.guild_scheduled_event) + : undefined, + // Metadata structure + uses: result.uses, + maxUses: result.max_uses, + maxAge: result.max_age, + temporary: result.temporary, + createdAt: Date.parse(result.created_at), + }; + return [invite.code, invite]; + }), ); } diff --git a/helpers/invites/getInvite.ts b/helpers/invites/getInvite.ts index 034eee463..30bfd5e07 100644 --- a/helpers/invites/getInvite.ts +++ b/helpers/invites/getInvite.ts @@ -1,8 +1,31 @@ import type { Bot } from "../../bot.ts"; +import { ScheduledEvent, TargetTypes, User } from "../../mod.ts"; import { DiscordInviteMetadata } from "../../types/discord.ts"; +export type Invite = { + code: string; + guildId?: bigint; + channelId?: bigint; + inviter?: User; + targetType?: TargetTypes; + targetUser?: User; + targetApplicationId?: bigint; + approximatePresenceCount?: number; + approximateMemberCount?: number; + expiresAt?: number; + guildScheduledEvent?: ScheduledEvent; +}; + +export type InviteMetadata = Invite & { + uses: number; + maxUses: number; + maxAge: number; + temporary: boolean; + createdAt: number; +}; + /** 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) { +export async function getInvite(bot: Bot, inviteCode: string, options?: GetInvite): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", @@ -14,7 +37,9 @@ export async function getInvite(bot: Bot, inviteCode: string, options?: GetInvit guildId: result.guild?.id ? bot.transformers.snowflake(result.guild.id) : undefined, channelId: result.channel?.id ? bot.transformers.snowflake(result.channel.id) : undefined, inviter: result.inviter ? bot.transformers.user(bot, result.inviter) : undefined, - targetType: result.target_type, + targetType: result.target_type + ? (result.target_type === 1 ? TargetTypes.Stream : TargetTypes.EmbeddedApplication) + : undefined, targetUser: result.target_user ? bot.transformers.user(bot, result.target_user) : undefined, targetApplicationId: result.target_application?.id ? bot.transformers.snowflake(result.target_application.id) diff --git a/helpers/invites/getInvites.ts b/helpers/invites/getInvites.ts index 93ef62344..bb1be0b49 100644 --- a/helpers/invites/getInvites.ts +++ b/helpers/invites/getInvites.ts @@ -1,76 +1,45 @@ import type { Bot } from "../../bot.ts"; +import { TargetTypes } from "../../mod.ts"; import { DiscordInviteMetadata } from "../../types/discord.ts"; import { Collection } from "../../util/collection.ts"; +import { InviteMetadata } from "./getInvite.ts"; /** Get all the invites for this guild. Requires MANAGE_GUILD permission */ -export async function getInvites(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod( +export async function getInvites(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_INVITES(guildId), ); return new Collection( - result.map((invite) => [ - invite.code, - { - code: invite.code, - type: invite.target_type, - expiresAt: invite.expires_at ? Date.parse(invite.expires_at) : undefined, - guild: invite.guild - ? { - id: bot.transformers.snowflake(invite.guild.id!), - name: invite.guild.name, - splash: invite.guild.splash ? bot.utils.iconHashToBigInt(invite.guild.splash) : undefined, - banner: invite.guild.banner ? bot.utils.iconHashToBigInt(invite.guild.banner) : undefined, - icon: invite.guild.icon ? bot.utils.iconHashToBigInt(invite.guild.icon) : undefined, - description: invite.guild.description, - features: invite.guild.features, - verificationLevel: invite.guild.verification_level, - vanityUrlCode: invite.guild.vanity_url_code, - premiumSubscriptionCount: invite.guild.premium_subscription_count, - nsfw_level: invite.guild.nsfw_level, - welcomeScreen: invite.guild.welcome_screen - ? { - description: invite.guild.welcome_screen.description, - channels: invite.guild.welcome_screen.welcome_channels.map((channel) => ({ - id: bot.transformers.snowflake(channel.channel_id), - description: channel.description, - emoji: { - id: channel.emoji_id ? bot.transformers.snowflake(channel.emoji_id) : undefined, - name: channel.emoji_name, - }, - })), - } - : undefined, - } + results.map<[string, InviteMetadata]>((result) => { + const inviteMetadata = { + code: result.code, + guildId: result.guild?.id ? bot.transformers.snowflake(result.guild.id) : undefined, + channelId: result.channel?.id ? bot.transformers.snowflake(result.channel.id) : undefined, + inviter: result.inviter ? bot.transformers.user(bot, result.inviter) : undefined, + targetType: result.target_type + ? (result.target_type === 1 ? TargetTypes.Stream : TargetTypes.EmbeddedApplication) : undefined, - channel: invite.channel - ? { - id: bot.transformers.snowflake(invite.channel.id!), - name: invite.channel.name, - type: invite.channel.type, - } + targetUser: result.target_user ? bot.transformers.user(bot, result.target_user) : undefined, + targetApplicationId: result.target_application?.id + ? bot.transformers.snowflake(result.target_application.id) : undefined, - inviter: invite.inviter - ? { - id: bot.transformers.snowflake(invite.inviter.id), - username: invite.inviter.username, - avatar: invite.inviter.avatar ? bot.utils.iconHashToBigInt(invite.inviter.avatar) : undefined, - // avatarDecoration: invite.inviter.avatar_decoration - // ? bot.utils.iconHashToBigInt(invite.inviter.avatar_decoration) - // : undefined, - discriminator: invite.inviter.discriminator, - publicFlags: invite.inviter.public_flags, - } + approximatePresenceCount: result.approximate_presence_count, + approximateMemberCount: result.approximate_member_count, + expiresAt: result.expires_at ? Date.parse(result.expires_at) : undefined, + guildScheduledEvent: result.guild_scheduled_event + ? bot.transformers.scheduledEvent(bot, result.guild_scheduled_event) : undefined, // Metadata structure - uses: invite.uses, - maxUses: invite.max_uses, - maxAge: invite.max_age, - temporary: invite.temporary, - createdAt: Date.parse(invite.created_at), - }, - ]), + uses: result.uses, + maxUses: result.max_uses, + maxAge: result.max_age, + temporary: result.temporary, + createdAt: Date.parse(result.created_at), + }; + return [inviteMetadata.code, inviteMetadata]; + }), ); } diff --git a/helpers/members/banMember.ts b/helpers/members/banMember.ts index 168d4769b..3b5a150df 100644 --- a/helpers/members/banMember.ts +++ b/helpers/members/banMember.ts @@ -1,8 +1,8 @@ import type { Bot } from "../../bot.ts"; /** Ban a user from the guild and optionally delete previous messages sent by the user. Requires the BAN_MEMBERS permission. */ -export async function banMember(bot: Bot, guildId: bigint, id: bigint, options?: CreateGuildBan) { - await bot.rest.runMethod( +export async function banMember(bot: Bot, guildId: bigint, id: bigint, options?: CreateGuildBan): Promise { + return await bot.rest.runMethod( bot.rest, "PUT", bot.constants.routes.GUILD_BAN(guildId, id), diff --git a/helpers/members/editBotNickname.ts b/helpers/members/editBotNickname.ts index 57fd5a562..0aad431bf 100644 --- a/helpers/members/editBotNickname.ts +++ b/helpers/members/editBotNickname.ts @@ -1,15 +1,19 @@ import type { Bot } from "../../bot.ts"; /** Edit the nickname of the bot in this guild */ -export async function editBotNickname(bot: Bot, guildId: bigint, options: { nick: string | null; reason?: string }) { - const response = await bot.rest.runMethod<{ nick: string }>( +export async function editBotNickname( + bot: Bot, + guildId: bigint, + options: { nick: string | null; reason?: string }, +): Promise { + const result = await bot.rest.runMethod<{ nick?: string }>( bot.rest, "PATCH", bot.constants.routes.USER_NICK(guildId), options, ); - if (!response?.nick) return; + if (!result?.nick) return; - return response.nick; + return result.nick; } diff --git a/helpers/members/editMember.ts b/helpers/members/editMember.ts index 83eb254ec..0a6ad6eff 100644 --- a/helpers/members/editMember.ts +++ b/helpers/members/editMember.ts @@ -1,8 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { Member } from "../../transformers/member.ts"; import { DiscordMemberWithUser } from "../../types/discord.ts"; /** Edit the member */ -export async function editMember(bot: Bot, guildId: bigint, memberId: bigint, options: ModifyGuildMember) { +export async function editMember( + bot: Bot, + guildId: bigint, + memberId: bigint, + options: ModifyGuildMember, +): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/members/fetchMembers.ts b/helpers/members/fetchMembers.ts index 7674be312..c52b95fa1 100644 --- a/helpers/members/fetchMembers.ts +++ b/helpers/members/fetchMembers.ts @@ -11,7 +11,7 @@ export function fetchMembers( bot: Bot, guildId: bigint, options?: Omit, -) { +): Promise { // You can request 1 member without the intent // Check if intents is not 0 as proxy ws won't set intents in other instances if (bot.intents && (!options?.limit || options.limit > 1) && !(bot.intents & GatewayIntents.GuildMembers)) { @@ -45,7 +45,7 @@ export function fetchMembers( nonce, }, }); - }) as Promise; + }); } /** https://discord.com/developers/docs/topics/gateway#request-guild-members */ diff --git a/helpers/members/avatarUrl.ts b/helpers/members/getAvatarUrl.ts similarity index 95% rename from helpers/members/avatarUrl.ts rename to helpers/members/getAvatarUrl.ts index fcdc89b47..f797311c4 100644 --- a/helpers/members/avatarUrl.ts +++ b/helpers/members/getAvatarUrl.ts @@ -1,7 +1,7 @@ import type { Bot } from "../../bot.ts"; /** The users custom avatar or the default avatar if you don't have a member object. */ -export function avatarURL( +export function getAvatarURL( bot: Bot, userId: bigint, discriminator: string, @@ -10,7 +10,7 @@ export function avatarURL( size?: ImageSize; format?: ImageFormat; }, -) { +): string { return options?.avatar ? bot.utils.formatImageURL( bot.constants.routes.USER_AVATAR( diff --git a/helpers/members/getDmChannel.ts b/helpers/members/getDmChannel.ts index fdc228aeb..7c5deeea3 100644 --- a/helpers/members/getDmChannel.ts +++ b/helpers/members/getDmChannel.ts @@ -1,15 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { Channel } from "../../transformers/channel.ts"; import { DiscordChannel } from "../../types/discord.ts"; /** Get a user's dm channel. This is required in order to send a DM. */ -export async function getDmChannel(bot: Bot, userId: bigint) { +export async function getDmChannel(bot: Bot, userId: bigint): Promise { if (userId === bot.id) throw new Error(bot.constants.Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF); - const dmChannelData = await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.USER_DM(), { + const result = await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.USER_DM(), { recipient_id: userId.toString(), }); - if (!dmChannelData?.id) return; - - return bot.transformers.channel(bot, { channel: dmChannelData }); + return bot.transformers.channel(bot, { channel: result }); } diff --git a/helpers/members/getMember.ts b/helpers/members/getMember.ts index 3263e3e88..91420cb1a 100644 --- a/helpers/members/getMember.ts +++ b/helpers/members/getMember.ts @@ -1,15 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { Member } from "../../transformers/member.ts"; import { DiscordMemberWithUser } from "../../types/discord.ts"; /** Returns a guild member object for the specified user. */ -export async function getMember(bot: Bot, guildId: bigint, id: bigint) { - const data = await bot.rest.runMethod( +export async function getMember(bot: Bot, guildId: bigint, id: bigint): Promise { + const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_MEMBER(guildId, id), ); - if (!data?.user.id) return; - - return bot.transformers.member(bot, data, guildId, id); + return bot.transformers.member(bot, result, guildId, id); } diff --git a/helpers/members/getMembers.ts b/helpers/members/getMembers.ts index f35923448..1d6d79767 100644 --- a/helpers/members/getMembers.ts +++ b/helpers/members/getMembers.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../bot.ts"; +import { Member } from "../../transformers/member.ts"; import { DiscordMemberWithUser } from "../../types/discord.ts"; import { Collection } from "../../util/collection.ts"; @@ -8,16 +9,20 @@ import { Collection } from "../../util/collection.ts"; * 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) { - const result = await bot.rest.runMethod( +export async function getMembers( + bot: Bot, + guildId: bigint, + options: ListGuildMembers, +): Promise> { + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_MEMBERS(guildId, options), ); return new Collection( - result.map((res) => { - const member = bot.transformers.member(bot, res, guildId, bot.transformers.snowflake(res.user.id)); + results.map((result) => { + const member = bot.transformers.member(bot, result, guildId, bot.transformers.snowflake(result.user.id)); return [member.id, member]; }), ); diff --git a/helpers/members/kickMember.ts b/helpers/members/kickMember.ts index fddca3e9d..3c9232bbd 100644 --- a/helpers/members/kickMember.ts +++ b/helpers/members/kickMember.ts @@ -1,8 +1,13 @@ 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(bot.rest, "DELETE", bot.constants.routes.GUILD_MEMBER(guildId, memberId), { - reason, - }); +export async function kickMember(bot: Bot, guildId: bigint, memberId: bigint, reason?: string): Promise { + return await bot.rest.runMethod( + bot.rest, + "DELETE", + bot.constants.routes.GUILD_MEMBER(guildId, memberId), + { + reason, + }, + ); } diff --git a/helpers/members/mod.ts b/helpers/members/mod.ts index e30177a80..f18d3c543 100644 --- a/helpers/members/mod.ts +++ b/helpers/members/mod.ts @@ -1,8 +1,8 @@ -export * from "./avatarUrl.ts"; export * from "./banMember.ts"; export * from "./editBotNickname.ts"; export * from "./editMember.ts"; export * from "./fetchMembers.ts"; +export * from "./getAvatarUrl.ts"; export * from "./getDmChannel.ts"; export * from "./getMember.ts"; export * from "./getMembers.ts"; @@ -10,3 +10,4 @@ export * from "./kickMember.ts"; export * from "./pruneMembers.ts"; export * from "./searchMembers.ts"; export * from "./unbanMember.ts"; + diff --git a/helpers/members/pruneMembers.ts b/helpers/members/pruneMembers.ts index c214ddf02..580ff359b 100644 --- a/helpers/members/pruneMembers.ts +++ b/helpers/members/pruneMembers.ts @@ -5,7 +5,7 @@ import type { Bot } from "../../bot.ts"; * * By default, prune will not remove users with roles. You can optionally include specific roles in your prune by providing the roles (resolved to include_roles internally) parameter. Any inactive user that has a subset of the provided role(s) will be included in the prune and users with additional roles will not. */ -export async function pruneMembers(bot: Bot, guildId: bigint, options: BeginGuildPrune) { +export async function pruneMembers(bot: Bot, guildId: bigint, options: BeginGuildPrune): Promise { 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); diff --git a/helpers/members/searchMembers.ts b/helpers/members/searchMembers.ts index 457652f12..da252c83b 100644 --- a/helpers/members/searchMembers.ts +++ b/helpers/members/searchMembers.ts @@ -1,8 +1,9 @@ -import type { SearchMembers } from "../../types/discordeno.ts"; import type { DiscordMemberWithUser } from "../../types/discord.ts"; +import type { SearchMembers } from "../../types/discordeno.ts"; -import { Collection } from "../../util/collection.ts"; import { Bot } from "../../bot.ts"; +import { Member } from "../../transformers/member.ts"; +import { Collection } from "../../util/collection.ts"; /** * Query string to match username(s) and nickname(s) against @@ -12,7 +13,7 @@ export async function searchMembers( guildId: bigint, query: string, options?: Omit, -) { +): Promise> { if (options?.limit) { if (options.limit < 1) throw new Error(bot.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_LOW); if (options.limit > 1000) { @@ -20,16 +21,16 @@ export async function searchMembers( } } - const result = await bot.rest.runMethod( + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.GUILD_MEMBERS_SEARCH(guildId, query, options), ); return new Collection( - result.map((member) => { - const m = bot.transformers.member(bot, member, guildId, bot.transformers.snowflake(member.user.id)); - return [m.id, m]; + results.map((result) => { + const member = bot.transformers.member(bot, result, guildId, bot.transformers.snowflake(result.user.id)); + return [member.id, member]; }), ); } diff --git a/helpers/members/unbanMember.ts b/helpers/members/unbanMember.ts index 81027d125..cb5a4ef33 100644 --- a/helpers/members/unbanMember.ts +++ b/helpers/members/unbanMember.ts @@ -1,6 +1,6 @@ import type { Bot } from "../../bot.ts"; /** Remove the ban for a user. Requires BAN_MEMBERS permission */ -export async function unbanMember(bot: Bot, guildId: bigint, id: bigint) { - await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_BAN(guildId, id)); +export async function unbanMember(bot: Bot, guildId: bigint, id: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_BAN(guildId, id)); } diff --git a/helpers/messages/addReaction.ts b/helpers/messages/addReaction.ts index 2937cf55a..0b9cd6a6b 100644 --- a/helpers/messages/addReaction.ts +++ b/helpers/messages/addReaction.ts @@ -1,14 +1,14 @@ import type { Bot } from "../../bot.ts"; /** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ -export async function addReaction(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { +export async function addReaction(bot: Bot, channelId: bigint, messageId: bigint, reaction: string): Promise { if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); } else if (reaction.startsWith("( + return await bot.rest.runMethod( bot.rest, "PUT", bot.constants.routes.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction), diff --git a/helpers/messages/addReactions.ts b/helpers/messages/addReactions.ts index faffb1d11..6b23f8c27 100644 --- a/helpers/messages/addReactions.ts +++ b/helpers/messages/addReactions.ts @@ -7,13 +7,12 @@ export async function addReactions( messageId: bigint, reactions: string[], ordered = false, -) { +): Promise { if (!ordered) { - await Promise.all(reactions.map((reaction) => bot.helpers.addReaction(channelId, messageId, reaction))); - } else { - for (const reaction of reactions) { - bot.events.debug("Running for of loop in addReactions function."); - await bot.helpers.addReaction(channelId, messageId, reaction); - } + return void await Promise.all(reactions.map((reaction) => bot.helpers.addReaction(channelId, messageId, reaction))); + } + + for (const reaction of reactions) { + await bot.helpers.addReaction(channelId, messageId, reaction); } } diff --git a/helpers/messages/deleteAllReactions.ts b/helpers/messages/deleteAllReactions.ts new file mode 100644 index 000000000..709df67af --- /dev/null +++ b/helpers/messages/deleteAllReactions.ts @@ -0,0 +1,10 @@ +import type { Bot } from "../../bot.ts"; + +/** Deletes all reactions for all emojis on this message. */ +export async function deleteAllReactions(bot: Bot, channelId: bigint, messageId: bigint): Promise { + return await bot.rest.runMethod( + bot.rest, + "DELETE", + bot.constants.routes.CHANNEL_MESSAGE_REACTIONS(channelId, messageId), + ); +} diff --git a/helpers/messages/deleteMessage.ts b/helpers/messages/deleteMessage.ts index a52895fc0..6b58cc32f 100644 --- a/helpers/messages/deleteMessage.ts +++ b/helpers/messages/deleteMessage.ts @@ -7,10 +7,10 @@ export async function deleteMessage( messageId: bigint, reason?: string, delayMilliseconds = 0, -) { +): Promise { if (delayMilliseconds) await bot.utils.delay(delayMilliseconds); - await bot.rest.runMethod( + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.CHANNEL_MESSAGE(channelId, messageId), diff --git a/helpers/messages/deleteMessages.ts b/helpers/messages/deleteMessages.ts index 9f05d3852..1e9ab21e7 100644 --- a/helpers/messages/deleteMessages.ts +++ b/helpers/messages/deleteMessages.ts @@ -1,17 +1,17 @@ import type { Bot } from "../../bot.ts"; /** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */ -export async function deleteMessages(bot: Bot, channelId: bigint, ids: bigint[], reason?: string) { +export async function deleteMessages(bot: Bot, channelId: bigint, ids: bigint[], reason?: string): Promise { if (ids.length < 2) { throw new Error(bot.constants.Errors.DELETE_MESSAGES_MIN); } if (ids.length > 100) { - console.warn(`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`); + console.warn(`This endpoint only accepts a maximum of 100 messages. Using the first 100 message ids provided.`); } - await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.CHANNEL_BULK_DELETE(channelId), { - messages: ids.splice(0, 100).map((id) => id.toString()), + return await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.CHANNEL_BULK_DELETE(channelId), { + messages: ids.slice(0, 100).map((id) => id.toString()), reason, }); } diff --git a/helpers/messages/removeReaction.ts b/helpers/messages/deleteReaction.ts similarity index 82% rename from helpers/messages/removeReaction.ts rename to helpers/messages/deleteReaction.ts index c656c44d3..fc01ababa 100644 --- a/helpers/messages/removeReaction.ts +++ b/helpers/messages/deleteReaction.ts @@ -1,20 +1,20 @@ import type { Bot } from "../../bot.ts"; -/** Removes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */ -export async function removeReaction( +/** Deletes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */ +export async function deleteReaction( bot: Bot, channelId: bigint, messageId: bigint, reaction: string, options?: { userId?: bigint }, -) { +): Promise { if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); } else if (reaction.startsWith("( + return await bot.rest.runMethod( bot.rest, "DELETE", options?.userId diff --git a/helpers/messages/removeReactionEmoji.ts b/helpers/messages/deleteReactionEmoji.ts similarity index 74% rename from helpers/messages/removeReactionEmoji.ts rename to helpers/messages/deleteReactionEmoji.ts index a7be2349c..a3edd9a1b 100644 --- a/helpers/messages/removeReactionEmoji.ts +++ b/helpers/messages/deleteReactionEmoji.ts @@ -1,14 +1,19 @@ /** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */ import type { Bot } from "../../bot.ts"; -export async function removeReactionEmoji(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { +export async function deleteReactionEmoji( + bot: Bot, + channelId: bigint, + messageId: bigint, + reaction: string, +): Promise { if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); } else if (reaction.startsWith("( + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction), diff --git a/helpers/messages/editMessage.ts b/helpers/messages/editMessage.ts index 912dffb90..770fab430 100644 --- a/helpers/messages/editMessage.ts +++ b/helpers/messages/editMessage.ts @@ -1,12 +1,13 @@ import type { Bot } from "../../bot.ts"; import { Attachment } from "../../transformers/attachment.ts"; import { Embed } from "../../transformers/embed.ts"; +import { Message } from "../../transformers/message.ts"; import { DiscordMessage } from "../../types/discord.ts"; import { AllowedMentions, FileContent, MessageComponents } from "../../types/discordeno.ts"; import { MessageComponentTypes } from "../../types/shared.ts"; /** Edit the message. */ -export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint, content: EditMessage) { +export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint, content: EditMessage): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/messages/getMessage.ts b/helpers/messages/getMessage.ts index e8e736cc9..68d610e1e 100644 --- a/helpers/messages/getMessage.ts +++ b/helpers/messages/getMessage.ts @@ -1,16 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { Message } from "../../transformers/message.ts"; import { DiscordMessage } from "../../types/discord.ts"; /** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ -export async function getMessage(bot: Bot, channelId: bigint, id: bigint) { +export async function getMessage(bot: Bot, channelId: bigint, id: bigint): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.CHANNEL_MESSAGE(channelId, id), ); - // If the message does not exist - if (!result?.id) return; - return bot.transformers.message(bot, result); } diff --git a/helpers/messages/getMessages.ts b/helpers/messages/getMessages.ts index 93968914e..20f9a1736 100644 --- a/helpers/messages/getMessages.ts +++ b/helpers/messages/getMessages.ts @@ -1,4 +1,5 @@ import type { Bot } from "../../bot.ts"; +import { Message } from "../../transformers/message.ts"; import { DiscordMessage } from "../../types/discord.ts"; import { Collection } from "../../util/collection.ts"; import { hasProperty } from "../../util/utils.ts"; @@ -8,21 +9,23 @@ export async function getMessages( bot: Bot, channelId: bigint, options?: GetMessagesOptions, -) { +): Promise> { if (options?.limit && (options.limit < 0 || options.limit > 100)) { throw new Error(bot.constants.Errors.INVALID_GET_MESSAGES_LIMIT); } - const result = await bot.rest.runMethod( + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.CHANNEL_MESSAGES(channelId, options), ); - return new Collection(result.map((res) => { - const msg = bot.transformers.message(bot, res); - return [msg.id, msg]; - })); + return new Collection( + results.map((result) => { + const message = bot.transformers.message(bot, result); + return [message.id, message]; + }), + ); } /** https://discord.com/developers/docs/resources/channel#get-channel-messages-query-string-params */ diff --git a/helpers/messages/getReactions.ts b/helpers/messages/getReactions.ts index 2969940de..2696b7bad 100644 --- a/helpers/messages/getReactions.ts +++ b/helpers/messages/getReactions.ts @@ -1,6 +1,7 @@ -import { Collection } from "../../util/collection.ts"; import type { Bot } from "../../bot.ts"; +import { User } from "../../transformers/member.ts"; import { DiscordUser } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Get a list of users that reacted with this emoji. */ export async function getReactions( @@ -9,23 +10,25 @@ export async function getReactions( messageId: bigint, reaction: string, options?: GetReactions, -) { +): Promise> { if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); } else if (reaction.startsWith("( + const results = await bot.rest.runMethod( bot.rest, "GET", bot.constants.routes.CHANNEL_MESSAGE_REACTION(channelId, messageId, encodeURIComponent(reaction), options), ); - return new Collection(users.map((u) => { - const user = bot.transformers.user(bot, u); - return [user.id, user]; - })); + return new Collection( + results.map((result) => { + const user = bot.transformers.user(bot, result); + return [user.id, user]; + }), + ); } /** https://discord.com/developers/docs/resources/channel#get-reactions-query-string-params */ diff --git a/helpers/messages/mod.ts b/helpers/messages/mod.ts index f42bc6f7b..dc2d87cda 100644 --- a/helpers/messages/mod.ts +++ b/helpers/messages/mod.ts @@ -1,15 +1,16 @@ export * from "./addReaction.ts"; export * from "./addReactions.ts"; +export * from "./deleteAllReactions.ts"; export * from "./deleteMessage.ts"; export * from "./deleteMessages.ts"; +export * from "./deleteReaction.ts"; +export * from "./deleteReactionEmoji.ts"; export * from "./editMessage.ts"; export * from "./getMessage.ts"; export * from "./getMessages.ts"; export * from "./getReactions.ts"; export * from "./pinMessage.ts"; export * from "./publishMessage.ts"; -export * from "./removeAllReactions.ts"; -export * from "./removeReaction.ts"; -export * from "./removeReactionEmoji.ts"; export * from "./sendMessage.ts"; export * from "./unpinMessage.ts"; + diff --git a/helpers/messages/pinMessage.ts b/helpers/messages/pinMessage.ts index 8665eeec8..dc02d0918 100644 --- a/helpers/messages/pinMessage.ts +++ b/helpers/messages/pinMessage.ts @@ -1,6 +1,6 @@ import type { Bot } from "../../bot.ts"; /** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */ -export async function pinMessage(bot: Bot, channelId: bigint, messageId: bigint) { - await bot.rest.runMethod(bot.rest, "PUT", bot.constants.routes.CHANNEL_PIN(channelId, messageId)); +export async function pinMessage(bot: Bot, channelId: bigint, messageId: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "PUT", bot.constants.routes.CHANNEL_PIN(channelId, messageId)); } diff --git a/helpers/messages/publishMessage.ts b/helpers/messages/publishMessage.ts index 803c339a8..b987a0372 100644 --- a/helpers/messages/publishMessage.ts +++ b/helpers/messages/publishMessage.ts @@ -1,13 +1,14 @@ import type { Bot } from "../../bot.ts"; +import { Message } from "../../transformers/message.ts"; import { DiscordMessage } from "../../types/discord.ts"; /** Crosspost a message in a News Channel to following channels. */ -export async function publishMessage(bot: Bot, channelId: bigint, messageId: bigint) { - const data = await bot.rest.runMethod( +export async function publishMessage(bot: Bot, channelId: bigint, messageId: bigint): Promise { + const result = await bot.rest.runMethod( bot.rest, "POST", bot.constants.routes.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId), ); - return bot.transformers.message(bot, data); + return bot.transformers.message(bot, result); } diff --git a/helpers/messages/removeAllReactions.ts b/helpers/messages/removeAllReactions.ts deleted file mode 100644 index 9c2f6a89d..000000000 --- a/helpers/messages/removeAllReactions.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { Bot } from "../../bot.ts"; - -/** Removes all reactions for all emojis on this message. */ -export async function removeAllReactions(bot: Bot, channelId: bigint, messageId: bigint) { - await bot.rest.runMethod( - bot.rest, - "DELETE", - bot.constants.routes.CHANNEL_MESSAGE_REACTIONS(channelId, messageId), - ); -} diff --git a/helpers/messages/sendMessage.ts b/helpers/messages/sendMessage.ts index df404cbf0..f0c77d9a1 100644 --- a/helpers/messages/sendMessage.ts +++ b/helpers/messages/sendMessage.ts @@ -1,11 +1,12 @@ import type { Bot } from "../../bot.ts"; -import { AllowedMentions, FileContent, MessageComponents } from "../../types/mod.ts"; -import { DiscordMessage } from "../../types/discord.ts"; -import { MessageComponentTypes } from "../../types/shared.ts"; import { Embed } from "../../transformers/embed.ts"; +import { Message } from "../../transformers/message.ts"; +import { DiscordMessage } from "../../types/discord.ts"; +import { AllowedMentions, FileContent, MessageComponents } from "../../types/mod.ts"; +import { MessageComponentTypes } from "../../types/shared.ts"; /** Send a message to the channel. Requires SEND_MESSAGES permission. */ -export async function sendMessage(bot: Bot, channelId: bigint, content: CreateMessage) { +export async function sendMessage(bot: Bot, channelId: bigint, content: CreateMessage): Promise { const result = await bot.rest.runMethod( bot.rest, "POST", diff --git a/helpers/messages/unpinMessage.ts b/helpers/messages/unpinMessage.ts index 1577d00b9..3b556fbe4 100644 --- a/helpers/messages/unpinMessage.ts +++ b/helpers/messages/unpinMessage.ts @@ -1,6 +1,10 @@ /** Unpin a message in a channel. Requires MANAGE_MESSAGES. */ import type { Bot } from "../../bot.ts"; -export async function unpinMessage(bot: Bot, channelId: bigint, messageId: bigint) { - await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.CHANNEL_PIN(channelId, messageId)); +export async function unpinMessage(bot: Bot, channelId: bigint, messageId: bigint): Promise { + return await bot.rest.runMethod( + bot.rest, + "DELETE", + bot.constants.routes.CHANNEL_PIN(channelId, messageId), + ); } diff --git a/helpers/misc/editBotProfile.ts b/helpers/misc/editBotProfile.ts index 8d1c1a0ad..d9c783eb7 100644 --- a/helpers/misc/editBotProfile.ts +++ b/helpers/misc/editBotProfile.ts @@ -1,10 +1,12 @@ import type { Bot } from "../../bot.ts"; +import { User } from "../../transformers/member.ts"; import { DiscordUser } from "../../types/discord.ts"; -/** Modifies the bot's username or avatar. +/** + * Modifies the bot's username or avatar. * NOTE: username: if changed may cause the bot's discriminator to be randomized. */ -export async function editBotProfile(bot: Bot, options: { username?: string; botAvatarURL?: string | null }) { +export async function editBotProfile(bot: Bot, options: { username?: string; botAvatarURL?: string | null }): Promise { const avatar = options?.botAvatarURL ? await bot.utils.urlToBase64(options?.botAvatarURL) : options?.botAvatarURL; const result = await bot.rest.runMethod(bot.rest, "PATCH", bot.constants.routes.USER_BOT(), { diff --git a/helpers/misc/editBotStatus.ts b/helpers/misc/editBotStatus.ts index 99448c3b8..21760795d 100644 --- a/helpers/misc/editBotStatus.ts +++ b/helpers/misc/editBotStatus.ts @@ -1,6 +1,6 @@ import { Bot } from "../../bot.ts"; import { StatusUpdate } from "./editShardStatus.ts"; -export async function editBotStatus(bot: Bot, data: StatusUpdate) { - await Promise.all(bot.gateway.manager.shards.map((shard) => bot.helpers.editShardStatus(shard.id, data))); +export async function editBotStatus(bot: Bot, data: StatusUpdate): Promise { + return void await Promise.all(bot.gateway.manager.shards.map((shard) => bot.helpers.editShardStatus(shard.id, data))); } diff --git a/helpers/misc/editShardStatus.ts b/helpers/misc/editShardStatus.ts index a5b2e2cee..12aee82f9 100644 --- a/helpers/misc/editShardStatus.ts +++ b/helpers/misc/editShardStatus.ts @@ -2,13 +2,13 @@ import type { Bot } from "../../bot.ts"; import { Activity } from "../../transformers/activity.ts"; import { GatewayOpcodes, PresenceStatus } from "../../types/shared.ts"; -export function editShardStatus(bot: Bot, shardId: number, data: StatusUpdate) { +export function editShardStatus(bot: Bot, shardId: number, data: StatusUpdate): Promise { const shard = bot.gateway.manager.shards.get(shardId); if (!shard) { throw new Error(`Shard (id: ${shardId}) not found.`); } - shard.send({ + return shard.send({ op: GatewayOpcodes.PresenceUpdate, d: { since: null, diff --git a/helpers/misc/getGatewayBot.ts b/helpers/misc/getGatewayBot.ts index 3cd5ee1c8..076fbe456 100644 --- a/helpers/misc/getGatewayBot.ts +++ b/helpers/misc/getGatewayBot.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { GetGatewayBot } from "../../transformers/gatewayBot.ts"; import { DiscordGetGatewayBot } from "../../types/discord.ts"; /** Get the bots Gateway metadata that can help during the operation of large or sharded bots. */ -export async function getGatewayBot(bot: Bot) { +export async function getGatewayBot(bot: Bot): Promise { const result = await bot.rest.runMethod(bot.rest, "GET", bot.constants.routes.GATEWAY_BOT()); return bot.transformers.gatewayBot(result); diff --git a/helpers/misc/getNitroStickerPacks.ts b/helpers/misc/getNitroStickerPacks.ts new file mode 100644 index 000000000..ab905be26 --- /dev/null +++ b/helpers/misc/getNitroStickerPacks.ts @@ -0,0 +1,20 @@ +import { Bot } from "../../bot.ts"; +import { StickerPack } from "../../transformers/sticker.ts"; +import { DiscordStickerPack } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; + +/** Returns the list of sticker packs available to Nitro subscribers. */ +export async function getNitroStickerPacks(bot: Bot): Promise> { + const results = await bot.rest.runMethod( + bot.rest, + "GET", + bot.constants.routes.NITRO_STICKER_PACKS(), + ); + + return new Collection( + results.map((result) => { + const pack = bot.transformers.stickerPack(bot, result); + return [pack.id, pack]; + }), + ); +} diff --git a/helpers/misc/getUser.ts b/helpers/misc/getUser.ts index 5e57051ff..dfda0b484 100644 --- a/helpers/misc/getUser.ts +++ b/helpers/misc/getUser.ts @@ -1,11 +1,10 @@ import type { Bot } from "../../bot.ts"; +import { User } from "../../transformers/member.ts"; import { DiscordUser } from "../../types/discord.ts"; /** This function will return the raw user payload in the rare cases you need to fetch a user directly from the API. */ -export async function getUser(bot: Bot, userId: bigint) { +export async function getUser(bot: Bot, userId: bigint): Promise { const result = await bot.rest.runMethod(bot.rest, "GET", bot.constants.routes.USER(userId)); - if (!result.id) return; - return bot.transformers.user(bot, result); } diff --git a/helpers/misc/mod.ts b/helpers/misc/mod.ts index f77958501..233bcf93f 100644 --- a/helpers/misc/mod.ts +++ b/helpers/misc/mod.ts @@ -2,5 +2,6 @@ export * from "./editBotProfile.ts"; export * from "./editBotStatus.ts"; export * from "./editShardStatus.ts"; export * from "./getGatewayBot.ts"; +export * from "./getNitroStickerPacks.ts"; export * from "./getUser.ts"; -export * from "./nitroStickerPacks.ts"; + diff --git a/helpers/misc/nitroStickerPacks.ts b/helpers/misc/nitroStickerPacks.ts deleted file mode 100644 index 9fa147329..000000000 --- a/helpers/misc/nitroStickerPacks.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Bot } from "../../bot.ts"; -import { DiscordStickerPack } from "../../types/discord.ts"; - -/** Returns the list of sticker packs available to Nitro subscribers. */ -export async function nitroStickerPacks(bot: Bot) { - const packs = await bot.rest.runMethod( - bot.rest, - "GET", - bot.constants.routes.NITRO_STICKER_PACKS(), - ); - - return packs.map((pack) => bot.transformers.stickerPack(bot, pack)); -} diff --git a/helpers/oauth/getApplicationInfo.ts b/helpers/oauth/getApplicationInfo.ts index 5c0c31eb4..14617eb32 100644 --- a/helpers/oauth/getApplicationInfo.ts +++ b/helpers/oauth/getApplicationInfo.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { Application } from "../../transformers/application.ts"; import { DiscordApplication } from "../../types/discord.ts"; /** Get the applications info */ -export async function getApplicationInfo(bot: Bot) { +export async function getApplicationInfo(bot: Bot): Promise { const result = await bot.rest.runMethod( bot.rest, "GET", diff --git a/helpers/roles/addRole.ts b/helpers/roles/addRole.ts index dad10f017..a73596149 100644 --- a/helpers/roles/addRole.ts +++ b/helpers/roles/addRole.ts @@ -1,8 +1,14 @@ import type { Bot } from "../../bot.ts"; /** Add a role to the member */ -export async function addRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { - await bot.rest.runMethod( +export async function addRole( + bot: Bot, + guildId: bigint, + memberId: bigint, + roleId: bigint, + reason?: string, +): Promise { + return await bot.rest.runMethod( bot.rest, "PUT", bot.constants.routes.GUILD_MEMBER_ROLE(guildId, memberId, roleId), diff --git a/helpers/roles/createRole.ts b/helpers/roles/createRole.ts index 4b342b6f0..42d28f9a0 100644 --- a/helpers/roles/createRole.ts +++ b/helpers/roles/createRole.ts @@ -1,9 +1,10 @@ import type { Bot } from "../../bot.ts"; +import { Role } from "../../transformers/role.ts"; import { DiscordRole } from "../../types/discord.ts"; import { PermissionStrings } from "../../types/shared.ts"; /** Create a new role for the guild. Requires the MANAGE_ROLES permission. */ -export async function createRole(bot: Bot, guildId: bigint, options: CreateGuildRole, reason?: string) { +export async function createRole(bot: Bot, guildId: bigint, options: CreateGuildRole, reason?: string): Promise { const result = await bot.rest.runMethod(bot.rest, "POST", bot.constants.routes.GUILD_ROLES(guildId), { name: options.name, color: options.color, diff --git a/helpers/roles/deleteRole.ts b/helpers/roles/deleteRole.ts index d680b576e..e2e38fb1c 100644 --- a/helpers/roles/deleteRole.ts +++ b/helpers/roles/deleteRole.ts @@ -1,6 +1,6 @@ import type { Bot } from "../../bot.ts"; /** Delete a guild role. Requires the MANAGE_ROLES permission. */ -export async function deleteRole(bot: Bot, guildId: bigint, id: bigint) { - await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_ROLE(guildId, id)); +export async function deleteRole(bot: Bot, guildId: bigint, id: bigint): Promise { + return await bot.rest.runMethod(bot.rest, "DELETE", bot.constants.routes.GUILD_ROLE(guildId, id)); } diff --git a/helpers/roles/editRole.ts b/helpers/roles/editRole.ts index bd7536aa0..047864585 100644 --- a/helpers/roles/editRole.ts +++ b/helpers/roles/editRole.ts @@ -1,9 +1,10 @@ import type { Bot } from "../../bot.ts"; +import { Role } from "../../transformers/role.ts"; import { DiscordRole } from "../../types/discord.ts"; import { PermissionStrings } from "../../types/shared.ts"; /** Edit a guild role. Requires the MANAGE_ROLES permission. */ -export async function editRole(bot: Bot, guildId: bigint, id: bigint, options: EditGuildRole) { +export async function editRole(bot: Bot, guildId: bigint, id: bigint, options: EditGuildRole): Promise { const result = await bot.rest.runMethod( bot.rest, "PATCH", diff --git a/helpers/roles/getRoles.ts b/helpers/roles/getRoles.ts index a622de83e..d0ec84fc2 100644 --- a/helpers/roles/getRoles.ts +++ b/helpers/roles/getRoles.ts @@ -1,15 +1,19 @@ import type { Bot } from "../../bot.ts"; -import { Collection } from "../../util/collection.ts"; +import { Role } from "../../transformers/role.ts"; import { DiscordRole } from "../../types/discord.ts"; +import { Collection } from "../../util/collection.ts"; /** Returns a list of role objects for the guild. * * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.** */ -export async function getRoles(bot: Bot, guildId: bigint) { - const result = await bot.rest.runMethod(bot.rest, "GET", bot.constants.routes.GUILD_ROLES(guildId)); +export async function getRoles(bot: Bot, guildId: bigint): Promise> { + const results = await bot.rest.runMethod(bot.rest, "GET", bot.constants.routes.GUILD_ROLES(guildId)); - const roleStructures = result.map((role) => bot.transformers.role(bot, { role, guildId })); - - return new Collection(roleStructures.map((role) => [role.id, role])); + return new Collection( + results.map((result) => { + const role = bot.transformers.role(bot, { role: result, guildId }); + return [role.id, role]; + }), + ); } diff --git a/helpers/roles/modifyRolePositions.ts b/helpers/roles/modifyRolePositions.ts index 00bedd164..138564f03 100644 --- a/helpers/roles/modifyRolePositions.ts +++ b/helpers/roles/modifyRolePositions.ts @@ -1,20 +1,27 @@ import { Bot } from "../../bot.ts"; +import { Role } from "../../transformers/role.ts"; import { DiscordRole } from "../../types/discord.ts"; import { Collection } from "../../util/collection.ts"; /** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. Returns a list of all of the guild's role objects on success. Fires multiple Guild Role Update Gateway events. */ -export async function modifyRolePositions(bot: Bot, guildId: bigint, options: ModifyRolePositions[]) { - const roles = await bot.rest.runMethod( +export async function modifyRolePositions( + bot: Bot, + guildId: bigint, + options: ModifyRolePositions[], +): Promise> { + const results = await bot.rest.runMethod( bot.rest, "PATCH", bot.constants.routes.GUILD_ROLES(guildId), options, ); - return new Collection(roles.map((role) => { - const result = bot.transformers.role(bot, { role, guildId }); - return [result.id, result]; - })); + return new Collection( + results.map((result) => { + const role = bot.transformers.role(bot, { role: result, guildId }); + return [role.id, role]; + }), + ); } export interface ModifyRolePositions { diff --git a/helpers/roles/removeRole.ts b/helpers/roles/removeRole.ts index cc929267e..3e9b87bdd 100644 --- a/helpers/roles/removeRole.ts +++ b/helpers/roles/removeRole.ts @@ -2,7 +2,7 @@ import type { Bot } from "../../bot.ts"; /** Remove a role from the member */ export async function removeRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { - await bot.rest.runMethod( + return await bot.rest.runMethod( bot.rest, "DELETE", bot.constants.routes.GUILD_MEMBER_ROLE(guildId, memberId, roleId), diff --git a/helpers/templates/createGuildFromTemplate.ts b/helpers/templates/createGuildFromTemplate.ts index 7c9ba4ef8..53ba7b473 100644 --- a/helpers/templates/createGuildFromTemplate.ts +++ b/helpers/templates/createGuildFromTemplate.ts @@ -1,11 +1,16 @@ import type { Bot } from "../../bot.ts"; +import { Guild } from "../../transformers/guild.ts"; import { DiscordGuild } from "../../types/discord.ts"; /** * Create a new guild based on a template * NOTE: This endpoint can be used only by bots in less than 10 guilds. */ -export async function createGuildFromTemplate(bot: Bot, templateCode: string, data: CreateGuildFromTemplate) { +export async function createGuildFromTemplate( + bot: Bot, + templateCode: string, + data: CreateGuildFromTemplate, +): Promise { if (data.icon) { data.icon = await bot.utils.urlToBase64(data.icon); } diff --git a/helpers/templates/createGuildTemplate.ts b/helpers/templates/createGuildTemplate.ts index 6bbcfee76..72cc42b7e 100644 --- a/helpers/templates/createGuildTemplate.ts +++ b/helpers/templates/createGuildTemplate.ts @@ -1,8 +1,9 @@ import type { Bot } from "../../bot.ts"; +import { Template } from "../../transformers/template.ts"; import { DiscordTemplate } from "../../types/discord.ts"; /** Creates a template for the guild. Requires the `MANAGE_GUILD` permission. */ -export async function createGuildTemplate(bot: Bot, guildId: bigint, data: CreateTemplate) { +export async function createGuildTemplate(bot: Bot, guildId: bigint, data: CreateTemplate): Promise