Skip to content

Commit

Permalink
refactor: use function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Oct 11, 2024
1 parent ef4fc9e commit 43eb7fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
12 changes: 3 additions & 9 deletions packages/discord.js/src/managers/ThreadMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -124,14 +124,8 @@ class ThreadMemberManager extends CachedManager {
* @returns {Promise<Snowflake>}
*/
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);
Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/src/structures/ThreadMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Base = require('./Base');
const ThreadMemberFlagsBitField = require('../util/ThreadMemberFlagsBitField');
const { emitDeprecationWarningRemoveThreadMember } = require('../util/Util');

/**
* Represents a Member for a Thread.
Expand Down Expand Up @@ -106,6 +107,10 @@ class ThreadMember extends Base {
* @returns {Promise<ThreadMember>}
*/
async remove(reason) {
if (reason !== undefined) {
emitDeprecationWarningRemoveThreadMember(this.constructor.name);
}

await this.thread.members.remove(this.id, reason);
return this;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'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');
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.
Expand Down Expand Up @@ -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,
Expand All @@ -518,6 +534,7 @@ module.exports = {
parseWebhookURL,
transformResolved,
resolveSKUId,
emitDeprecationWarningRemoveThreadMember,
};

// Fixes Circular
Expand Down

0 comments on commit 43eb7fe

Please sign in to comment.