mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
feat: add support for fetching multiple guilds (#5472)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com> Co-authored-by: Noel <icrawltogo@gmail.com>
This commit is contained in:
115
src/structures/BaseGuild.js
Normal file
115
src/structures/BaseGuild.js
Normal file
@@ -0,0 +1,115 @@
|
||||
'use strict';
|
||||
|
||||
const Base = require('./Base');
|
||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||
|
||||
/**
|
||||
* The base class for {@link Guild} and {@link OAuth2Guild}.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class BaseGuild extends Base {
|
||||
constructor(client, data) {
|
||||
super(client);
|
||||
|
||||
/**
|
||||
* The ID of this guild
|
||||
* @type {Snowflake}
|
||||
*/
|
||||
this.id = data.id;
|
||||
|
||||
/**
|
||||
* The name of this guild
|
||||
* @type {string}
|
||||
*/
|
||||
this.name = data.name;
|
||||
|
||||
/**
|
||||
* The icon hash of this guild
|
||||
* @type {?string}
|
||||
*/
|
||||
this.icon = data.icon;
|
||||
|
||||
/**
|
||||
* An array of features available to this guild
|
||||
* @type {Features[]}
|
||||
*/
|
||||
this.features = data.features;
|
||||
}
|
||||
|
||||
/**
|
||||
* The timestamp this guild was created at
|
||||
* @type {number}
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* The time this guild was created at
|
||||
* @type {Date}
|
||||
* @readonly
|
||||
*/
|
||||
get createdAt() {
|
||||
return new Date(this.createdTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* The acronym that shows up in place of a guild icon
|
||||
* @type {string}
|
||||
* @readonly
|
||||
*/
|
||||
get nameAcronym() {
|
||||
return this.name
|
||||
.replace(/'s /g, ' ')
|
||||
.replace(/\w+/g, e => e[0])
|
||||
.replace(/\s/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this guild is partnered
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get partnered() {
|
||||
return this.features.includes('PARTNERED');
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this guild is verified
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get verified() {
|
||||
return this.features.includes('VERIFIED');
|
||||
}
|
||||
|
||||
/**
|
||||
* The URL to this guild's icon.
|
||||
* @param {ImageURLOptions} [options={}] Options for the Image URL
|
||||
* @returns {?string}
|
||||
*/
|
||||
iconURL({ format, size, dynamic } = {}) {
|
||||
if (!this.icon) return null;
|
||||
return this.client.rest.cdn.Icon(this.id, this.icon, format, size, dynamic);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches this guild.
|
||||
* @returns {Promise<Guild>}
|
||||
*/
|
||||
async fetch() {
|
||||
const data = await this.client.api.guilds(this.id).get({ query: { with_counts: true } });
|
||||
return this.client.guilds.add(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* When concatenated with a string, this automatically returns the guild's name instead of the Guild object.
|
||||
* @returns {string}
|
||||
*/
|
||||
toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseGuild;
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const Base = require('./Base');
|
||||
const BaseGuild = require('./BaseGuild');
|
||||
const GuildAuditLogs = require('./GuildAuditLogs');
|
||||
const GuildPreview = require('./GuildPreview');
|
||||
const GuildTemplate = require('./GuildTemplate');
|
||||
@@ -27,7 +27,6 @@ const {
|
||||
NSFWLevels,
|
||||
} = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||
const SystemChannelFlags = require('../util/SystemChannelFlags');
|
||||
const Util = require('../util/Util');
|
||||
|
||||
@@ -35,15 +34,11 @@ const Util = require('../util/Util');
|
||||
* Represents a guild (or a server) on Discord.
|
||||
* <info>It's recommended to see if a guild is available before performing operations or reading data from it. You can
|
||||
* check this with `guild.available`.</info>
|
||||
* @extends {Base}
|
||||
* @extends {BaseGuild}
|
||||
*/
|
||||
class Guild extends Base {
|
||||
/**
|
||||
* @param {Client} client The instantiating client
|
||||
* @param {Object} data The data for the guild
|
||||
*/
|
||||
class Guild extends BaseGuild {
|
||||
constructor(client, data) {
|
||||
super(client);
|
||||
super(client, data);
|
||||
|
||||
/**
|
||||
* A manager of the application commands belonging to this guild
|
||||
@@ -100,12 +95,6 @@ class Guild extends Base {
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.available = false;
|
||||
|
||||
/**
|
||||
* The Unique ID of the guild, useful for comparisons
|
||||
* @type {Snowflake}
|
||||
*/
|
||||
this.id = data.id;
|
||||
} else {
|
||||
this._patch(data);
|
||||
if (!data.channels) this.available = false;
|
||||
@@ -133,17 +122,11 @@ class Guild extends Base {
|
||||
* @private
|
||||
*/
|
||||
_patch(data) {
|
||||
/**
|
||||
* The name of the guild
|
||||
* @type {string}
|
||||
*/
|
||||
this.id = data.id;
|
||||
this.name = data.name;
|
||||
|
||||
/**
|
||||
* The hash of the guild icon
|
||||
* @type {?string}
|
||||
*/
|
||||
this.icon = data.icon;
|
||||
this.features = data.features;
|
||||
this.available = !data.unavailable;
|
||||
|
||||
/**
|
||||
* The hash of the guild invite splash image
|
||||
@@ -204,12 +187,6 @@ class Guild extends Base {
|
||||
* @typedef {string} Features
|
||||
*/
|
||||
|
||||
/**
|
||||
* An array of guild features available to the guild
|
||||
* @type {Features[]}
|
||||
*/
|
||||
this.features = data.features;
|
||||
|
||||
/**
|
||||
* The ID of the application that created this guild (if applicable)
|
||||
* @type {?Snowflake}
|
||||
@@ -378,10 +355,6 @@ class Guild extends Base {
|
||||
*/
|
||||
this.banner = data.banner;
|
||||
|
||||
this.id = data.id;
|
||||
this.available = !data.unavailable;
|
||||
this.features = data.features || this.features || [];
|
||||
|
||||
/**
|
||||
* The ID of the rules channel for the guild
|
||||
* @type {?Snowflake}
|
||||
@@ -463,24 +436,6 @@ class Guild extends Base {
|
||||
return this.client.rest.cdn.Banner(this.id, this.banner, format, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* The timestamp the guild was created at
|
||||
* @type {number}
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return SnowflakeUtil.deconstruct(this.id).timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* The time the guild was created at
|
||||
* @type {Date}
|
||||
* @readonly
|
||||
*/
|
||||
get createdAt() {
|
||||
return new Date(this.createdTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* The time the client user joined the guild
|
||||
* @type {Date}
|
||||
@@ -490,46 +445,6 @@ class Guild extends Base {
|
||||
return new Date(this.joinedTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* If this guild is partnered
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get partnered() {
|
||||
return this.features.includes('PARTNERED');
|
||||
}
|
||||
|
||||
/**
|
||||
* If this guild is verified
|
||||
* @type {boolean}
|
||||
* @readonly
|
||||
*/
|
||||
get verified() {
|
||||
return this.features.includes('VERIFIED');
|
||||
}
|
||||
|
||||
/**
|
||||
* The URL to this guild's icon.
|
||||
* @param {ImageURLOptions} [options={}] Options for the Image URL
|
||||
* @returns {?string}
|
||||
*/
|
||||
iconURL({ format, size, dynamic } = {}) {
|
||||
if (!this.icon) return null;
|
||||
return this.client.rest.cdn.Icon(this.id, this.icon, format, size, dynamic);
|
||||
}
|
||||
|
||||
/**
|
||||
* The acronym that shows up in place of a guild icon.
|
||||
* @type {string}
|
||||
* @readonly
|
||||
*/
|
||||
get nameAcronym() {
|
||||
return this.name
|
||||
.replace(/'s /g, ' ')
|
||||
.replace(/\w+/g, e => e[0])
|
||||
.replace(/\s/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* The URL to this guild's invite splash image.
|
||||
* @param {ImageURLOptions} [options={}] Options for the Image URL
|
||||
@@ -626,20 +541,6 @@ class Guild extends Base {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches this guild.
|
||||
* @returns {Promise<Guild>}
|
||||
*/
|
||||
fetch() {
|
||||
return this.client.api
|
||||
.guilds(this.id)
|
||||
.get({ query: { with_counts: true } })
|
||||
.then(data => {
|
||||
this._patch(data);
|
||||
return this;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a collection of integrations to this guild.
|
||||
* Resolves with a collection mapping integrations by their ids.
|
||||
@@ -1409,17 +1310,6 @@ class Guild extends Base {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* When concatenated with a string, this automatically returns the guild's name instead of the Guild object.
|
||||
* @returns {string}
|
||||
* @example
|
||||
* // Logs: Hello from My Guild!
|
||||
* console.log(`Hello from ${guild}!`);
|
||||
*/
|
||||
toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
const json = super.toJSON({
|
||||
available: false,
|
||||
|
||||
28
src/structures/OAuth2Guild.js
Normal file
28
src/structures/OAuth2Guild.js
Normal file
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const BaseGuild = require('./BaseGuild');
|
||||
const Permissions = require('../util/Permissions');
|
||||
|
||||
/**
|
||||
* A partial guild received when using {@link GuildManager#fetch} to fetch multiple guilds.
|
||||
* @extends {BaseGuild}
|
||||
*/
|
||||
class OAuth2Guild extends BaseGuild {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
|
||||
/**
|
||||
* Whether the client user is the owner of the guild
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.owner = data.owner;
|
||||
|
||||
/**
|
||||
* The permissions that the client user has in this guild
|
||||
* @type {Readonly<Permissions>}
|
||||
*/
|
||||
this.permissions = new Permissions(BigInt(data.permissions)).freeze();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OAuth2Guild;
|
||||
Reference in New Issue
Block a user