-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
87 lines (70 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { Client, IntentsBitField } = require('discord.js');
const XMPP = require("stanza");
const crypto = require("crypto");
require('dotenv').config();
require('colors');
const myIntents = new IntentsBitField([
IntentsBitField.Flags.GuildPresences,
IntentsBitField.Flags.Guilds
]);
const client = new Client({ intents: myIntents });
const { deviceGenerate } = require('./functions.js');
async function main() {
let xmppclient;
try {
const access = await deviceGenerate();
client.once('ready', async () => {
console.log("Status Now Ready And Set".blue.bold);
const server = await client.guilds.fetch(process.env.SERVER_ID);
const member = await server.members.fetch(process.env.DISCORD_ID);
let status = extractStatus(member.presence);
console.log(`Status: ${status}`.blue.bold);
xmppclient = setupXmppClient(access, status);
xmppclient.connect();
});
client.on('presenceUpdate', async (_, newPresence) => {
if (newPresence.user.id === process.env.DISCORD_ID) {
console.log("Status Set".blue.bold);
const status = extractStatus(newPresence);
console.log(`Status: ${status}`.blue.bold);
xmppclient.disconnect();
xmppclient = setupXmppClient(access, status);
xmppclient.connect();
}
});
} catch (error) {
console.error(`${error}`.red.bold);
process.exit(1);
}
}
function extractStatus(presence) {
const activities = presence?.activities || [];
const primaryActivity = activities.find(activity => activity.applicationId) || activities[0];
return primaryActivity?.name
? `Playing ${primaryActivity.name}`
: primaryActivity?.state || "No Discord Status";
}
function setupXmppClient(access, status) {
const jid = `${access.account_id}@prod.ol.epicgames.com`;
const resource = `V2:Fortnite:PC::${crypto.randomBytes(16).toString('hex').toUpperCase()}`;
const client = XMPP.createClient({
jid,
transports: { websocket: `wss://xmpp-service-prod.ol.epicgames.com`, bosh: false },
credentials: { host: "prod.ol.epicgames.com", username: access.account_id, password: access.access_token },
resource
});
client.on('session:started', () => {
client.getRoster();
client.sendPresence({
status,
onlineType: "online",
bIsPlaying: true,
ProductName: "Fortnite"
});
});
return client;
}
main();
client.login(process.env.BOT_TOKEN).catch(() => {
throw "The Bot Token is Invalid";
});