Skip to content

Commit

Permalink
feat(Guild): add systemChannelFlags (#3559)
Browse files Browse the repository at this point in the history
* Add systemChannelFlags bitfield to Guild

* Implement @vladfrangu's suggestions

* fix: apply suggestions, reverse order of flags, reword docs

* docs: add SystemCHannelFlagsResolvable typedef

Co-authored-by: SpaceEEC <[email protected]>
  • Loading branch information
ottomated and SpaceEEC committed Dec 21, 2019
1 parent e13b3f5 commit f578cce
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
Snowflake: require('./util/Snowflake'),
SnowflakeUtil: require('./util/Snowflake'),
Structures: require('./util/Structures'),
SystemChannelFlags: require('./util/SystemChannelFlags'),
Util: Util,
util: Util,
version: require('../package.json').version,
Expand Down
21 changes: 21 additions & 0 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Collection = require('../util/Collection');
const Util = require('../util/Util');
const DataResolver = require('../util/DataResolver');
const Snowflake = require('../util/Snowflake');
const SystemChannelFlags = require('../util/SystemChannelFlags');
const GuildMemberStore = require('../stores/GuildMemberStore');
const RoleStore = require('../stores/RoleStore');
const GuildEmojiStore = require('../stores/GuildEmojiStore');
Expand Down Expand Up @@ -275,6 +276,12 @@ class Guild extends Base {
this.defaultMessageNotifications = DefaultMessageNotifications[data.default_message_notifications] ||
data.default_message_notifications;

/**
* The value set for the guild's system channel flags
* @type {Readonly<SystemChannelFlags>}
*/
this.systemChannelFlags = new SystemChannelFlags(data.system_channel_flags).freeze();

/**
* The maximum amount of members the guild can have
* <info>You will need to fetch the guild using {@link Guild#fetch} if you want to receive this parameter</info>
Expand Down Expand Up @@ -773,6 +780,7 @@ class Guild extends Base {
* @property {Base64Resolvable} [splash] The splash screen of the guild
* @property {Base64Resolvable} [banner] The banner of the guild
* @property {DefaultMessageNotifications|number} [defaultMessageNotifications] The default message notifications
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The system channel flags of the guild
*/

/**
Expand Down Expand Up @@ -813,6 +821,9 @@ class Guild extends Base {
DefaultMessageNotifications.indexOf(data.defaultMessageNotifications) :
Number(data.defaultMessageNotifications);
}
if (typeof data.systemChannelFlags !== 'undefined') {
_data.systemChannelFlags = SystemChannelFlags.resolve(data.systemChannelFlags);
}
return this.client.api.guilds(this.id).patch({ data: _data, reason })
.then(newData => this.client.actions.GuildUpdate.handle(newData).updated);
}
Expand All @@ -839,6 +850,16 @@ class Guild extends Base {
}
/* eslint-enable max-len */

/**
* 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<Guild>}
*/
setSystemChannelFlags(systemChannelFlags, reason) {
return this.edit({ systemChannelFlags }, reason);
}

/**
* Edits the name of the guild.
* @param {string} name The new name of the guild
Expand Down
33 changes: 33 additions & 0 deletions src/util/SystemChannelFlags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const BitField = require('./BitField');

/**
* Data structure that makes it easy to interact with a {@link Guild#systemChannelFlags} bitfield.
* <info>Note that all event message types are enabled by default,
* and by setting their corresponding flags you are disabling them</info>
* @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;
13 changes: 13 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ declare module 'discord.js' {
public shardID: number;
public splash: string | null;
public readonly systemChannel: TextChannel | null;
public systemChannelFlags: Readonly<SystemChannelFlags>;
public systemChannelID: Snowflake | null;
public vanityURLCode: string | null;
public verificationLevel: number;
Expand Down Expand Up @@ -755,6 +756,7 @@ declare module 'discord.js' {
public setRolePositions(rolePositions: RolePosition[]): Promise<Guild>;
public setSplash(splash: Base64Resolvable | null, reason?: string): Promise<Guild>;
public setSystemChannel(systemChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
public setVerificationLevel(verificationLevel: number, reason?: string): Promise<Guild>;
public splashURL(options?: AvatarOptions): string | null;
public toJSON(): object;
Expand Down Expand Up @@ -1368,6 +1370,11 @@ declare module 'discord.js' {
static extend<T extends Function>(structure: string, extender: (baseClass: typeof Function) => T): T;
}

export class SystemChannelFlags extends BitField<SystemChannelFlagsString> {
public static FLAGS: Record<SystemChannelFlagsString, number>;
public static resolve(bit?: BitFieldResolvable<SystemChannelFlagsString>): number;
}

export class TextChannel extends TextBasedChannel(GuildChannel) {
constructor(guild: Guild, data?: object);
public messages: MessageStore;
Expand Down Expand Up @@ -2285,6 +2292,7 @@ declare module 'discord.js' {
defaultMessageNotifications?: DefaultMessageNotifications | number;
afkChannel?: ChannelResolvable;
systemChannel?: ChannelResolvable;
systemChannelFlags?: SystemChannelFlags;
afkTimeout?: number;
icon?: Base64Resolvable;
owner?: GuildMemberResolvable;
Expand Down Expand Up @@ -2613,6 +2621,11 @@ declare module 'discord.js' {

type StringResolvable = string | string[] | any;

type SystemChannelFlagsString = 'WELCOME_MESSAGE_DISABLED'
| 'BOOST_MESSAGE_DISABLED';

type SystemChannelFlagsResolvable = BitFieldResolvable<SystemChannelFlagsString>;

type TargetUser = number;

type UserResolvable = User | Snowflake | Message | GuildMember;
Expand Down

0 comments on commit f578cce

Please sign in to comment.