From 13eb78256da901e6c3c405f546f36617ef5e8239 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Sat, 8 Jan 2022 11:44:28 +0000 Subject: [PATCH] refactor(integration): turn undefined into null and consistency (#7209) --- .../discord.js/src/structures/Integration.js | 52 +++++++++++++------ packages/discord.js/src/util/Constants.js | 1 - packages/discord.js/typings/index.d.ts | 11 ++-- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/packages/discord.js/src/structures/Integration.js b/packages/discord.js/src/structures/Integration.js index 5773de861..f30388831 100644 --- a/packages/discord.js/src/structures/Integration.js +++ b/packages/discord.js/src/structures/Integration.js @@ -2,6 +2,7 @@ const Base = require('./Base'); const IntegrationApplication = require('./IntegrationApplication'); +const { IntegrationExpireBehaviors } = require('../util/Constants'); /** * The information account for an integration @@ -55,17 +56,21 @@ class Integration extends Base { */ this.enabled = data.enabled; - /** - * Whether this integration is syncing - * @type {?boolean} - */ - this.syncing = data.syncing; + if ('syncing' in data) { + /** + * Whether this integration is syncing + * @type {?boolean} + */ + this.syncing = data.syncing; + } else { + this.syncing ??= null; + } /** * The role that this integration uses for subscribers * @type {?Role} */ - this.role = this.guild.roles.cache.get(data.role_id); + this.role = this.guild.roles.resolve(data.role_id); if ('enable_emoticons' in data) { /** @@ -84,7 +89,7 @@ class Integration extends Base { */ this.user = this.client.users._add(data.user); } else { - this.user = null; + this.user ??= null; } /** @@ -93,11 +98,15 @@ class Integration extends Base { */ this.account = data.account; - /** - * The last time this integration was last synced - * @type {?number} - */ - this.syncedAt = data.synced_at; + if ('synced_at' in data) { + /** + * The timestamp at which this integration was last synced at + * @type {?number} + */ + this.syncedTimestamp = Date.parse(data.synced_at); + } else { + this.syncedTimestamp ??= null; + } if ('subscriber_count' in data) { /** @@ -122,6 +131,15 @@ class Integration extends Base { this._patch(data); } + /** + * The date at which this integration was last synced at + * @type {?Date} + * @readonly + */ + get syncedAt() { + return this.syncedTimestamp && new Date(this.syncedTimestamp); + } + /** * All roles that are managed by this integration * @type {Collection} @@ -136,17 +154,21 @@ class Integration extends Base { if ('expire_behavior' in data) { /** * The behavior of expiring subscribers - * @type {?number} + * @type {?IntegrationExpireBehavior} */ - this.expireBehavior = data.expire_behavior; + this.expireBehavior = IntegrationExpireBehaviors[data.expire_behavior]; + } else { + this.expireBehavior ??= null; } if ('expire_grace_period' in data) { /** - * The grace period before expiring subscribers + * The grace period (in days) before expiring subscribers * @type {?number} */ this.expireGracePeriod = data.expire_grace_period; + } else { + this.expireGracePeriod ??= null; } if ('application' in data) { diff --git a/packages/discord.js/src/util/Constants.js b/packages/discord.js/src/util/Constants.js index aebd943e3..740d2ff95 100644 --- a/packages/discord.js/src/util/Constants.js +++ b/packages/discord.js/src/util/Constants.js @@ -367,7 +367,6 @@ exports.InviteScopes = [ 'webhook.incoming', ]; -// TODO: change Integration#expireBehavior to this and clean up Integration /** * The behavior of expiring subscribers for Integrations. This can be: * * REMOVE_ROLE diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index b5ca3feee..81cd3c2e7 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1241,16 +1241,17 @@ export class Integration extends Base { public account: IntegrationAccount; public application: IntegrationApplication | null; public enabled: boolean; - public expireBehavior: number | undefined; - public expireGracePeriod: number | undefined; + public expireBehavior: IntegrationExpireBehaviors | null; + public expireGracePeriod: number | null; public guild: Guild; public id: Snowflake | string; public name: string; - public role: Role | undefined; + public role: Role | null; public enableEmoticons: boolean | null; public readonly roles: Collection; - public syncedAt: number | undefined; - public syncing: boolean | undefined; + public readonly syncedAt: Date | null; + public syncedTimestamp: number | null; + public syncing: boolean | null; public type: IntegrationType; public user: User | null; public subscriberCount: number | null;