Files
discord.js/src/structures/Integration.js
Souji d6234b764e feat(Role): role tags (#4628)
* feat(Role): role tags

* fix(RoleManager): fix js docs and mark nullable

* fix(Role): typings tags are be null, not undefined

* fix(Role): getters should actually return a bool

* fix(RoleManager): typo

* fix(Role): should always return a boolean

* fix(Gmrm): getter should return null

* fix(RoleManager): getters should return null

* fix: typing getters should return null

* fix(Role): docs grammar and consistency

* chore: prefer in operator over Reflect#has

* feat(GmRm): botRole getter

* fix(GmRm): use the actual properties

* feat(RoleManager): rem myRole in pref o botRoleFor

* fix(Role): remove obsolete is- getters

* fix:  checking tags after getter removal

* chore: identifier naming consistency

* chore: prefer explicit true type over boolean

* fix: typo

* feat(Integration): Add Integration#roles getter (#1)

* fix(RoleManager): remove bot check r:partials

* feat(RoleManager): robustness against uncached u

* docs: possibly undefined

Co-authored-by: Jan <66554238+Vaporox@users.noreply.github.com>
2020-12-14 13:54:02 +01:00

197 lines
4.4 KiB
JavaScript

'use strict';
const Base = require('./Base');
const IntegrationApplication = require('./IntegrationApplication');
/**
* The information account for an integration
* @typedef {Object} IntegrationAccount
* @property {string} id The id of the account
* @property {string} name The name of the account
*/
/**
* Represents a guild integration.
*/
class Integration extends Base {
constructor(client, data, guild) {
super(client);
/**
* The guild this integration belongs to
* @type {Guild}
*/
this.guild = guild;
/**
* The integration id
* @type {Snowflake}
*/
this.id = data.id;
/**
* The integration name
* @type {string}
*/
this.name = data.name;
/**
* The integration type (twitch, youtube, etc)
* @type {string}
*/
this.type = data.type;
/**
* Whether this integration is enabled
* @type {boolean}
*/
this.enabled = data.enabled;
/**
* Whether this integration is syncing
* @type {boolean}
*/
this.syncing = data.syncing;
/**
* The role that this integration uses for subscribers
* @type {Role}
*/
this.role = this.guild.roles.cache.get(data.role_id);
if (data.user) {
/**
* The user for this integration
* @type {?User}
*/
this.user = this.client.users.add(data.user);
} else {
this.user = null;
}
/**
* The account integration information
* @type {IntegrationAccount}
*/
this.account = data.account;
/**
* The last time this integration was last synced
* @type {number}
*/
this.syncedAt = data.synced_at;
this._patch(data);
}
/**
* All roles that are managed by this integration
* @type {Collection<Snowflake, Role>}
* @readonly
*/
get roles() {
const roles = this.guild.roles.cache;
return roles.filter(role => role.tags && role.tags.integrationID === this.id);
}
_patch(data) {
/**
* The behavior of expiring subscribers
* @type {number}
*/
this.expireBehavior = data.expire_behavior;
/**
* The grace period before expiring subscribers
* @type {number}
*/
this.expireGracePeriod = data.expire_grace_period;
if ('application' in data) {
if (this.application) {
this.application._patch(data.application);
} else {
/**
* The application for this integration
* @type {?IntegrationApplication}
*/
this.application = new IntegrationApplication(this.client, data.application);
}
} else if (!this.application) {
this.application = null;
}
}
/**
* Sync this integration
* @returns {Promise<Integration>}
*/
sync() {
this.syncing = true;
return this.client.api
.guilds(this.guild.id)
.integrations(this.id)
.post()
.then(() => {
this.syncing = false;
this.syncedAt = Date.now();
return this;
});
}
/**
* The data for editing an integration.
* @typedef {Object} IntegrationEditData
* @property {number} [expireBehavior] The new behaviour of expiring subscribers
* @property {number} [expireGracePeriod] The new grace period before expiring subscribers
*/
/**
* Edits this integration.
* @param {IntegrationEditData} data The data to edit this integration with
* @param {string} reason Reason for editing this integration
* @returns {Promise<Integration>}
*/
edit(data, reason) {
if ('expireBehavior' in data) {
data.expire_behavior = data.expireBehavior;
data.expireBehavior = null;
}
if ('expireGracePeriod' in data) {
data.expire_grace_period = data.expireGracePeriod;
data.expireGracePeriod = null;
}
// The option enable_emoticons is only available for Twitch at this moment
return this.client.api
.guilds(this.guild.id)
.integrations(this.id)
.patch({ data, reason })
.then(() => {
this._patch(data);
return this;
});
}
/**
* Deletes this integration.
* @returns {Promise<Integration>}
* @param {string} [reason] Reason for deleting this integration
*/
delete(reason) {
return this.client.api
.guilds(this.guild.id)
.integrations(this.id)
.delete({ reason })
.then(() => this);
}
toJSON() {
return super.toJSON({
role: 'roleID',
guild: 'guildID',
user: 'userID',
});
}
}
module.exports = Integration;