From 43eb7fed7cce3948fcb0ab7cca7bd3c4e8cad790 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:38:49 +0100 Subject: [PATCH] refactor: use function --- .../src/managers/ThreadMemberManager.js | 12 +++--------- .../discord.js/src/structures/ThreadMember.js | 5 +++++ packages/discord.js/src/util/Util.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/discord.js/src/managers/ThreadMemberManager.js b/packages/discord.js/src/managers/ThreadMemberManager.js index e7e7382f3532b..8f06269b30eaf 100644 --- a/packages/discord.js/src/managers/ThreadMemberManager.js +++ b/packages/discord.js/src/managers/ThreadMemberManager.js @@ -7,9 +7,9 @@ const { Routes } = require('discord-api-types/v10'); const CachedManager = require('./CachedManager'); const { DiscordjsTypeError, ErrorCodes } = require('../errors'); const ThreadMember = require('../structures/ThreadMember'); +const { emitDeprecationWarningRemoveThreadMember } = require('../util/Util'); let deprecationEmittedForAdd = false; -let deprecationEmittedForRemove = false; /** * Manages API methods for GuildMembers and stores their cache. @@ -124,14 +124,8 @@ class ThreadMemberManager extends CachedManager { * @returns {Promise} */ async remove(member, reason) { - if (reason !== undefined && !deprecationEmittedForRemove) { - process.emitWarning( - // eslint-disable-next-line max-len - 'The reason parameter of ThreadMemberManager#remove() is deprecated as Discord does not parse them. It will be removed in the next major version.', - 'DeprecationWarning', - ); - - deprecationEmittedForRemove = true; + if (reason !== undefined) { + emitDeprecationWarningRemoveThreadMember(this.constructor.name); } const id = member === '@me' ? member : this.client.users.resolveId(member); diff --git a/packages/discord.js/src/structures/ThreadMember.js b/packages/discord.js/src/structures/ThreadMember.js index 9af7e2abdd13d..00929453543ff 100644 --- a/packages/discord.js/src/structures/ThreadMember.js +++ b/packages/discord.js/src/structures/ThreadMember.js @@ -2,6 +2,7 @@ const Base = require('./Base'); const ThreadMemberFlagsBitField = require('../util/ThreadMemberFlagsBitField'); +const { emitDeprecationWarningRemoveThreadMember } = require('../util/Util'); /** * Represents a Member for a Thread. @@ -106,6 +107,10 @@ class ThreadMember extends Base { * @returns {Promise} */ async remove(reason) { + if (reason !== undefined) { + emitDeprecationWarningRemoveThreadMember(this.constructor.name); + } + await this.thread.members.remove(this.id, reason); return this; } diff --git a/packages/discord.js/src/util/Util.js b/packages/discord.js/src/util/Util.js index 1214ad71fb82d..47f12e1fc14d3 100644 --- a/packages/discord.js/src/util/Util.js +++ b/packages/discord.js/src/util/Util.js @@ -1,6 +1,7 @@ 'use strict'; const { parse } = require('node:path'); +const process = require('node:process'); const { Collection } = require('@discordjs/collection'); const { ChannelType, RouteBases, Routes } = require('discord-api-types/v10'); const { fetch } = require('undici'); @@ -8,6 +9,8 @@ const Colors = require('./Colors'); const { DiscordjsError, DiscordjsRangeError, DiscordjsTypeError, ErrorCodes } = require('../errors'); const isObject = d => typeof d === 'object' && d !== null; +let deprecationEmittedForRemoveThreadMember = false; + /** * Flatten an object. Any properties that are collections will get converted to an array of keys. * @param {Object} obj The object to flatten. @@ -499,6 +502,19 @@ function resolveSKUId(resolvable) { return null; } +/** + * Deprecation function for the reason parameter of removing thread members. + * @param {string} name Name of the class + * @private + */ +function emitDeprecationWarningRemoveThreadMember(name) { + if (deprecationEmittedForRemoveThreadMember) return; + process.emitWarning( + `The reason parameter of ${name}#remove() is deprecated as Discord does not parse them. It will be removed in the next major version.`, + ); + deprecationEmittedForRemoveThreadMember = true; +} + module.exports = { flatten, fetchRecommendedShardCount, @@ -518,6 +534,7 @@ module.exports = { parseWebhookURL, transformResolved, resolveSKUId, + emitDeprecationWarningRemoveThreadMember, }; // Fixes Circular