Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Miscellaneous backports #9769

Merged
merged 9 commits into from
Nov 5, 2023
16 changes: 6 additions & 10 deletions src/client/actions/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ class GenericAction {
}

getChannel(data) {
const payloadData = { recipients: data.recipients ?? [data.author ?? data.user ?? { id: data.user_id }] };
const id = data.channel_id ?? data.id;
if (id !== undefined) payloadData.id = id;
if ('guild_id' in data) payloadData.guild_id = data.guild_id;
if ('last_message_id' in data) payloadData.last_message_id = data.last_message_id;

return (
data[this.client.actions.injectedChannel] ??
this.getPayload(
{
id,
guild_id: data.guild_id,
recipients: [data.author ?? data.user ?? { id: data.user_id }],
},
this.client.channels,
id,
PartialTypes.CHANNEL,
)
this.getPayload(payloadData, this.client.channels, id, PartialTypes.CHANNEL)
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/client/websocket/handlers/GUILD_CREATE.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module.exports = (client, { d: data }, shard) => {
if (!guild.available && !data.unavailable) {
// A newly available guild
guild._patch(data);

/**
* Emitted whenever a guild becomes available.
* @event Client#guildAvailable
* @param {Guild} guild The guild that became available
*/
client.emit(Events.GUILD_AVAILABLE, guild);
}
} else {
// A new guild
Expand Down
35 changes: 32 additions & 3 deletions src/structures/ClientApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Permissions = require('../util/Permissions');
*/

/**
* Represents a Client OAuth2 Application.
* Represents a client application.
* @extends {Application}
*/
class ClientApplication extends Application {
Expand Down Expand Up @@ -69,6 +69,26 @@ class ClientApplication extends Application {
this.flags = new ApplicationFlags(data.flags).freeze();
}

if ('approximate_guild_count' in data) {
/**
* An approximate amount of guilds this application is in.
* @type {?number}
*/
this.approximateGuildCount = data.approximate_guild_count;
} else {
this.approximateGuildCount ??= null;
}

if ('guild_id' in data) {
/**
* The id of the guild associated with this application.
* @type {?Snowflake}
*/
this.guildId = data.guild_id;
} else {
this.guildId ??= null;
}

if ('cover_image' in data) {
/**
* The hash of the application's cover image
Expand Down Expand Up @@ -120,6 +140,15 @@ class ClientApplication extends Application {
: this.owner ?? null;
}

/**
* The guild associated with this application.
* @type {?Guild}
* @readonly
*/
get guild() {
return this.client.guilds.cache.get(this.guildId) ?? null;
}

/**
* Whether this application is partial
* @type {boolean}
Expand All @@ -134,8 +163,8 @@ class ClientApplication extends Application {
* @returns {Promise<ClientApplication>}
*/
async fetch() {
const app = await this.client.api.oauth2.applications('@me').get();
this._patch(app);
const data = await this.client.api.applications('@me').get();
this._patch(data);
return this;
}

Expand Down
10 changes: 9 additions & 1 deletion src/structures/ClientPresence.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ class ClientPresence extends Presence {
if (activities?.length) {
for (const [i, activity] of activities.entries()) {
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', `activities[${i}].name`, 'string');
activity.type ??= 0;

activity.type ??= ActivityTypes.PLAYING;

if (activity.type === ActivityTypes.CUSTOM && !activity.state) {
activity.state = activity.name;
activity.name = 'Custom Status';
}

data.activities.push({
type: typeof activity.type === 'number' ? activity.type : ActivityTypes[activity.type],
name: activity.name,
state: activity.state,
url: activity.url,
});
}
Expand All @@ -62,6 +69,7 @@ class ClientPresence extends Presence {
...this.activities.map(a => ({
name: a.name,
type: ActivityTypes[a.type],
state: a.state ?? undefined,
url: a.url ?? undefined,
})),
);
Expand Down
7 changes: 4 additions & 3 deletions src/structures/ClientUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class ClientUser extends User {
/**
* Options for setting activities
* @typedef {Object} ActivitiesOptions
* @property {string} [name] Name of the activity
* @property {string} name Name of the activity
* @property {string} [state] State of the activity
* @property {ActivityType|number} [type] Type of the activity
* @property {string} [url] Twitch / YouTube stream URL
*/
Expand Down Expand Up @@ -147,15 +148,15 @@ class ClientUser extends User {
/**
* Options for setting an activity.
* @typedef {Object} ActivityOptions
* @property {string} [name] Name of the activity
* @property {string} name Name of the activity
* @property {string} [url] Twitch / YouTube stream URL
* @property {ActivityType|number} [type] Type of the activity
* @property {number|number[]} [shardId] Shard Id(s) to have the activity set on
*/

/**
* Sets the activity the client user is playing.
* @param {string|ActivityOptions} [name] Activity being played, or options for setting the activity
* @param {string|ActivityOptions} name Activity being played, or options for setting the activity
* @param {ActivityOptions} [options] Options for setting the activity
* @returns {ClientPresence}
* @example
Expand Down
11 changes: 11 additions & 0 deletions src/structures/MessageAttachment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const AttachmentFlags = require('../util/AttachmentFlags');
const Util = require('../util/Util');

/**
Expand Down Expand Up @@ -169,6 +170,16 @@ class MessageAttachment {
} else {
this.waveform ??= null;
}

if ('flags' in data) {
/**
* The flags of this attachment
* @type {Readonly<AttachmentFlags>}
*/
this.flags = new AttachmentFlags(data.flags).freeze();
} else {
this.flags ??= new AttachmentFlags().freeze();
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const process = require('node:process');
const Base = require('./Base');
const { Error } = require('../errors');
const Permissions = require('../util/Permissions');
const RoleFlags = require('../util/RoleFlags');
const SnowflakeUtil = require('../util/SnowflakeUtil');

let deprecationEmittedForComparePositions = false;
Expand Down Expand Up @@ -142,6 +143,16 @@ class Role extends Base {
this.tags.guildConnections = true;
}
}

if ('flags' in data) {
/**
* The flags of this role
* @type {Readonly<RoleFlags>}
*/
this.flags = new RoleFlags(data.flags).freeze();
} else {
this.flags ??= new RoleFlags().freeze();
}
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/util/AttachmentFlags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

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

/**
* Data structure that makes it easy to interact with an {@link GuildMember#flags} bitfield.
* @extends {BitField}
*/
class AttachmentFlags extends BitField {}

/**
* @name AttachmentFlags
* @kind constructor
* @memberof AttachmentFlags
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
*/

/* eslint-disable max-len */
/**
* Numeric guild member flags. All available properties:
* * `IS_REMIX`
* @type {Object}
* @see {@link https://discord.com/developers/docs/resources/channel#attachment-object-attachment-structure-attachment-flags}
*/
AttachmentFlags.FLAGS = {
IS_REMIX: 1 << 2,
};

/**
* Data that can be resolved to give a guild attachment bitfield. This can be:
* * A string (see {@link AttachmentFlags.FLAGS})
* * A attachment flag
* * An instance of AttachmentFlags
* * An Array of AttachmentFlagsResolvable
* @typedef {string|number|AttachmentFlags|AttachmentFlagsResolvable[]} AttachmentFlagsResolvable
*/

module.exports = AttachmentFlags;
2 changes: 2 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
* * AUTO_MODERATION_RULE_CREATE: autoModerationRuleCreate
* * AUTO_MODERATION_RULE_DELETE: autoModerationRuleDelete
* * AUTO_MODERATION_RULE_UPDATE: autoModerationRuleUpdate
* * GUILD_AVAILABLE: guildAvailable
* * GUILD_CREATE: guildCreate
* * GUILD_DELETE: guildDelete
* * GUILD_UPDATE: guildUpdate
Expand Down Expand Up @@ -267,6 +268,7 @@
AUTO_MODERATION_RULE_CREATE: 'autoModerationRuleCreate',
AUTO_MODERATION_RULE_DELETE: 'autoModerationRuleDelete',
AUTO_MODERATION_RULE_UPDATE: 'autoModerationRuleUpdate',
GUILD_AVAILABLE: 'guildAvailable',
GUILD_CREATE: 'guildCreate',
GUILD_DELETE: 'guildDelete',
GUILD_UPDATE: 'guildUpdate',
Expand Down Expand Up @@ -535,7 +537,7 @@
'role_connections.write',
];

// TODO: change Integration#expireBehavior to this and clean up Integration

Check warning on line 540 in src/util/Constants.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: change Integration#expireBehavior...'
/**
* The behavior of expiring subscribers for Integrations. This can be:
* * REMOVE_ROLE
Expand Down
37 changes: 37 additions & 0 deletions src/util/RoleFlags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

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

/**
* Data structure that makes it easy to interact with an {@link GuildMember#flags} bitfield.
* @extends {BitField}
*/
class RoleFlags extends BitField {}

/**
* @name RoleFlags
* @kind constructor
* @memberof RoleFlags
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
*/

/**
* Numeric guild member flags. All available properties:
* * `IN_PROMPT`
* @type {Object}
* @see {@link https://discord.com/developers/docs/topics/permissions#role-object-role-flags}
*/
RoleFlags.FLAGS = {
IN_PROMPT: 1 << 0,
};

/**
* Data that can be resolved to give a role flag bitfield. This can be:
* * A string (see {@link RoleFlags.FLAGS})
* * A role flag
* * An instance of RoleFlags
* * An Array of RoleFlagsResolvable
* @typedef {string|number|RoleFlags|RoleFlagsResolvable[]} RoleFlagsResolvable
*/

module.exports = RoleFlags;
Loading
Loading