99 lines
3.1 KiB
JavaScript
99 lines
3.1 KiB
JavaScript
const {extend: createGotClient} = require('got');
|
|
const {ChatClient} = require('dank-twitch-irc');
|
|
const chalk = require('chalk');
|
|
const Config = require('./config.json');
|
|
|
|
const clients = [];
|
|
let banChunks = [];
|
|
let lastIndex = 0;
|
|
const maxClients = 30;
|
|
const maxChunkSize = 5000;
|
|
const delayPerChunk = 30e3;
|
|
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
|
|
|
const pnslClient = createGotClient({
|
|
prefixUrl: 'https://bot.tetyys.com/api/v1',
|
|
throwHttpErrors: false,
|
|
headers: {
|
|
'Authorization': `Bearer ${Config.PNSLToken}`,
|
|
},
|
|
});
|
|
|
|
const sleep = (ms) => {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
};
|
|
|
|
const createTwitchClient = () => {
|
|
return new ChatClient({
|
|
username: Config.Username,
|
|
password: `oauth:${Config.Password}`,
|
|
rateLimits: 'verifiedBot',
|
|
});
|
|
};
|
|
|
|
const sendPong = () => {
|
|
for (const [i, client] of Object.entries(clients)) {
|
|
client.say(Config.Username, `Pong from client ${i}`);
|
|
}
|
|
};
|
|
|
|
const getConnection = () => {
|
|
return clients[++lastIndex % clients.length];
|
|
};
|
|
|
|
(() => {
|
|
[...Array(maxClients)].map((_, i) => {
|
|
clients[i] = createTwitchClient();
|
|
clients[i].connect();
|
|
clients[i].on('ready', async () => {
|
|
console.log(`${chalk.green('[CONNECTED]')} || Client ${i} Connected to twitch.`);
|
|
await clients[i].join(Config.Username);
|
|
});
|
|
});
|
|
|
|
clients[0].on('PRIVMSG', async (msg) => {
|
|
if (msg.messageText === '=mbping' && msg.senderUserID === Config.AdminID) {
|
|
sendPong();
|
|
} else if (msg.messageText.startsWith('=mbrun') && msg.senderUserID === Config.AdminID) {
|
|
const listID = msg.messageText.split(' ')[1];
|
|
const channel = msg.messageText.split(' ')[2];
|
|
if (!uuidRegex.test(listID)) {
|
|
return clients[1].say(msg.channelName, 'Invalid List ID!');
|
|
}
|
|
const {body, statusCode} = await pnslClient(`BotLists/${listID}`);
|
|
|
|
if (statusCode === 404) {
|
|
return clients[1].say(msg.channelName, 'List was not found.');
|
|
}
|
|
|
|
if (statusCode === 401) {
|
|
return clients[1].say(msg.channelName, 'Authorization token is invalid');
|
|
}
|
|
|
|
if (statusCode !== 200) {
|
|
return clients[1].say(msg.channelName, 'Unexpected error occurred.');
|
|
}
|
|
|
|
const banArr = body.split('\n');
|
|
|
|
if (banArr.length > maxChunkSize) {
|
|
while (banArr.length > 0) {
|
|
banChunks.push(banArr.splice(0, maxChunkSize));
|
|
}
|
|
|
|
for (const chunk of banChunks) {
|
|
for (const entry of chunk) {
|
|
getConnection().privmsg(channel || msg.channelName, entry);
|
|
}
|
|
await sleep(delayPerChunk);
|
|
}
|
|
banChunks = [];
|
|
} else {
|
|
for (const entry of banArr) {
|
|
getConnection().privmsg(channel || msg.channelName, entry);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
})();
|