Skip to content

Commit

Permalink
feat(Guild): add support for system channel flags (#3793)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC authored Feb 22, 2020
1 parent ab866d6 commit 330c410
Show file tree
Hide file tree
Showing 4 changed files with 65 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 @@ -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,
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 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.
Expand Down Expand Up @@ -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<SystemChannelFlags>}
*/
this.systemChannelFlags = new SystemChannelFlags(data.system_channel_flags).freeze();

/**
* The type of premium tier:
* * 0: NONE
Expand Down Expand Up @@ -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
*/

/**
Expand Down Expand Up @@ -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);
}

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

/**
* Edit the name of the guild.
* @param {string} name The new name of the guild
Expand Down
31 changes: 31 additions & 0 deletions src/util/SystemChannelFlags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;
12 changes: 12 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ declare module 'discord.js' {
public readonly splashURL: string;
public readonly suppressEveryone: boolean;
public readonly systemChannel: GuildChannel;
public systemChannelFlags: Readonly<SystemChannelFlags>;
public systemChannelID: Snowflake;
public vanityURLCode: string;
public readonly verified: boolean;
Expand Down Expand Up @@ -612,6 +613,7 @@ declare module 'discord.js' {
public setRolePositions(rolePositions: RolePosition[]): Promise<Guild>;
public setSplash(splash: Base64Resolvable, reason?: string): Promise<Guild>;
public setSystemChannel(systemChannel: ChannelResolvable, reason?: string): Promise<Guild>;
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
public setVerificationLevel(verificationLevel: number, reason?: string): Promise<Guild>;
public sync(): void;
public toString(): string;
Expand Down Expand Up @@ -1296,6 +1298,11 @@ declare module 'discord.js' {
public setBitrate(bitrate: number | 'auto'): void;
}

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

export class Team {
constructor(client: Client, data: object);
public readonly client: Client;
Expand Down Expand Up @@ -1967,6 +1974,7 @@ declare module 'discord.js' {
explicitContentFilter?: number;
afkChannel?: ChannelResolvable;
systemChannel?: ChannelResolvable;
systemChannelFlags?: SystemChannelFlagsResolvable;
afkTimeout?: number;
banner?: Base64Resolvable;
icon?: Base64Resolvable;
Expand Down Expand Up @@ -2305,6 +2313,10 @@ declare module 'discord.js' {

type StringResolvable = string | string[] | any;

type SystemChannelFlagsString = 'WELCOME_MESSAGE_DISABLED' | 'BOOST_MESSAGE_DISABLED';

type SystemChannelFlagsResolvable = BitFieldResolvable<SystemChannelFlagsString>;

type UserResolvable = User | Snowflake | Message | Guild | GuildMember;

type VoiceStatus = number;
Expand Down

0 comments on commit 330c410

Please sign in to comment.