diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 2fb0db114..8612d300f 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -6,6 +6,7 @@ import { requestAllMembers } from "../module/shardingManager.ts"; import { Guild } from "../structures/guild.ts"; import { Member } from "../structures/member.ts"; import { structures } from "../structures/mod.ts"; +import { Template } from "../structures/template.ts"; import { ImageFormats, ImageSize } from "../types/cdn.ts"; import { ChannelCreatePayload, ChannelTypes } from "../types/channel.ts"; import { Errors } from "../types/errors.ts"; @@ -15,6 +16,7 @@ import { ChannelCreateOptions, CreateEmojisOptions, CreateGuildFromTemplate, + CreateGuildPayload, CreateGuildTemplate, CreateRoleOptions, CreateServerOptions, @@ -24,10 +26,10 @@ import { FetchMembersOptions, GetAuditLogsOptions, GuildEditOptions, + GuildTemplate, PositionSwap, PruneOptions, PrunePayload, - Template, UpdateGuildPayload, UserPayload, } from "../types/guild.ts"; @@ -645,7 +647,7 @@ export function getGuildTemplate( export async function createGuildFromTemplate( templateCode: string, data: CreateGuildFromTemplate, -): Promise { +) { if (cache.guilds.size >= 10) { throw new Error( "This function can only be used by bots in less than 10 guilds.", @@ -657,7 +659,7 @@ export async function createGuildFromTemplate( } try { - const guild = RequestManager.post( + const guild: CreateGuildPayload = await RequestManager.post( endpoints.GUILD_TEMPLATE(templateCode), data, ) as any; @@ -667,15 +669,24 @@ export async function createGuildFromTemplate( } } -/** Returns an array of guild templates */ +/** + * Returns an array of templates. + * Requires the `MANAGE_GUILD` permission. + */ export async function getGuildTemplates(guildID: string) { const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); - return RequestManager.get(endpoints.GUILD_TEMPLATES(guildID)); + const templates = await RequestManager.get( + endpoints.GUILD_TEMPLATES(guildID), + ) as GuildTemplate[]; + return templates.map((template) => structures.createTemplate(template)); } -/** Deletes a template from a guild */ +/** + * Deletes a template from a guild. + * Requires the `MANAGE_GUILD` permission. + */ export async function deleteGuildTemplate( guildID: string, templateCode: string, @@ -683,14 +694,15 @@ export async function deleteGuildTemplate( const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); - return RequestManager.delete( + const deletedTemplate: GuildTemplate = await RequestManager.delete( `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, - ); + ) as any; + return structures.createTemplate(deletedTemplate); } /** - * Creates a template for the guild - * @param guildID The ID of the guild to create a template in + * Creates a template for the guild. + * Requires the `MANAGE_GUILD` permission. * @param name name of the template (1-100 characters) * @param description description for the template (0-120 characters */ @@ -712,20 +724,31 @@ export async function createGuildTemplate( throw new Error("The description can only be in between 0-120 characters."); } - return RequestManager.post(endpoints.GUILD_TEMPLATES(guildID), data); + const template = await RequestManager.post( + endpoints.GUILD_TEMPLATES(guildID), + data, + ) as GuildTemplate; + return structures.createTemplate(template); } -/** Syncs the template to the guild's current state */ +/** + * Syncs the template to the guild's current state. + * Requires the `MANAGE_GUILD` permission. + */ export async function syncGuildTemplate(guildID: string, templateCode: string) { const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); - return RequestManager.put( + const template: GuildTemplate = await RequestManager.put( `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, - ); + ) as any; + return structures.createTemplate(template); } -/** Edit a template's metadata */ +/** + * Edit a template's metadata. + * Requires the `MANAGE_GUILD` permission. + */ export async function editGuildTemplate( guildID: string, templateCode: string, @@ -745,8 +768,9 @@ export async function editGuildTemplate( throw new Error("The description can only be in between 0-120 characters."); } - return RequestManager.patch( + const template: GuildTemplate = await RequestManager.patch( `${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`, data, - ); + ) as any; + return structures.createTemplate(template); } diff --git a/src/structures/mod.ts b/src/structures/mod.ts index d69713721..abbdea3b7 100644 --- a/src/structures/mod.ts +++ b/src/structures/mod.ts @@ -3,6 +3,7 @@ import { createGuild } from "./guild.ts"; import { createMember } from "./member.ts"; import { createMessage } from "./message.ts"; import { createRole } from "./role.ts"; +import { createTemplate } from "./template.ts"; /** This is the placeholder where the structure creation functions are kept. */ export let structures = { @@ -11,6 +12,7 @@ export let structures = { createMember, createMessage, createRole, + createTemplate, }; export type Structures = typeof structures; diff --git a/src/structures/template.ts b/src/structures/template.ts new file mode 100644 index 000000000..59d84a99a --- /dev/null +++ b/src/structures/template.ts @@ -0,0 +1,31 @@ +import { GuildTemplate } from "../types/guild.ts"; + +export function createTemplate( + data: GuildTemplate, +) { + const { + usage_count: usageCount, + creator_id: creatorID, + created_at: createdAt, + updated_at: updatedAt, + source_guild_id: sourceGuildID, + serialized_source_guild: serializedSourceGuild, + is_dirty: isDirty, + ...rest + } = data; + + const template = { + ...rest, + usageCount, + creatorID, + createdAt, + updatedAt, + sourceGuildID, + serializedSourceGuild, + isDirty, + }; + + return template; +} + +export interface Template extends ReturnType {} diff --git a/src/types/guild.ts b/src/types/guild.ts index 96402bd89..315d03454 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -609,7 +609,7 @@ export interface CreateServerOptions { } // https://discord.com/developers/docs/resources/template#template-object -export interface Template { +export interface GuildTemplate { /** the template code (unique ID) */ code: string; /** template name */