From 2ad6627e020e9910097a0ed903f7519537f0448d Mon Sep 17 00:00:00 2001 From: zneix Date: Tue, 23 Mar 2021 03:02:31 +0100 Subject: [PATCH] Added support for url lists --- config_dist.json | 1 + index.js | 77 +++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/config_dist.json b/config_dist.json index 49fef98..cf45f31 100644 --- a/config_dist.json +++ b/config_dist.json @@ -2,6 +2,7 @@ "Username": "", "Password": "", "PNSLToken": "", + "UsePNSL": true, "Users": [], "Prefix": "!!", "MaxConnections": 30, diff --git a/index.js b/index.js index 0975185..9e9483f 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,8 @@ const pnslClient = createGotClient({ }, }); +const urlClient = createGotClient(); + const fetchList = async (list) => { const listID = uuidRegex.exec(list)?.[0]; if (!listID) { @@ -44,6 +46,19 @@ const fetchList = async (list) => { return {listMeta, listData}; }; +const fetchUrlList = async (list) => { + const {body: listMetaBody, statusCode} = await urlClient(list); + if (statusCode === 404) { + throw new Error('List was not found.'); + } + + if (statusCode !== 200) { + throw new Error('Unexpected error occured!'); + } + + return listMetaBody; +}; + const runList = async (listData, listID, channel) => { const banArr = listData.split('\n'); @@ -106,33 +121,35 @@ const say = (channel, message) => { getConnection().say(channel, message); }; -const pnslWebSocket = new WS('wss://bot.tetyys.com/api/wss', [], {headers: {Authorization: `Bearer ${Config.PNSLToken}`}}); +if (Config.UsePNSL){ + const pnslWebSocket = new WS('wss://bot.tetyys.com/api/wss', [], {headers: {Authorization: `Bearer ${Config.PNSLToken}`}}); -pnslWebSocket.on('open', () => { - console.log(`${chalk.green('[P&SL]')} || Connected to P&SL Websocket server`); -}); + pnslWebSocket.on('open', () => { + console.log(`${chalk.green('[P&SL]')} || Connected to P&SL Websocket server`); + }); -pnslWebSocket.on('message', async (data) => { - const {o: type, p: payload} = JSON.parse(data); - switch (type) { - case 0: { - const {listMeta} = await fetchList(payload.LatestBotList); - console.log(`${chalk.green('[P&SL]')} || Latest Botlist: ${payload.LatestBotList} [${listMeta.name}] (${listMeta.count})`); - break; - } - case 1: { - console.log(`${chalk.green('[P&SL]')} || New Botlist: ${payload.Guid} [${payload.Name}] (${payload.Count}) (${payload.Tags.join(' ')})`); - break; - } - case 2: { - console.log(`${chalk.green('[P&SL]')} || New False Positive: ${payload.UserTwitchId} [${payload.BotListGuid}]`); - break; - } + pnslWebSocket.on('message', async (data) => { + const {o: type, p: payload} = JSON.parse(data); + switch (type) { + case 0: { + const {listMeta} = await fetchList(payload.LatestBotList); + console.log(`${chalk.green('[P&SL]')} || Latest Botlist: ${payload.LatestBotList} [${listMeta.name}] (${listMeta.count})`); + break; + } + case 1: { + console.log(`${chalk.green('[P&SL]')} || New Botlist: ${payload.Guid} [${payload.Name}] (${payload.Count}) (${payload.Tags.join(' ')})`); + break; + } + case 2: { + console.log(`${chalk.green('[P&SL]')} || New False Positive: ${payload.UserTwitchId} [${payload.BotListGuid}]`); + break; + } - default: - break; - } -}); + default: + break; + } + }); +} (async () => { await Promise.all([...Array(Config.MaxConnections)].map(async (_, i) => { @@ -193,8 +210,14 @@ pnslWebSocket.on('message', async (data) => { const mods = await getConnection().getMods(channel); if (!mods.includes(Config.Username)) return say(Config.Username, `I am not a moderator in channel ${channel}!`); } - const data = await fetchList(listID); - await runList(data.listData, listID, channel || channelName); + if (Config.UsePNSL) { + const data = await fetchList(listID); + await runList(data.listData, listID, channel || channelName); + } + else { + const data = await fetchUrlList(listID); + await runList(data, listID, channel || channelName); + } say(Config.Username, `List ${listID} executed successfully on channel ${channel || channelName}`); } catch (error) { console.error(error); @@ -218,4 +241,4 @@ process .on('uncaughtException', (err) => { console.error(`${chalk.red('[UncaughtException]')} || ${err.message}`); process.exit(0); - }); \ No newline at end of file + });