presence.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const presence = new Presence({
  2. clientId: "813518808634621952",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. let details: string,
  6. state: string,
  7. smallImageKey: string,
  8. activityName: HTMLDivElement,
  9. userCount: number;
  10. const noGames: string[] = [
  11. "Video Chat",
  12. "Web view",
  13. "Paint",
  14. "Table",
  15. "Virtual room",
  16. "Watch Party",
  17. ];
  18. const enum Assets {
  19. Kosmimain = "https://cdn.rcd.gg/PreMiD/websites/K/kosmi.io/assets/0.png",
  20. Vcall = "https://cdn.rcd.gg/PreMiD/websites/K/kosmi.io/assets/1.png",
  21. Gamepad = "https://cdn.rcd.gg/PreMiD/websites/K/kosmi.io/assets/2.png",
  22. Paintbrush = "https://cdn.rcd.gg/PreMiD/websites/K/kosmi.io/assets/3.png",
  23. }
  24. presence.on("UpdateData", async () => {
  25. if (location.pathname === "/") {
  26. details = "Viewing the Home Page...";
  27. location.hash === "#contactScreen"
  28. ? (state = "Viewing Contact Information")
  29. : (state = null);
  30. smallImageKey = Assets.Reading;
  31. } else if (location.pathname === "/lobby") {
  32. details = "Browsing public rooms...";
  33. state = null;
  34. smallImageKey = Assets.Search;
  35. } else if (location.pathname.includes("room")) {
  36. /*Try to get Metadata: Viewers, Game Name, if the elements don't exist, assume the user is on the indexpage */
  37. try {
  38. activityName = document.querySelector<HTMLDivElement>(
  39. 'div[class="appTitle-WJ3"]'
  40. );
  41. userCount =
  42. parseInt(
  43. document
  44. .querySelectorAll<HTMLDivElement>(
  45. 'div[class="ui tabular swipableMenu-xjk menu"] > a'
  46. )[1]
  47. .textContent.trim(),
  48. 10
  49. ) - 1;
  50. } catch {
  51. return;
  52. }
  53. /* Re-set the index status, as user is likely on the index page again and the Metadata objects exist now */
  54. details = "Choosing an activity";
  55. smallImageKey = Assets.Reading;
  56. /* Show the usercount in the lower text */
  57. state = userCount === 0 ? "Alone" : `With ${userCount} others`;
  58. /* This is executed if the user plays a game that is not in the "Special Activities" Array */
  59. if (activityName && !noGames.includes(activityName.textContent)) {
  60. details = `Playing ${activityName.textContent}`;
  61. smallImageKey = Assets.Gamepad;
  62. } else if (activityName && noGames.includes(activityName.textContent)) {
  63. switch (
  64. activityName.textContent /* Proper Grammar for the Activities */
  65. ) {
  66. case "Watch Party":
  67. details = `In a ${activityName.textContent}`;
  68. smallImageKey = Assets.Live;
  69. break;
  70. case "Paint":
  71. details = "Painting";
  72. smallImageKey = Assets.Paintbrush;
  73. break;
  74. case "Table":
  75. details = "At the Table";
  76. break;
  77. default:
  78. details = `In a ${activityName.textContent}`;
  79. smallImageKey = Assets.Vcall;
  80. break;
  81. }
  82. }
  83. }
  84. const presenceData: PresenceData = {
  85. largeImageKey:
  86. "https://cdn.rcd.gg/PreMiD/websites/K/kosmi.io/assets/logo.png",
  87. smallImageKey,
  88. details,
  89. state,
  90. startTimestamp: browsingTimestamp,
  91. };
  92. if (presenceData.details) presence.setActivity(presenceData);
  93. else presence.setActivity();
  94. });