diff --git a/app.js b/app.js index 26d9aec..b64233d 100644 --- a/app.js +++ b/app.js @@ -14,11 +14,12 @@ const runEvent = (name, args) => { eventy[name].forEach(listener => { listener(...args); }); -} +}; fs.readdirSync(modulFolder).forEach(function (soubor) { if (soubor.endsWith(".js")) { const modul = require(`${modulFolder}${soubor}`); + modul.client = client; Object.keys(modul).forEach(name => { if (name.startsWith('on_')) { if (!eventy[name]) { @@ -49,7 +50,7 @@ const spim = mes => { else return false; } return true; -} +}; client.on("message", function (mes) { if (process.env.IGNORE_MESS || spim(mes)) return; diff --git a/modules/status.js b/modules/status.js new file mode 100644 index 0000000..f787238 --- /dev/null +++ b/modules/status.js @@ -0,0 +1,77 @@ +const fetch = require("node-fetch"); + +const role = { online: '684443816383610916', idle: '684444083065978941', dnd: '684444020558135399', offline: '684443903759614049', onlinephone: '777989420728975390', idlephone: "836554207342362626", dndphone: "836554469478760449" }; +const statusy = { offline: "0", online: "1", idle: "2", dnd: "3", onlinephone: "11", idlephone: "12", dndphone: "13" }; + +const prepSend = pair => { + const changes = []; + + pair.forEach(par => { + const us = par[0]; + changes.push({ id: us.id, status: par[1], nick: us.username, pfp: us.avatar ?? "" }); + }); + + poslatData({ changes }); +}; + +const poslatData = data => { + data.pwd = process.env.STAT_PASS; + fetch("http://deadfish.cz:4629/endpoint-pro-denimka/", { method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); +}; + +const statusOnFoun = (bef, aft) => { + if (!bef) bef = { status: 'offline', clientStatus: {} }; + [bef, aft].forEach(s => { + const mobile = s.clientStatus.mobile; + if (mobile && mobile !== s.clientStatus.desktop) s.status = `${mobile}phone`; + }); + return [bef, aft]; +}; + +const hovno = () => { + poslatData({ nejsemPoslej: !0 }); + setTimeout(hovno, 60_000); +}; +hovno(); + +module.exports = { + + // Změna rolí podle statusu a odeslání statusu + on_presenceUpdate: (bef, aft) => { + if (aft.guild.id !== '555779161067749446' || process.env.IGNORE_PRESENCE) return; + + [bef, aft] = statusOnFoun(bef, aft); + if (bef.status === aft.status) return; + + aft.member.roles.add(role[aft.status]); + aft.member.roles.remove(role[bef.status]); + + prepSend([[aft.user, statusy[aft.status]]]); + }, + + // Odeslání statusů při startu bota + on_ready: () => { + const client = module.exports.client; + const guild = client.guilds.cache.get("555779161067749446"); + const memberove = guild.members.cache.clone(); + const presence = [...guild.presences.cache.values()]; + const changes = []; + + presence.forEach(presenc => { + const status = statusOnFoun(null, presenc)[1].status; + changes.push([memberove.get(presenc.userID).user, statusy[status]]); + memberove.delete(presenc.userID); + }); + + for (const [_, value] of memberove) { + changes.push([value.user, 0]); + } + + prepSend(changes); + }, + + // Odeslání statusu při změně jména nebo profilovky + on_userUpdate: (_, aft) => { + prepSend([[aft, statusy[statusOnFoun(null, aft.presence)[1].status]]]); + } +}; diff --git a/modules/zbytek.js b/modules/zbytek.js index 09a8863..d535aa7 100644 --- a/modules/zbytek.js +++ b/modules/zbytek.js @@ -1,6 +1,5 @@ // Komandy nebo handelery který se nikam jinam nehodí -const role = { online: '684443816383610916', idle: '684444083065978941', dnd: '684444020558135399', offline: '684443903759614049', onlinephone: '777989420728975390', idlephone: "836554207342362626", dndphone: "836554469478760449" }; const spoustece = ['mares', 'mareš', 'purfie', 'denim', '<@!477202009066438668>']; const emouty = ['purfiek3:616026189269696512', 'purfiek3:616026189269696512', 'purfiek3:616026189269696512', 'purfieUchyl:576487602203525138', 'bafW:574646319696576532', 'purfiek4:616026180889477258']; const rand = max => Math.floor(Math.random() * max); @@ -30,21 +29,6 @@ module.exports = { return true; }, - // Změna rolí podle statusu - on_presenceUpdate: (bef, aft) => { - if (aft.guild.id !== '555779161067749446' || process.env.IGNORE_PRESENCE) return; - - if (!bef) bef = { status: 'offline', clientStatus: {} }; - [bef, aft].forEach(s => { - const mobile = s.clientStatus.mobile; - if (mobile && mobile !== s.clientStatus.desktop) s.status = `${mobile}phone`; - }); - if (bef.status === aft.status) return; - - aft.member.roles.add(role[aft.status]); - aft.member.roles.remove(role[bef.status]); - }, - // Zareaguje na zprávu pokud obsahuje spouštěče a pokud RNG on_message: mes => { let sance = 0; @@ -54,4 +38,4 @@ module.exports = { if (rand(3) < sance) mes.react(emouty[rand(emouty.length)]) .catch(er => console.log("neco se doebalo:", er.message)); } -} +};