Denim-Bot/modules/status.js
2021-06-27 00:27:46 +02:00

101 lines
2.7 KiB
JavaScript

const fetch = require("node-fetch");
const role = { online: "Online", idle: "Idle", dnd: "DND", offline: "Offline" };
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: {} };
const predAPo = ["", ""];
[bef, aft].forEach((s, i) => {
const mobile = s.clientStatus.mobile;
if (mobile && mobile !== s.clientStatus.desktop) {
predAPo[i] = `${role[mobile]}Phone`;
}
else {
predAPo[i] = role[s.status];
}
});
return predAPo;
};
const ziju = () => {
poslatData({ nejsemPoslej: !0 });
setTimeout(ziju, 60_000);
};
if (!process.env.IGNORE_PRESENCE) ziju();
const getRole = (status, server) =>
server.roles.cache.find(role => role.name === `Status${status}`);
module.exports = {
// Změna rolí podle statusu a odeslání statusu
on_presenceUpdate: (bef, aft) => {
if (process.env.IGNORE_PRESENCE) return;
const [statusPred, statusPo] = statusOnFoun(bef, aft);
if (statusPred === statusPo) return;
const rolePred = getRole(statusPred, aft.guild);
const rolePo = getRole(statusPo, aft.guild);
if (rolePred) aft.member.roles.remove(rolePred);
if (rolePo) aft.member.roles.add(rolePo);
prepSend([[aft.user, statusy[statusPo]]]);
},
// Odeslání statusů při startu bota
on_ready: () => {
if (process.env.IGNORE_PRESENCE) return;
const client = module.exports.client;
const guildy = client.guilds.cache;
const memberove = client.users.cache.clone();
const presence = [];
const changes = [];
guildy.each(guilda => {
guilda.presences.cache.each(pres => {
if (!presence.filter(prs => prs.userID === pres.userID).length) presence.push(pres);
});
});
presence.forEach(presenc => {
const status = statusOnFoun(null, presenc)[1];
changes.push([memberove.get(presenc.userID), statusy[status]]);
memberove.delete(presenc.userID);
});
memberove.each(member => {
changes.push([member, 0]);
});
prepSend(changes);
},
// Odeslání statusu při změně jména nebo profilovky
on_userUpdate: (bef, aft) => {
if (!process.env.IGNORE_PRESENCE)
prepSend([[aft, statusy[statusOnFoun(null, aft.presence)[1]]]]);
}
};