From 78ae0bbb56485de81ef7a5059b7602b7a4126668 Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Sat, 16 Jan 2021 23:22:13 +0900 Subject: [PATCH] Make guild delete and remove delete function from BaseManager --- src/gateway/handlers/guildMemberRemove.ts | 2 +- src/gateway/handlers/messageDeleteBulk.ts | 2 +- src/gateway/handlers/messageReactionRemove.ts | 2 +- src/gateway/handlers/voiceStateUpdate.ts | 2 +- src/managers/base.ts | 5 ---- src/managers/guildChannels.ts | 2 +- src/managers/guildEmojis.ts | 2 +- src/managers/guilds.ts | 23 +++++++++++++++---- src/managers/memberRoles.ts | 2 +- src/structures/guild.ts | 6 +++++ src/types/guild.ts | 2 ++ 11 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/gateway/handlers/guildMemberRemove.ts b/src/gateway/handlers/guildMemberRemove.ts index f4b89cb..c4c574f 100644 --- a/src/gateway/handlers/guildMemberRemove.ts +++ b/src/gateway/handlers/guildMemberRemove.ts @@ -12,7 +12,7 @@ export const guildMemberRemove: GatewayEventHandler = async ( if (guild === undefined) return const member = await guild.members.get(d.user.id) - await guild.members.delete(d.user.id) + await guild.members._delete(d.user.id) if (member !== undefined) gateway.client.emit('guildMemberRemove', member) else { diff --git a/src/gateway/handlers/messageDeleteBulk.ts b/src/gateway/handlers/messageDeleteBulk.ts index d914911..245b6e5 100644 --- a/src/gateway/handlers/messageDeleteBulk.ts +++ b/src/gateway/handlers/messageDeleteBulk.ts @@ -25,7 +25,7 @@ export const messageDeleteBulk: GatewayEventHandler = async ( if (message === undefined) uncached.add(id) else { messages.set(id, message) - await channel.messages.delete(id) + await channel.messages._delete(id) } } diff --git a/src/gateway/handlers/messageReactionRemove.ts b/src/gateway/handlers/messageReactionRemove.ts index 77ca7df..658a1ce 100644 --- a/src/gateway/handlers/messageReactionRemove.ts +++ b/src/gateway/handlers/messageReactionRemove.ts @@ -31,7 +31,7 @@ export const messageReactionRemove: GatewayEventHandler = async ( const reaction = await message.reactions.get(emojiID) if (reaction === undefined) return - reaction.users.delete(d.user_id) + reaction.users._delete(d.user_id) gateway.client.emit('messageReactionRemove', reaction, user) } diff --git a/src/gateway/handlers/voiceStateUpdate.ts b/src/gateway/handlers/voiceStateUpdate.ts index 8a537b5..d3e52be 100644 --- a/src/gateway/handlers/voiceStateUpdate.ts +++ b/src/gateway/handlers/voiceStateUpdate.ts @@ -24,7 +24,7 @@ export const voiceStateUpdate: GatewayEventHandler = async ( return gateway.client.emit('voiceStateRemoveUncached', { guild, member }) } // No longer in the channel, so delete - await guild.voiceStates.delete(d.user_id) + await guild.voiceStates._delete(d.user_id) gateway.client.emit( 'voiceStateRemove', (voiceState as unknown) as VoiceState diff --git a/src/managers/base.ts b/src/managers/base.ts index 9d5bd7e..e522d31 100644 --- a/src/managers/base.ts +++ b/src/managers/base.ts @@ -41,11 +41,6 @@ export class BaseManager { return this.client.cache.delete(this.cacheName, key) } - /** Alias to _delete (cache) for compatibility purposes */ - async delete(key: string): Promise { - return await this._delete(key) - } - /** Gets an Array of values from Cache */ async array(): Promise { let arr = await (this.client.cache.array(this.cacheName) as T[]) diff --git a/src/managers/guildChannels.ts b/src/managers/guildChannels.ts index 0cbd52e..74cc8bf 100644 --- a/src/managers/guildChannels.ts +++ b/src/managers/guildChannels.ts @@ -57,7 +57,7 @@ export class GuildChannelsManager extends BaseChildManager< async flush(): Promise { const arr = await this.array() for (const elem of arr) { - this.parent.delete(elem.id) + this.parent._delete(elem.id) } return true } diff --git a/src/managers/guildEmojis.ts b/src/managers/guildEmojis.ts index bab24c9..cac97eb 100644 --- a/src/managers/guildEmojis.ts +++ b/src/managers/guildEmojis.ts @@ -88,7 +88,7 @@ export class GuildEmojisManager extends BaseChildManager { const arr = await this.array() for (const elem of arr) { const emojiID = elem.id !== null ? elem.id : elem.name - this.parent.delete(emojiID as string) + this.parent._delete(emojiID as string) } return true } diff --git a/src/managers/guilds.ts b/src/managers/guilds.ts index e0ffac5..84e82b1 100644 --- a/src/managers/guilds.ts +++ b/src/managers/guilds.ts @@ -137,17 +137,17 @@ export class GuildManager extends BaseManager { } async edit( - guildID: string, + guild: Guild | string, options: GuildModifyOptions, asRaw: false ): Promise async edit( - guildID: string, + guild: Guild | string, options: GuildModifyOptions, asRaw: true ): Promise async edit( - guildID: string, + guild: Guild | string, options: GuildModifyOptions, asRaw: boolean = false ): Promise { @@ -175,6 +175,9 @@ export class GuildManager extends BaseManager { ) { options.banner = await fetchAuto(options.banner) } + if (guild instanceof Guild) { + guild = guild.id + } const body: GuildModifyPayload = { name: options.name, @@ -184,6 +187,7 @@ export class GuildManager extends BaseManager { explicit_content_filter: options.explicitContentFilter, afk_channel_id: options.afkChannelID, afk_timeout: options.afkTimeout, + owner_id: options.ownerID, icon: options.icon, splash: options.splash, banner: options.banner, @@ -194,7 +198,7 @@ export class GuildManager extends BaseManager { } const result: GuildPayload = await this.client.rest.patch( - GUILD(guildID), + GUILD(guild), body ) @@ -205,4 +209,15 @@ export class GuildManager extends BaseManager { return result } } + + async delete(guild: Guild | string): Promise { + if (guild instanceof Guild) { + guild = guild.id + } + + const oldGuild = await this.get(guild) + + await this.client.rest.delete(GUILD(guild)) + return oldGuild + } } diff --git a/src/managers/memberRoles.ts b/src/managers/memberRoles.ts index a405340..7c43920 100644 --- a/src/managers/memberRoles.ts +++ b/src/managers/memberRoles.ts @@ -42,7 +42,7 @@ export class MemberRolesManager extends BaseChildManager { async flush(): Promise { const arr = await this.array() for (const elem of arr) { - this.parent.delete(elem.id) + this.parent._delete(elem.id) } return true } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 638baa2..7ab0102 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -342,6 +342,12 @@ export class Guild extends Base { return new Guild(this.client, result) } + + async delete(): Promise { + const result = await this.client.guilds.delete(this.id) + + return result === undefined ? this : result + } } export class GuildIntegration extends Base { diff --git a/src/types/guild.ts b/src/types/guild.ts index 6ca9a4e..77446f1 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -241,6 +241,7 @@ export interface GuildModifyPayload { afk_channel_id?: string | null afk_timeout?: number icon?: string | null + owner_id?: string splash?: string | null banner?: string | null system_channel_id?: string | null @@ -258,6 +259,7 @@ export interface GuildModifyOptions { afkChannelID?: string | null afkTimeout?: number icon?: string | null + ownerID?: string splash?: string | null banner?: string | null systemChannelID?: string | null