make say more resilient to connection errors

This commit is contained in:
Leppunen 2020-12-22 10:10:29 +02:00
parent 9f37b43861
commit d42b939af8

View file

@ -69,7 +69,7 @@ const runList = async (listData, listID, channel) => {
break; break;
} }
clients[0].say(Config.Username, `Chunk ${Number(i) + 1} of ${listID} executed successfully on channel ${channel}`); say(Config.Username, `Chunk ${Number(i) + 1} of ${listID} executed successfully on channel ${channel}`);
await sleep(Config.DelayPerChunk); await sleep(Config.DelayPerChunk);
} }
} else { } else {
@ -102,6 +102,10 @@ const getConnection = () => {
return readyClients[++lastIndex % readyClients.length]; return readyClients[++lastIndex % readyClients.length];
}; };
const say = (channel, message) => {
getConnection().say(channel, message);
};
const pnslWebSocket = new WS('wss://bot.tetyys.com/api/wss', [], {headers: {Authorization: `Bearer ${Config.PNSLToken}`}}); const pnslWebSocket = new WS('wss://bot.tetyys.com/api/wss', [], {headers: {Authorization: `Bearer ${Config.PNSLToken}`}});
pnslWebSocket.on('open', () => { pnslWebSocket.on('open', () => {
@ -168,26 +172,26 @@ pnslWebSocket.on('message', async (data) => {
if (command === 'ping') { if (command === 'ping') {
const channel = args[0]; const channel = args[0];
return clients[0].say(channel || channelName, `${clients.filter((i) => i.ready).length} Clients from total of ${clients.length} are operational`); return say(channel || channelName, `${clients.filter((i) => i.ready).length} Clients from total of ${clients.length} are operational`);
} }
if (command === 'runlist') { if (command === 'runlist') {
const listID = args[0]; const listID = args[0];
const channel = args[1]; const channel = args[1];
if (!listID) return clients[0].say(Config.Username, 'No list ID provided!'); if (!listID) return say(Config.Username, 'No list ID provided!');
try { try {
if (channel) { if (channel) {
const mods = await clients[0].getMods(channel); const mods = await clients[0].getMods(channel);
if (!mods.includes(Config.Username)) return clients[0].say(Config.Username, `I am not a moderator in channel ${channel}!`); if (!mods.includes(Config.Username)) return say(Config.Username, `I am not a moderator in channel ${channel}!`);
} }
const data = await fetchList(listID); const data = await fetchList(listID);
await runList(data.listData, listID, channel || channelName); await runList(data.listData, listID, channel || channelName);
clients[0].say(Config.Username, `List ${listID} executed successfully on channel ${channel || channelName}`); say(Config.Username, `List ${listID} executed successfully on channel ${channel || channelName}`);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return clients[0].say(Config.Username, `Error Occurred! ${error.message}`); return say(Config.Username, `Error Occurred! ${error.message}`);
} }
} }
}); });