diff --git a/src/api/controllers/misc.ts b/src/api/controllers/misc.ts index da19bad3b..f3ff65254 100644 --- a/src/api/controllers/misc.ts +++ b/src/api/controllers/misc.ts @@ -1,4 +1,4 @@ -import { eventHandlers, setBotID } from "../../bot.ts"; +import { eventHandlers, setApplicationID, setBotID } from "../../bot.ts"; import { DiscordPayload, PresenceUpdatePayload, @@ -24,6 +24,7 @@ export async function handleInternalReady( const payload = data.d as ReadyPayload; setBotID(payload.user.id); + setApplicationID(payload.application.id); // Triggered on each shard eventHandlers.shardReady?.(shardID); diff --git a/src/api/handlers/channel.ts b/src/api/handlers/channel.ts index 118ca4e34..25ee577cc 100644 --- a/src/api/handlers/channel.ts +++ b/src/api/handlers/channel.ts @@ -9,6 +9,7 @@ import { GetMessagesAfter, GetMessagesAround, GetMessagesBefore, + InvitePayload, MessageContent, MessageCreateOptions, Permission, @@ -19,6 +20,7 @@ import { import { endpoints } from "../../util/constants.ts"; import { botHasChannelPermissions, + botHasPermission, calculateBits, } from "../../util/permissions.ts"; import { cacheHandlers } from "../controllers/cache.ts"; @@ -301,6 +303,39 @@ export async function createInvite( return RequestManager.post(endpoints.CHANNEL_INVITES(channelID), options); } +/** Returns an invite for the given code. */ +export function getInvite(inviteCode: string) { + return RequestManager.get(endpoints.INVITE(inviteCode)) as Promise< + InvitePayload + >; +} + +/** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */ +export async function deleteInvite( + channelID: string, + inviteCode: string, +) { + const hasPerm = await botHasChannelPermissions(channelID, [ + "MANAGE_CHANNELS", + ]); + + if (!hasPerm) { + const channel = await cacheHandlers.get("channels", channelID); + + const hasManageGuildPerm = await botHasPermission(channel!.guildID, [ + "MANAGE_GUILD", + ]); + + if (!hasManageGuildPerm) { + throw new Error(Errors.MISSING_MANAGE_CHANNELS); + } + } + + return RequestManager.delete(endpoints.INVITE(inviteCode)) as Promise< + InvitePayload + >; +} + /** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */ export async function getChannelWebhooks(channelID: string) { const hasManageWebhooksPerm = await botHasChannelPermissions( diff --git a/src/api/handlers/guild.ts b/src/api/handlers/guild.ts index b70e532ab..c57d6c810 100644 --- a/src/api/handlers/guild.ts +++ b/src/api/handlers/guild.ts @@ -729,6 +729,11 @@ export function leaveGuild(guildID: string) { return RequestManager.delete(endpoints.GUILD_LEAVE(guildID)); } +/** Returns an array of voice regions that can be used when creating servers. */ +export function getAvailableVoiceRegions() { + return RequestManager.get(endpoints.VOICE_REGIONS); +} + /** 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 function getVoiceRegions(guildID: string) { return RequestManager.get(endpoints.GUILD_REGIONS(guildID)); @@ -767,13 +772,23 @@ export function getGuild(guildID: string, counts = true) { } /** Returns the guild template if it exists */ +export async function getTemplate(templateCode: string) { + const result = await RequestManager.get( + endpoints.GUILD_TEMPLATE(templateCode), + ) as GuildTemplate; + const template = await structures.createTemplate(result); + return template; +} + +/** + * Returns the guild template if it exists + * @deprecated will get removed in v11 use `getTemplate` instead + */ export function getGuildTemplate( guildID: string, templateCode: string, ) { - return RequestManager.get( - `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, - ) as Promise