c.ix
c.ix
DIdiscord.js - Imagine ❄
Created by c.ix on 11/26/2024 in #djs-questions
Managing Internal Queues
Ah, fixed it.
11 replies
DIdiscord.js - Imagine ❄
Created by c.ix on 11/26/2024 in #djs-questions
Managing Internal Queues
const log = require("./loggerUtils");
const { RateLimitError } = require('discord.js');

const channelNameUpdateQueue = new Map();

async function updateChannelName(channel, newName) {
const channelId = channel.id;

if (channelNameUpdateQueue.has(channelId)) {
const queue = channelNameUpdateQueue.get(channelId);
log.warn(`Rate limit pending for channel ${channelId}. Replacing queued name with "${newName}".`);
queue.latestName = newName;
return;
}

const queue = {
latestName: newName,
timeout: null,
};
channelNameUpdateQueue.set(channelId, queue);

log.debug(`Queuing name update for channel ${channelId}: "${newName}".`);

await processQueue(channelId, channel);
}

async function processQueue(channelId, channel) {
const queue = channelNameUpdateQueue.get(channelId);

if (!queue) return;

try {
await channel.setName(queue.latestName);
log.debug(`Channel name updated successfully: ${queue.latestName}`);

channelNameUpdateQueue.delete(channelId);
} catch (error) {
if (error instanceof RateLimitError) {
const retryAfter = error.retryAfter;
log.warn(`Rate limit hit for channel ${channelId}. Retrying after ${retryAfter}ms.`);
queue.timeout = setTimeout(() => {
processQueue(channelId, channel).catch((err) =>
log.error(`Failed to retry channel name update: ${err.message}`)
);
}, retryAfter);
} else {
log.error(`Error updating channel name: ${error.message}`);
channelNameUpdateQueue.delete(channelId);
}
}
}

module.exports = { updateChannelName };
const log = require("./loggerUtils");
const { RateLimitError } = require('discord.js');

const channelNameUpdateQueue = new Map();

async function updateChannelName(channel, newName) {
const channelId = channel.id;

if (channelNameUpdateQueue.has(channelId)) {
const queue = channelNameUpdateQueue.get(channelId);
log.warn(`Rate limit pending for channel ${channelId}. Replacing queued name with "${newName}".`);
queue.latestName = newName;
return;
}

const queue = {
latestName: newName,
timeout: null,
};
channelNameUpdateQueue.set(channelId, queue);

log.debug(`Queuing name update for channel ${channelId}: "${newName}".`);

await processQueue(channelId, channel);
}

async function processQueue(channelId, channel) {
const queue = channelNameUpdateQueue.get(channelId);

if (!queue) return;

try {
await channel.setName(queue.latestName);
log.debug(`Channel name updated successfully: ${queue.latestName}`);

channelNameUpdateQueue.delete(channelId);
} catch (error) {
if (error instanceof RateLimitError) {
const retryAfter = error.retryAfter;
log.warn(`Rate limit hit for channel ${channelId}. Retrying after ${retryAfter}ms.`);
queue.timeout = setTimeout(() => {
processQueue(channelId, channel).catch((err) =>
log.error(`Failed to retry channel name update: ${err.message}`)
);
}, retryAfter);
} else {
log.error(`Error updating channel name: ${error.message}`);
channelNameUpdateQueue.delete(channelId);
}
}
}

module.exports = { updateChannelName };
I now simply call await updateChannelName(channel, newName); instead of:
await channel.setName(``);
await channel.setName(``);
Here's how I added rejectOnRateLimit to the rest options in Client:
const client = new Client({
//intents: [...],
rest: {
rejectOnRateLimit: (info) => {
return info.route == "/channels/:id" && info.method == "PATCH";
},
},
});
const client = new Client({
//intents: [...],
rest: {
rejectOnRateLimit: (info) => {
return info.route == "/channels/:id" && info.method == "PATCH";
},
},
});
11 replies
DIdiscord.js - Imagine ❄
Created by c.ix on 11/26/2024 in #djs-questions
Managing Internal Queues
great, cheers.
11 replies
DIdiscord.js - Imagine ❄
Created by c.ix on 11/26/2024 in #djs-questions
Managing Internal Queues
How can I make changes to throw error instead of queueing ?
11 replies
DIdiscord.js - Imagine ❄
Created by c.ix on 11/26/2024 in #djs-questions
Managing Internal Queues
Sure, you can find it here. It does work independent from discord.js, and it worked as expected... 🤷‍♂️
11 replies