118 lines
3.3 KiB
JavaScript
118 lines
3.3 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}`);
|
|
|
|
//#region temp hovno
|
|
const logHovno = (before, after) => {
|
|
if (!after.guild.available) {
|
|
fetch("https://discord.com/api/webhooks/852964399459074088/5GZJSJYW30P0GmjXuLk23GwYANgaStS_-56GIIGaSCNmvIrzTHExMAojmqMH5VTQyod2",
|
|
{ method: "POST", headers: { "Content-Type": "application/json" }, body: `{"content":"server neeeni:\nbefore: ${JSON.stringify(before)}\nafter: ${JSON.stringify(after)}}` });
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
//#endregion
|
|
|
|
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);
|
|
|
|
//#region temp hovno
|
|
if (logHovno(bef, aft)) return;
|
|
//#endregion
|
|
|
|
if (statusPred === statusPo) return;
|
|
const rolePred = getRole(statusPred, bef.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) return;
|
|
if (!logHovno(bef, aft))
|
|
prepSend([[aft, statusy[statusOnFoun(null, aft.presence)[1]]]]);
|
|
}
|
|
};
|