From 19e2020e386106663c0746effa87fdb420e420c0 Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Sat, 16 Jan 2021 02:15:24 +0900 Subject: [PATCH] Add guild edit feature, fix some types --- src/managers/guilds.ts | 74 ++++++++++++++++++++++++++++++++++++++++- src/structures/guild.ts | 16 +++++++-- src/types/guild.ts | 40 ++++++++++++++++++++-- 3 files changed, 123 insertions(+), 7 deletions(-) diff --git a/src/managers/guilds.ts b/src/managers/guilds.ts index b9f6ebd..e0ffac5 100644 --- a/src/managers/guilds.ts +++ b/src/managers/guilds.ts @@ -12,7 +12,9 @@ import { GuildCreateChannelOptions, GuildCreateChannelPayload, GuildPreview, - GuildPreviewPayload + GuildPreviewPayload, + GuildModifyOptions, + GuildModifyPayload } from '../types/guild.ts' import { BaseManager } from './base.ts' import { MembersManager } from './members.ts' @@ -133,4 +135,74 @@ export class GuildManager extends BaseManager { return result } + + async edit( + guildID: string, + options: GuildModifyOptions, + asRaw: false + ): Promise + async edit( + guildID: string, + options: GuildModifyOptions, + asRaw: true + ): Promise + async edit( + guildID: string, + options: GuildModifyOptions, + asRaw: boolean = false + ): Promise { + if ( + options.icon !== undefined && + options.icon !== null && + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + !options.icon.startsWith('data:') + ) { + options.icon = await fetchAuto(options.icon) + } + if ( + options.splash !== undefined && + options.splash !== null && + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + !options.splash.startsWith('data:') + ) { + options.splash = await fetchAuto(options.splash) + } + if ( + options.banner !== undefined && + options.banner !== null && + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + !options.banner.startsWith('data:') + ) { + options.banner = await fetchAuto(options.banner) + } + + const body: GuildModifyPayload = { + name: options.name, + region: options.region, + verification_level: options.verificationLevel, + default_message_notifications: options.defaultMessageNotifications, + explicit_content_filter: options.explicitContentFilter, + afk_channel_id: options.afkChannelID, + afk_timeout: options.afkTimeout, + icon: options.icon, + splash: options.splash, + banner: options.banner, + system_channel_id: options.systemChannelID, + rules_channel_id: options.rulesChannelID, + public_updates_channel_id: options.publicUpdatesChannelID, + preferred_locale: options.preferredLocale + } + + const result: GuildPayload = await this.client.rest.patch( + GUILD(guildID), + body + ) + + if (asRaw) { + const guild = new Guild(this.client, result) + return guild + } else { + return result + } + } } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 7fdde23..638baa2 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -8,7 +8,10 @@ import { IntegrationExpireBehavior, Verification, GuildChannels, - GuildPreview + GuildPreview, + MessageNotification, + ContentFilter, + GuildModifyOptions } from '../types/guild.ts' import { Base } from './base.ts' import { CreateGuildRoleOptions, RolesManager } from '../managers/roles.ts' @@ -134,8 +137,8 @@ export class Guild extends Base { widgetEnabled?: boolean widgetChannelID?: string verificationLevel?: Verification - defaultMessageNotifications?: number - explicitContentFilter?: number + defaultMessageNotifications?: MessageNotification + explicitContentFilter?: ContentFilter roles: RolesManager emojis: GuildEmojisManager invites: InviteManager @@ -332,6 +335,13 @@ export class Guild extends Base { async preview(): Promise { return this.client.guilds.preview(this.id) } + + async edit(options: GuildModifyOptions): Promise { + const result = await this.client.guilds.edit(this.id, options, true) + this.readFromData(result) + + return new Guild(this.client, result) + } } export class GuildIntegration extends Base { diff --git a/src/types/guild.ts b/src/types/guild.ts index 66b40eb..6ca9a4e 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -32,8 +32,8 @@ export interface GuildPayload { widget_enabled?: boolean widget_channel_id?: string verification_level: Verification - default_message_notifications: number - explicit_content_filter: number + default_message_notifications: MessageNotification + explicit_content_filter: ContentFilter roles: RolePayload[] emojis: EmojiPayload[] features: GuildFeatures[] @@ -83,7 +83,7 @@ export enum MessageNotification { export enum ContentFilter { DISABLED = 0, MEMBERS_WITHOUT_ROLES = 1, - ALL_MEMBERS = 3 + ALL_MEMBERS = 2 } export enum MFA { @@ -231,3 +231,37 @@ export interface GuildPreview { approximatePresenceCount: number description: string | null } + +export interface GuildModifyPayload { + name?: string + region?: string | null + verification_level?: Verification | null + default_message_notifications?: MessageNotification | null + explicit_content_filter?: ContentFilter | null + afk_channel_id?: string | null + afk_timeout?: number + icon?: string | null + splash?: string | null + banner?: string | null + system_channel_id?: string | null + rules_channel_id?: string | null + public_updates_channel_id?: string | null + preferred_locale?: string | null +} + +export interface GuildModifyOptions { + name?: string + region?: string | null + verificationLevel?: Verification | null + defaultMessageNotifications?: MessageNotification | null + explicitContentFilter?: ContentFilter | null + afkChannelID?: string | null + afkTimeout?: number + icon?: string | null + splash?: string | null + banner?: string | null + systemChannelID?: string | null + rulesChannelID?: string | null + publicUpdatesChannelID?: string | null + preferredLocale?: string | null +}