Send messages in batches to avoid rate limiting

This commit is contained in:
Wizzard 2024-02-05 16:53:57 -05:00
parent 8d65a162c4
commit 73fc0e06e0
1 changed files with 31 additions and 7 deletions

28
main.js
View File

@ -4,23 +4,47 @@ const client = new Client();
const SOURCE_CHANNEL_ID = process.env.SOURCE_CHANNEL_ID;
const TARGET_CHANNEL_ID = process.env.TARGET_CHANNEL_ID;
const BATCH_INTERVAL = 5000;
let messageQueue = [];
client.on('ready', () => {
console.log(`${client.user.tag} is ready!`);
setInterval(processMessageQueue, BATCH_INTERVAL);
});
client.on('messageCreate', async message => {
if (message.channel.id === SOURCE_CHANNEL_ID) {
if (message.author.id === client.user.id) return;
// Add the new message to the queue
messageQueue.push(`<@${message.author.id}> / **${message.author.tag}**: ${message.content}`);
}
});
async function processMessageQueue() {
if (messageQueue.length === 0) return; // Skip if no messages
const targetChannel = await client.channels.fetch(TARGET_CHANNEL_ID);
if (!targetChannel) {
console.error('Target channel not found');
return;
}
targetChannel.send(`<@${message.author.id}> / **${message.author.tag}**: ${message.content}`);
let batchMessage = '';
while (messageQueue.length > 0 && (batchMessage.length + messageQueue[0].length) <= 2000) {
const nextMessage = messageQueue.shift();
if (batchMessage.length + nextMessage.length + 1 <= 2000) {
batchMessage += nextMessage + '\n';
} else {
await targetChannel.send(batchMessage);
batchMessage = nextMessage + '\n';
}
});
}
if (batchMessage.length > 0) {
await targetChannel.send(batchMessage);
}
}
client.login(process.env.DISCORD_TOKEN);