mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
feat(Application): application flags (#5147)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com> Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
@@ -10,7 +10,6 @@ const ChannelManager = require('../managers/ChannelManager');
|
||||
const GuildManager = require('../managers/GuildManager');
|
||||
const UserManager = require('../managers/UserManager');
|
||||
const ShardClientUtil = require('../sharding/ShardClientUtil');
|
||||
const ClientApplication = require('../structures/ClientApplication');
|
||||
const GuildPreview = require('../structures/GuildPreview');
|
||||
const GuildTemplate = require('../structures/GuildTemplate');
|
||||
const Invite = require('../structures/Invite');
|
||||
@@ -151,6 +150,12 @@ class Client extends BaseClient {
|
||||
*/
|
||||
this.user = null;
|
||||
|
||||
/**
|
||||
* The application of this bot
|
||||
* @type {?ClientApplication}
|
||||
*/
|
||||
this.application = null;
|
||||
|
||||
/**
|
||||
* Time at which the client was last regarded as being in the `READY` state
|
||||
* (each time the client disconnects and successfully reconnects, this will be overwritten)
|
||||
@@ -346,17 +351,6 @@ class Client extends BaseClient {
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the OAuth Application of this bot from Discord.
|
||||
* @returns {Promise<ClientApplication>}
|
||||
*/
|
||||
fetchApplication() {
|
||||
return this.api.oauth2
|
||||
.applications('@me')
|
||||
.get()
|
||||
.then(app => new ClientApplication(this, app));
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a guild preview from Discord, available for all guilds the bot is in and all Discoverable guilds.
|
||||
* @param {GuildResolvable} guild The guild to fetch the preview for
|
||||
@@ -383,24 +377,23 @@ class Client extends BaseClient {
|
||||
/**
|
||||
* Generates a link that can be used to invite the bot to a guild.
|
||||
* @param {InviteGenerationOptions} [options={}] Options for the invite
|
||||
* @returns {Promise<string>}
|
||||
* @returns {string}
|
||||
* @example
|
||||
* client.generateInvite({
|
||||
* const link = client.generateInvite({
|
||||
* permissions: [
|
||||
* Permissions.FLAGS.SEND_MESSAGES,
|
||||
* Permissions.FLAGS.MANAGE_GUILD,
|
||||
* Permissions.FLAGS.MENTION_EVERYONE,
|
||||
* ],
|
||||
* })
|
||||
* .then(link => console.log(`Generated bot invite link: ${link}`))
|
||||
* .catch(console.error);
|
||||
* });
|
||||
* console.log(`Generated bot invite link: ${link}`);
|
||||
*/
|
||||
async generateInvite(options = {}) {
|
||||
generateInvite(options = {}) {
|
||||
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
|
||||
if (!this.application) throw new Error('CLIENT_NOT_READY', 'generate an invite link');
|
||||
|
||||
const application = await this.fetchApplication();
|
||||
const query = new URLSearchParams({
|
||||
client_id: application.id,
|
||||
client_id: this.application.id,
|
||||
scope: 'bot',
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const ClientApplication = require('../../../structures/ClientApplication');
|
||||
let ClientUser;
|
||||
|
||||
module.exports = (client, { d: data }, shard) => {
|
||||
@@ -7,9 +8,8 @@ module.exports = (client, { d: data }, shard) => {
|
||||
client.user._patch(data.user);
|
||||
} else {
|
||||
if (!ClientUser) ClientUser = require('../../../structures/ClientUser');
|
||||
const clientUser = new ClientUser(client, data.user);
|
||||
client.user = clientUser;
|
||||
client.users.cache.set(clientUser.id, clientUser);
|
||||
client.user = new ClientUser(client, data.user);
|
||||
client.users.cache.set(client.user.id, client.user);
|
||||
}
|
||||
|
||||
for (const guild of data.guilds) {
|
||||
@@ -17,5 +17,11 @@ module.exports = (client, { d: data }, shard) => {
|
||||
client.guilds.add(guild);
|
||||
}
|
||||
|
||||
if (client.application) {
|
||||
client.application._patch(data.application);
|
||||
} else {
|
||||
client.application = new ClientApplication(client, data.application);
|
||||
}
|
||||
|
||||
shard.checkReady();
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@ const Messages = {
|
||||
CLIENT_INVALID_OPTION: (prop, must) => `The ${prop} option must be ${must}`,
|
||||
CLIENT_INVALID_PROVIDED_SHARDS: 'None of the provided shards were valid.',
|
||||
CLIENT_MISSING_INTENTS: 'Valid intents must be provided for the Client.',
|
||||
CLIENT_NOT_READY: action => `The client needs to be logged in to ${action}.`,
|
||||
|
||||
TOKEN_INVALID: 'An invalid token was provided.',
|
||||
TOKEN_MISSING: 'Request to use token, but token was unavailable to the client.',
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
const Team = require('./Team');
|
||||
const Application = require('./interfaces/Application');
|
||||
const ApplicationFlags = require('../util/ApplicationFlags');
|
||||
|
||||
/**
|
||||
* Represents a Client OAuth2 Application.
|
||||
@@ -11,35 +12,64 @@ class ClientApplication extends Application {
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
/**
|
||||
* The flags this application has
|
||||
* @type {ApplicationFlags}
|
||||
*/
|
||||
this.flags = 'flags' in data ? new ApplicationFlags(data.flags) : this.flags;
|
||||
|
||||
/**
|
||||
* The app's cover image
|
||||
* @type {?string}
|
||||
*/
|
||||
this.cover = data.cover_image || null;
|
||||
this.cover = data.cover_image ?? this.cover ?? null;
|
||||
|
||||
/**
|
||||
* The app's RPC origins, if enabled
|
||||
* @type {string[]}
|
||||
*/
|
||||
this.rpcOrigins = data.rpc_origins || [];
|
||||
this.rpcOrigins = data.rpc_origins ?? this.rpcOrigins ?? [];
|
||||
|
||||
/**
|
||||
* If this app's bot requires a code grant when using the OAuth2 flow
|
||||
* @type {?boolean}
|
||||
*/
|
||||
this.botRequireCodeGrant = typeof data.bot_require_code_grant !== 'undefined' ? data.bot_require_code_grant : null;
|
||||
this.botRequireCodeGrant = data.bot_require_code_grant ?? this.botRequireCodeGrant ?? null;
|
||||
|
||||
/**
|
||||
* If this app's bot is public
|
||||
* @type {?boolean}
|
||||
*/
|
||||
this.botPublic = typeof data.bot_public !== 'undefined' ? data.bot_public : null;
|
||||
this.botPublic = data.bot_public ?? this.botPublic ?? null;
|
||||
|
||||
/**
|
||||
* The owner of this OAuth application
|
||||
* @type {?User|Team}
|
||||
*/
|
||||
this.owner = data.team ? new Team(this.client, data.team) : data.owner ? this.client.users.add(data.owner) : null;
|
||||
this.owner = data.team
|
||||
? new Team(this.client, data.team)
|
||||
: data.owner
|
||||
? this.client.users.add(data.owner)
|
||||
: this.owner ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this application is partial
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get partial() {
|
||||
return !this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains this application from Discord.
|
||||
* @returns {Promise<ClientApplication>}
|
||||
*/
|
||||
async fetch() {
|
||||
const app = await this.client.api.oauth2.applications('@me').get();
|
||||
this._patch(app);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,15 +10,11 @@ class IntegrationApplication extends Application {
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
if (typeof data.bot !== 'undefined') {
|
||||
/**
|
||||
* The bot {@link User user} for this application
|
||||
* @type {?User}
|
||||
*/
|
||||
this.bot = this.client.users.add(data.bot);
|
||||
} else if (!this.bot) {
|
||||
this.bot = null;
|
||||
}
|
||||
/**
|
||||
* The bot user for this application
|
||||
* @type {?User}
|
||||
*/
|
||||
this.bot = data.bot ? this.client.users.add(data.bot) : this.bot ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,21 +25,21 @@ class Application extends Base {
|
||||
|
||||
/**
|
||||
* The name of the app
|
||||
* @type {string}
|
||||
* @type {?string}
|
||||
*/
|
||||
this.name = data.name;
|
||||
this.name = data.name ?? this.name ?? null;
|
||||
|
||||
/**
|
||||
* The app's description
|
||||
* @type {string}
|
||||
* @type {?string}
|
||||
*/
|
||||
this.description = data.description;
|
||||
this.description = data.description ?? this.description ?? null;
|
||||
|
||||
/**
|
||||
* The app's icon hash
|
||||
* @type {string}
|
||||
* @type {?string}
|
||||
*/
|
||||
this.icon = data.icon;
|
||||
this.icon = data.icon ?? this.icon ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +108,7 @@ class Application extends Base {
|
||||
/**
|
||||
* When concatenated with a string, this automatically returns the application's name instead of the
|
||||
* Oauth2Application object.
|
||||
* @returns {string}
|
||||
* @returns {?string}
|
||||
* @example
|
||||
* // Logs: Application name: My App
|
||||
* console.log(`Application name: ${application}`);
|
||||
|
||||
49
src/util/ApplicationFlags.js
Normal file
49
src/util/ApplicationFlags.js
Normal file
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
const BitField = require('./BitField');
|
||||
|
||||
/**
|
||||
* Data structure that makes it easy to interact with a {@link ClientApplication#flags} bitfield.
|
||||
* @extends {BitField}
|
||||
*/
|
||||
class ApplicationFlags extends BitField {}
|
||||
|
||||
/**
|
||||
* @name ApplicationFlags
|
||||
* @kind constructor
|
||||
* @memberof ApplicationFlags
|
||||
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
|
||||
*/
|
||||
|
||||
/**
|
||||
* Bitfield of the packed bits
|
||||
* @type {number}
|
||||
* @name ApplicationFlags#bitfield
|
||||
*/
|
||||
|
||||
/**
|
||||
* Numeric application flags. All available properties:
|
||||
* * `MANAGED_EMOJI`
|
||||
* * `GROUP_DM_CREATE`
|
||||
* * `RPC_HAS_CONNECTED`
|
||||
* * `GATEWAY_PRESENCE`
|
||||
* * `FATEWAY_PRESENCE_LIMITED`
|
||||
* * `GATEWAY_GUILD_MEMBERS`
|
||||
* * `GATEWAY_GUILD_MEMBERS_LIMITED`
|
||||
* * `VERIFICATION_PENDING_GUILD_LIMIT`
|
||||
* * `EMBEDDED`
|
||||
* @type {Object}
|
||||
*/
|
||||
ApplicationFlags.FLAGS = {
|
||||
MANAGED_EMOJI: 1 << 2,
|
||||
GROUP_DM_CREATE: 1 << 4,
|
||||
RPC_HAS_CONNECTED: 1 << 11,
|
||||
GATEWAY_PRESENCE: 1 << 12,
|
||||
GATEWAY_PRESENCE_LIMITED: 1 << 13,
|
||||
GATEWAY_GUILD_MEMBERS: 1 << 14,
|
||||
GATEWAY_GUILD_MEMBERS_LIMITED: 1 << 15,
|
||||
VERIFICATION_PENDING_GUILD_LIMIT: 1 << 16,
|
||||
EMBEDDED: 1 << 17,
|
||||
};
|
||||
|
||||
module.exports = ApplicationFlags;
|
||||
@@ -3,7 +3,7 @@
|
||||
const BitField = require('./BitField');
|
||||
|
||||
/**
|
||||
* Data structure that makes it easy to interact with an {@link Message#flags} bitfield.
|
||||
* Data structure that makes it easy to interact with a {@link Message#flags} bitfield.
|
||||
* @extends {BitField}
|
||||
*/
|
||||
class MessageFlags extends BitField {}
|
||||
|
||||
Reference in New Issue
Block a user