Merge pull request 'Added support for self created ban lists' (#1) from zneix/massbanner:feature/add-url-lists into master

Reviewed-on: https://gitea.ivr.fi/Leppunen/massbanner/pulls/1
This commit is contained in:
Leppunen 2021-03-24 08:55:25 +00:00
commit 4d411afdee
2 changed files with 52 additions and 28 deletions

View File

@ -2,6 +2,7 @@
"Username": "",
"Password": "",
"PNSLToken": "",
"IgnorePNSL": false,
"Users": [],
"Prefix": "!!",
"MaxConnections": 30,

View File

@ -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,6 +121,7 @@ const say = (channel, message) => {
getConnection().say(channel, message);
};
if (!Config.IgnorePNSL){
const pnslWebSocket = new WS('wss://bot.tetyys.com/api/wss', [], {headers: {Authorization: `Bearer ${Config.PNSLToken}`}});
pnslWebSocket.on('open', () => {
@ -133,6 +149,7 @@ pnslWebSocket.on('message', async (data) => {
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}!`);
}
if (!Config.IgnorePNSL) {
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);