summaryrefslogtreecommitdiff
path: root/src/main/richpresence.ts
blob: c50c84f33945f281bf814ddb9fc26ca8e11ae304 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Status } from "./status";
import { Client, register } from "discord-rpc"
// Only for testing as of right now -> Experimental
const ClientId = "551884486351126528";

const slogans = [
  "free OpenSource 2D MMORPG",
  "Community made",
  "Join a Server or start your own",
];
const dataSet: {
  [key: string]: { name: string; logo: string; description: string };
} = {
  "server.tmw2.org": {
    name: "Moubootaur Legends",
    logo: "tmw2",
    description: "Playing on Moubootaur Legends ⎛tmw2.org⎠",
  },
  "world.evolonline.org": {
    name: "Evol Online",
    logo: "evol",
    description: "Playing on Evol Online ⎛evolonline.org⎠",
  },
  "server.themanaworld.org": {
    name: "The Mana World",
    logo: "tmw",
    description: "Playing on The Mana World ⎛themanaworld.org⎠",
  },
  noServer: {
    name: "Manaplus",
    logo: "manaplus",
    description: "Playing on any of the M+ servers",
  },
};

let DiscordRPC_Client:Client = null;

async function setActivity() {
  const status = Status.getStatus();

  const slogan = slogans[Math.floor(Math.random() * slogans.length)];

  const data = dataSet[status.gameStatus.server];

  const details =
    status.gameStatus.server === "Launcher"
      ? "in Launcher Menu"
      : `🎮 ${data.name} 🎮`;
  const logo = data && data.logo;

  // IDEA maybe check if changed before updating to save traffic?

  DiscordRPC_Client?.setActivity({
    state: ${slogan}«`,
    details,
    largeImageKey: logo,
    largeImageText: data && data.description,
    //smallImageKey: ,
    //smallImageText: 'string',
    //partyId: 'ae488379-351d-4a4f-ad32-2b9b01c91657',
    //partySize: 1,
    //partyMax: 999,
    //matchSecret: 'string',
    //joinSecret: 'string',
    //spectateSecret: 'string',
    //startTimestamp
  }).catch(console.error);
}

export async function setup(): Promise<void>{
  register(ClientId)
  DiscordRPC_Client = await (new Client({transport: 'ipc'})).connect(ClientId)

  DiscordRPC_Client.on("ready", () => {
    setActivity();
  
    // activity can only be set every 15 seconds
    setInterval(() => {
      setActivity();
    }, 15e3);
  });

  DiscordRPC_Client.subscribe("ACTIVITY_JOIN", (secret: string) => {
    console.log("we should join with", secret);
  });
  
  DiscordRPC_Client.subscribe("ACTIVITY_SPECTATE", (secret: string) => {
    console.log("we should spectate with", secret);
  });
  
  // client.subscribe('ACTIVITY_JOIN_REQUEST', (user) => {
  //   if (user.discriminator === '1337') {
  //     client.sendJoinInvite(user);
  //   } else {
  //     client.closeJoinRequest(user);
  //   }
  // });
}


export function quit() {
  DiscordRPC_Client?.destroy();
  console.log("Shutting down Discord RPC integration");
}

process.on("unhandledRejection", console.error);