Added channel mappings to support multiple channels / servers

This commit is contained in:
Wizzard 2024-02-05 17:22:14 -05:00
parent d248923f1d
commit c324512523
1 changed files with 27 additions and 22 deletions

35
main.js
View File

@ -2,51 +2,56 @@ require('dotenv').config();
const { Client } = require('discord.js-selfbot-v13'); const { Client } = require('discord.js-selfbot-v13');
const client = new Client(); 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; const BATCH_INTERVAL = 5000;
let messageQueue = []; let messageQueue = [];
const channelMappings = {
[process.env.SOURCE_CHANNEL_ID_1]: process.env.TARGET_CHANNEL_ID_1,
[process.env.SOURCE_CHANNEL_ID_2]: process.env.TARGET_CHANNEL_ID_2,
};
client.on('ready', () => { client.on('ready', () => {
console.log(`${client.user.tag} is ready!`); console.log(`${client.user.tag} is ready!`);
setInterval(processMessageQueue, BATCH_INTERVAL); setInterval(processMessageQueue, BATCH_INTERVAL);
}); });
client.on('messageCreate', async message => { client.on('messageCreate', async message => {
if (message.channel.id === SOURCE_CHANNEL_ID) { if (channelMappings[message.channel.id]) {
if (message.author.id === client.user.id) return; if (message.author.id === client.user.id) return;
const timestamp = new Date(message.createdTimestamp).toISOString(); const timestamp = new Date(message.createdTimestamp).toISOString();
const formattedMessage = {
const formattedMessage = `<@${message.author.id}> / **${message.author.tag}**: ${message.content} \`${timestamp}\``; content: `<@${message.author.id}> / **${message.author.tag}**: ${message.content} \`${timestamp}\``,
target: channelMappings[message.channel.id]
};
messageQueue.push(formattedMessage); messageQueue.push(formattedMessage);
} }
}); });
async function processMessageQueue() { async function processMessageQueue() {
if (messageQueue.length === 0) return; // Skip if no messages if (messageQueue.length === 0) return;
const targetChannel = await client.channels.fetch(TARGET_CHANNEL_ID);
if (!targetChannel) {
console.error('Target channel not found');
return;
}
while (messageQueue.length > 0) {
let batchMessage = ''; let batchMessage = '';
while (messageQueue.length > 0 && (batchMessage.length + messageQueue[0].length) <= 2000) { let targetChannelId = messageQueue[0].target;
const nextMessage = messageQueue.shift();
while (messageQueue.length > 0 && messageQueue[0].target === targetChannelId && (batchMessage.length + messageQueue[0].content.length) <= 2000) {
const nextMessage = messageQueue.shift().content;
if (batchMessage.length + nextMessage.length + 1 <= 2000) { if (batchMessage.length + nextMessage.length + 1 <= 2000) {
batchMessage += nextMessage + '\n'; batchMessage += nextMessage + '\n';
} else { } else {
const targetChannel = await client.channels.fetch(targetChannelId);
await targetChannel.send(batchMessage); await targetChannel.send(batchMessage);
batchMessage = nextMessage + '\n'; batchMessage = nextMessage + '\n'; // Start a new batch
} }
} }
if (batchMessage.length > 0) { if (batchMessage.length > 0) {
const targetChannel = await client.channels.fetch(targetChannelId);
await targetChannel.send(batchMessage); await targetChannel.send(batchMessage);
} }
}
} }
client.login(process.env.DISCORD_TOKEN); client.login(process.env.DISCORD_TOKEN);