Denim-Bot/modules/status.js

90 lines
2.6 KiB
JavaScript

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 ziju = () => {
poslatData({ nejsemPoslej: !0 });
setTimeout(ziju, 60_000);
};
if (!process.env.IGNORE_PRESENCE) ziju();
module.exports = {
// Změna rolí podle statusu a odeslání statusu
on_presenceUpdate: (bef, aft) => {
if (process.env.IGNORE_PRESENCE) return;
[bef, aft] = statusOnFoun(bef, aft);
if (bef.status === aft.status) return;
if (aft.guild.id !== '555779161067749446') {
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: () => {
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].status;
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: (_, aft) => {
if (!process.env.IGNORE_PRESENCE)
prepSend([[aft, statusy[statusOnFoun(null, aft.presence)[1].status]]]);
}
};