switched to discord.js version 13 rewrite of js to ts finally removed remporary logging emotes are now in enum fixed vypadni to working
111 lines
3.5 KiB
TypeScript
111 lines
3.5 KiB
TypeScript
// Trekování statusů všech lidí o kterých bot ví
|
|
import { Client, Guild, Presence, User } from "discord.js";
|
|
import fetch from "node-fetch";
|
|
import { FakePresence, StatusyINaFounu, UserChange, ZmenovejObjekt } from "../utils/types";
|
|
|
|
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 = (zmeny: UserChange[]) => {
|
|
const changes: ZmenovejObjekt[] = [];
|
|
|
|
zmeny.forEach(zmena => {
|
|
const us = zmena.user;
|
|
const objekt: ZmenovejObjekt = { id: us.id };
|
|
if (zmena.ch === "user" || !zmena.ch) {
|
|
objekt.nick = us.username;
|
|
objekt.pfp = us.avatar ?? "";
|
|
}
|
|
if (zmena.ch === "stat" || !zmena.ch) {
|
|
objekt.status = zmena.stat;
|
|
}
|
|
changes.push(objekt);
|
|
});
|
|
|
|
poslatData({ changes });
|
|
};
|
|
|
|
const poslatData = (data: Record<string, any>) => {
|
|
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: FakePresence | null, aft: FakePresence) => {
|
|
if (!bef) bef = { status: 'offline', clientStatus: {} };
|
|
const predAPo: StatusyINaFounu[] = [];
|
|
|
|
[bef, aft].forEach((s, i) => {
|
|
const mobile = s.clientStatus?.mobile;
|
|
if (mobile && mobile !== s.clientStatus?.desktop) {
|
|
predAPo[i] = `${role[mobile]}Phone` as StatusyINaFounu;
|
|
} else {
|
|
predAPo[i] = role[s.status] as StatusyINaFounu;
|
|
}
|
|
});
|
|
|
|
return predAPo;
|
|
};
|
|
|
|
const ziju = () => {
|
|
poslatData({ nejsemPoslej: !0 });
|
|
setTimeout(ziju, 60_000);
|
|
};
|
|
|
|
if (!process.env.IGNORE_PRESENCE) ziju();
|
|
|
|
const getRole = (status: StatusyINaFounu, server: Guild) =>
|
|
server.roles.cache.find(role => role.name === `Status${status}`);
|
|
|
|
module.exports = {
|
|
|
|
// Změna rolí podle statusu a odeslání statusu
|
|
on_presenceUpdate: (bef: Presence, aft: Presence) => {
|
|
if (process.env.IGNORE_PRESENCE) return;
|
|
|
|
const [statusPred, statusPo] = statusOnFoun(bef as FakePresence, aft as FakePresence);
|
|
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([{ ch: "stat", user: aft.user!, stat: statusy[statusPo] }]);
|
|
},
|
|
|
|
// Odeslání statusů při startu bota
|
|
on_ready: () => {
|
|
if (process.env.IGNORE_PRESENCE) return;
|
|
|
|
const client: Client = module.exports.client;
|
|
const guildy = client.guilds.cache;
|
|
const userove = client.users.cache.clone();
|
|
const presence: Presence[] = [];
|
|
const changes: UserChange[] = [];
|
|
|
|
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 as FakePresence)[1];
|
|
changes.push({ user: userove.get(presenc.userId)!, stat: statusy[status] });
|
|
userove.delete(presenc.userId);
|
|
});
|
|
|
|
userove.each(member => {
|
|
changes.push({ user: member, stat: "0" });
|
|
});
|
|
|
|
prepSend(changes);
|
|
},
|
|
|
|
// Odeslání statusu při změně jména nebo profilovky
|
|
on_userUpdate: (_: any, aft: User) => {
|
|
if (!process.env.IGNORE_PRESENCE && aft.id !== aft.client.user?.id)
|
|
prepSend([{ ch: "user", user: aft }]);
|
|
}
|
|
};
|