From 330c410796b806e208afd118d931a8501998fbb3 Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Sat, 22 Feb 2020 12:36:59 +0100 Subject: [PATCH] feat(Guild): add support for system channel flags (#3793) --- src/index.js | 1 + src/structures/Guild.js | 21 +++++++++++++++++++++ src/util/SystemChannelFlags.js | 31 +++++++++++++++++++++++++++++++ typings/index.d.ts | 12 ++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 src/util/SystemChannelFlags.js diff --git a/src/index.js b/src/index.js index 49e5f00e2..015e2b3ac 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ module.exports = { Permissions: require('./util/Permissions'), Snowflake: require('./util/Snowflake'), SnowflakeUtil: require('./util/Snowflake'), + SystemChannelFlags: require('./util/SystemChannelFlags'), Util: Util, util: Util, version: require('../package').version, diff --git a/src/structures/Guild.js b/src/structures/Guild.js index c5f1f3d0a..7ad241f89 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -10,6 +10,7 @@ const Constants = require('../util/Constants'); const Collection = require('../util/Collection'); const Util = require('../util/Util'); const Snowflake = require('../util/Snowflake'); +const SystemChannelFlags = require('../util/SystemChannelFlags'); /** * Represents a guild (or a server) on Discord. @@ -185,6 +186,12 @@ class Guild { this.defaultMessageNotifications = Constants.DefaultMessageNotifications[data.default_message_notifications] || data.default_message_notifications; + /** + * The value for the guild's system channel flags + * @type {Readonly} + */ + this.systemChannelFlags = new SystemChannelFlags(data.system_channel_flags).freeze(); + /** * The type of premium tier: * * 0: NONE @@ -891,6 +898,7 @@ class Guild { * @property {Base64Resolvable} [banner] The banner of the guild * @property {GuildMemberResolvable} [owner] The owner of the guild * @property {Base64Resolvable} [splash] The splash screen of the guild + * @property {SystemChannelFlagsResolvable} [systemChannelFlags] The system channel flags of the guild */ /** @@ -931,6 +939,9 @@ class Guild { Constants.DefaultMessageNotifications.indexOf(data.defaultMessageNotifications) : Number(data.defaultMessageNotifications); } + if (typeof data.systemChannelFlags !== 'undefined') { + _data.system_channel_flags = SystemChannelFlags.resolve(data.systemChannelFlags); + } return this.client.rest.methods.updateGuild(this, _data, reason); } @@ -965,6 +976,16 @@ class Guild { return this.edit({ defaultMessageNotifications }, reason); } + /** + * Edits the flags of the default message notifications of the guild. + * @param {SystemChannelFlagsResolvable} systemChannelFlags The new flags for the default message notifications + * @param {string} [reason] Reason for changing the flags of the default message notifications + * @returns {Promise} + */ + setSystemChannelFlags(systemChannelFlags, reason) { + return this.edit({ systemChannelFlags }, reason); + } + /** * Edit the name of the guild. * @param {string} name The new name of the guild diff --git a/src/util/SystemChannelFlags.js b/src/util/SystemChannelFlags.js new file mode 100644 index 000000000..9f749846c --- /dev/null +++ b/src/util/SystemChannelFlags.js @@ -0,0 +1,31 @@ +const BitField = require('./BitField'); + +/** + * Data structure that makes it easy to interact with a {@link Guild#systemChannelFlags} bitfield. + * Note that all event message types are enabled by default, + * and by setting their corresponding flags you are disabling them + * @extends {BitField} + */ +class SystemChannelFlags extends BitField {} + +/** + * Data that can be resolved to give a sytem channel flag bitfield. This can be: + * * A string (see {@link SystemChannelFlags.FLAGS}) + * * A sytem channel flag + * * An instance of SystemChannelFlags + * * An Array of SystemChannelFlagsResolvable + * @typedef {string|number|SystemChannelFlags|SystemChannelFlagsResolvable[]} SystemChannelFlagsResolvable + */ + +/** + * Numeric system channel flags. All available properties: + * * `WELCOME_MESSAGE_DISABLED` + * * `BOOST_MESSAGE_DISABLED` + * @type {Object} + */ +SystemChannelFlags.FLAGS = { + WELCOME_MESSAGE_DISABLED: 1 << 0, + BOOST_MESSAGE_DISABLED: 1 << 1, +}; + +module.exports = SystemChannelFlags; diff --git a/typings/index.d.ts b/typings/index.d.ts index 55a85ca64..f436b1347 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -556,6 +556,7 @@ declare module 'discord.js' { public readonly splashURL: string; public readonly suppressEveryone: boolean; public readonly systemChannel: GuildChannel; + public systemChannelFlags: Readonly; public systemChannelID: Snowflake; public vanityURLCode: string; public readonly verified: boolean; @@ -612,6 +613,7 @@ declare module 'discord.js' { public setRolePositions(rolePositions: RolePosition[]): Promise; public setSplash(splash: Base64Resolvable, reason?: string): Promise; public setSystemChannel(systemChannel: ChannelResolvable, reason?: string): Promise; + public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise; public setVerificationLevel(verificationLevel: number, reason?: string): Promise; public sync(): void; public toString(): string; @@ -1296,6 +1298,11 @@ declare module 'discord.js' { public setBitrate(bitrate: number | 'auto'): void; } + export class SystemChannelFlags extends BitField { + public static FLAGS: Record; + public static resolve(bit?: BitFieldResolvable): number; + } + export class Team { constructor(client: Client, data: object); public readonly client: Client; @@ -1967,6 +1974,7 @@ declare module 'discord.js' { explicitContentFilter?: number; afkChannel?: ChannelResolvable; systemChannel?: ChannelResolvable; + systemChannelFlags?: SystemChannelFlagsResolvable; afkTimeout?: number; banner?: Base64Resolvable; icon?: Base64Resolvable; @@ -2305,6 +2313,10 @@ declare module 'discord.js' { type StringResolvable = string | string[] | any; + type SystemChannelFlagsString = 'WELCOME_MESSAGE_DISABLED' | 'BOOST_MESSAGE_DISABLED'; + + type SystemChannelFlagsResolvable = BitFieldResolvable; + type UserResolvable = User | Snowflake | Message | Guild | GuildMember; type VoiceStatus = number;