From 38ef5d91cf9ff5094ec7719646c7a3b9475128a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Mon, 15 Apr 2024 14:48:44 -0400 Subject: [PATCH] fix: some shorters doesn't return structs (#183) --- src/common/shorters/guilds.ts | 24 ++++++++++++++++++---- src/common/shorters/roles.ts | 29 +++++++++++---------------- src/common/shorters/templates.ts | 34 ++++++++++++++++++++++++++------ src/common/shorters/webhook.ts | 10 ++++++++-- 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/src/common/shorters/guilds.ts b/src/common/shorters/guilds.ts index c674e09..a0b06fb 100644 --- a/src/common/shorters/guilds.ts +++ b/src/common/shorters/guilds.ts @@ -19,6 +19,7 @@ import { GuildMember, Sticker, type CreateStickerBodyRequest, + AutoModerationRule, } from '../../structures'; import channelFrom from '../../structures/channels'; import { BaseShorter } from './base'; @@ -197,7 +198,11 @@ export class GuildShorter extends BaseShorter { * @param guildId The ID of the guild. * @returns A Promise that resolves to an array of auto-moderation rules. */ - list: (guildId: string) => this.client.proxy.guilds(guildId)['auto-moderation'].rules.get(), + list: (guildId: string) => + this.client.proxy + .guilds(guildId) + ['auto-moderation'].rules.get() + .then(rules => rules.map(rule => new AutoModerationRule(this.client, rule))), /** * Creates a new auto-moderation rule in the guild. @@ -206,7 +211,10 @@ export class GuildShorter extends BaseShorter { * @returns A Promise that resolves to the created auto-moderation rule. */ create: (guildId: string, body: RESTPostAPIAutoModerationRuleJSONBody) => - this.client.proxy.guilds(guildId)['auto-moderation'].rules.post({ body }), + this.client.proxy + .guilds(guildId) + ['auto-moderation'].rules.post({ body }) + .then(rule => new AutoModerationRule(this.client, rule)), /** * Deletes an auto-moderation rule from the guild. @@ -226,7 +234,11 @@ export class GuildShorter extends BaseShorter { * @returns A Promise that resolves to the fetched auto-moderation rule. */ fetch: (guildId: string, ruleId: string) => { - return this.client.proxy.guilds(guildId)['auto-moderation'].rules(ruleId).get(); + return this.client.proxy + .guilds(guildId) + ['auto-moderation'].rules(ruleId) + .get() + .then(rule => new AutoModerationRule(this.client, rule)); }, /** @@ -243,7 +255,11 @@ export class GuildShorter extends BaseShorter { body: ObjectToLower, reason?: string, ) => { - return this.client.proxy.guilds(guildId)['auto-moderation'].rules(ruleId).patch({ body, reason }); + return this.client.proxy + .guilds(guildId) + ['auto-moderation'].rules(ruleId) + .patch({ body, reason }) + .then(rule => new AutoModerationRule(this.client, rule)); }, }; } diff --git a/src/common/shorters/roles.ts b/src/common/shorters/roles.ts index caf8656..be27175 100644 --- a/src/common/shorters/roles.ts +++ b/src/common/shorters/roles.ts @@ -15,11 +15,10 @@ export class RoleShorter extends BaseShorter { * @param reason The reason for creating the role. * @returns A Promise that resolves when the role is created. */ - create(guildId: string, body: RESTPostAPIGuildRoleJSONBody, reason?: string) { - return this.client.proxy - .guilds(guildId) - .roles.post({ body, reason }) - .then(res => this.client.cache.roles?.setIfNI('Guilds', res.id, guildId, res)); + async create(guildId: string, body: RESTPostAPIGuildRoleJSONBody, reason?: string) { + const res = await this.client.proxy.guilds(guildId).roles.post({ body, reason }); + await this.client.cache.roles?.setIfNI('Guilds', res.id, guildId, res); + return new GuildRole(this.client, res, guildId); } /** @@ -52,12 +51,10 @@ export class RoleShorter extends BaseShorter { * @param reason The reason for editing the role. * @returns A Promise that resolves when the role is edited. */ - edit(guildId: string, roleId: string, body: RESTPatchAPIGuildRoleJSONBody, reason?: string) { - return this.client.proxy - .guilds(guildId) - .roles(roleId) - .patch({ body, reason }) - .then(res => this.client.cache.roles?.setIfNI('Guilds', roleId, guildId, res)); + async edit(guildId: string, roleId: string, body: RESTPatchAPIGuildRoleJSONBody, reason?: string) { + const res = await this.client.proxy.guilds(guildId).roles(roleId).patch({ body, reason }); + await this.client.cache.roles?.setIfNI('Guilds', roleId, guildId, res); + return new GuildRole(this.client, res, guildId); } /** @@ -67,12 +64,10 @@ export class RoleShorter extends BaseShorter { * @param reason The reason for deleting the role. * @returns A Promise that resolves when the role is deleted. */ - delete(guildId: string, roleId: string, reason?: string) { - return this.client.proxy - .guilds(guildId) - .roles(roleId) - .delete({ reason }) - .then(() => this.client.cache.roles?.removeIfNI('Guilds', roleId, guildId)); + async delete(guildId: string, roleId: string, reason?: string) { + const res = await this.client.proxy.guilds(guildId).roles(roleId).delete({ reason }); + this.client.cache.roles?.removeIfNI('Guilds', roleId, guildId); + return new GuildRole(this.client, res, guildId); } /** diff --git a/src/common/shorters/templates.ts b/src/common/shorters/templates.ts index 8702249..bf60760 100644 --- a/src/common/shorters/templates.ts +++ b/src/common/shorters/templates.ts @@ -1,28 +1,50 @@ import type { RESTPatchAPIGuildTemplateJSONBody, RESTPostAPIGuildTemplatesJSONBody } from 'discord-api-types/v10'; import { BaseShorter } from './base'; +import { GuildTemplate } from '../..'; export class TemplateShorter extends BaseShorter { fetch(code: string) { - return this.client.proxy.guilds.templates(code).get(); + return this.client.proxy.guilds + .templates(code) + .get() + .then(template => new GuildTemplate(this.client, template)); } list(guildId: string) { - return this.client.proxy.guilds(guildId).templates.get(); + return this.client.proxy + .guilds(guildId) + .templates.get() + .then(list => list.map(template => new GuildTemplate(this.client, template))); } create(guildId: string, body: RESTPostAPIGuildTemplatesJSONBody) { - return this.client.proxy.guilds(guildId).templates.post({ body }); + return this.client.proxy + .guilds(guildId) + .templates.post({ body }) + .then(template => new GuildTemplate(this.client, template)); } sync(guildId: string, code: string) { - return this.client.proxy.guilds(guildId).templates(code).put({}); + return this.client.proxy + .guilds(guildId) + .templates(code) + .put({}) + .then(template => new GuildTemplate(this.client, template)); } edit(guildId: string, code: string, body: RESTPatchAPIGuildTemplateJSONBody) { - return this.client.proxy.guilds(guildId).templates(code).patch({ body }); + return this.client.proxy + .guilds(guildId) + .templates(code) + .patch({ body }) + .then(template => new GuildTemplate(this.client, template)); } delete(guildId: string, code: string) { - return this.client.proxy.guilds(guildId).templates(code).delete(); + return this.client.proxy + .guilds(guildId) + .templates(code) + .delete() + .then(template => new GuildTemplate(this.client, template)); } } diff --git a/src/common/shorters/webhook.ts b/src/common/shorters/webhook.ts index 44c19ba..d941818 100644 --- a/src/common/shorters/webhook.ts +++ b/src/common/shorters/webhook.ts @@ -47,9 +47,15 @@ export class WebhookShorter extends BaseShorter { options: WebhookShorterOptionalParams, ) { if (options.token) { - return this.client.proxy.webhooks(webhookId)(options.token).patch({ body, reason: options.reason, auth: false }); + return this.client.proxy + .webhooks(webhookId)(options.token) + .patch({ body, reason: options.reason, auth: false }) + .then(webhook => new Webhook(this.client, webhook)); } - return this.client.proxy.webhooks(webhookId).patch({ body, reason: options.reason }); + return this.client.proxy + .webhooks(webhookId) + .patch({ body, reason: options.reason }) + .then(webhook => new Webhook(this.client, webhook)); } /**