presence.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const presence = new Presence({
  2. clientId: "945791515169521694",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. const enum Assets {
  6. Logo = "https://cdn.rcd.gg/PreMiD/websites/H/Homestuck%5E2/assets/logo.png",
  7. Heart = "https://cdn.rcd.gg/PreMiD/websites/H/Homestuck%5E2/assets/0.png",
  8. }
  9. presence.on("UpdateData", async () => {
  10. const presenceData: PresenceData = {
  11. largeImageKey: Assets.Logo,
  12. startTimestamp: browsingTimestamp,
  13. },
  14. { pathname } = document.location,
  15. pathArr = pathname.split("/");
  16. switch (pathArr[1]) {
  17. case "":
  18. presenceData.details = "Viewing the home page";
  19. break;
  20. case "story":
  21. presenceData.details = "Reading Homestuck^2";
  22. presenceData.smallImageKey = Assets.Heart;
  23. if (!pathArr[2]) presenceData.state = `Page 1 of ${await getPages()}`;
  24. else presenceData.state = `Page ${pathArr[2]} of ${await getPages()}`;
  25. if (document.querySelector("h2"))
  26. presenceData.smallImageText = document.querySelector("h2").textContent;
  27. else {
  28. presenceData.smallImageText =
  29. document.querySelector("title").textContent;
  30. }
  31. presenceData.buttons = [
  32. {
  33. label: "Read Along",
  34. url: `https://www.homestuck2.com${pathname}`,
  35. },
  36. ];
  37. break;
  38. case "bonus":
  39. presenceData.details = "Viewing bonus content";
  40. switch (pathArr[2]) {
  41. case "catnapped":
  42. presenceData.details = "Reading Catnapped";
  43. presenceData.smallImageKey = Assets.Heart;
  44. presenceData.state = `Page ${pathArr[3]} of 28`;
  45. presenceData.smallImageText =
  46. document.querySelector("h2").textContent;
  47. presenceData.buttons = [
  48. {
  49. label: "Read Along",
  50. url: `https://www.homestuck2.com${pathname}`,
  51. },
  52. ];
  53. break;
  54. case "a-treatise-on-representational-democracy":
  55. presenceData.details =
  56. "Reading A Treatise on Representational Democracy";
  57. presenceData.smallImageKey = Assets.Heart;
  58. presenceData.state = `Page ${pathArr[3]} of 13`;
  59. presenceData.smallImageText =
  60. document.querySelector("h2").textContent;
  61. presenceData.buttons = [
  62. {
  63. label: "Read Along",
  64. url: `https://www.homestuck2.com${pathname}`,
  65. },
  66. ];
  67. break;
  68. case "diamonds-dames-and-dads":
  69. presenceData.details = "Reading Diamonds, Dames, and Dads";
  70. presenceData.smallImageKey = Assets.Heart;
  71. presenceData.state = `Page ${pathArr[3]} of 46`;
  72. presenceData.smallImageText =
  73. document.querySelector("h2").textContent;
  74. presenceData.buttons = [
  75. {
  76. label: "Read Along",
  77. url: `https://www.homestuck2.com${pathname}`,
  78. },
  79. ];
  80. break;
  81. case "a-threat-sensed":
  82. presenceData.details = "Reading A Threat, Sensed";
  83. presenceData.smallImageKey = Assets.Heart;
  84. presenceData.state = `Page ${pathArr[3]} of 13`;
  85. presenceData.smallImageText =
  86. document.querySelector("h2").textContent;
  87. presenceData.buttons = [
  88. {
  89. label: "Read Along",
  90. url: `https://www.homestuck2.com${pathname}`,
  91. },
  92. ];
  93. break;
  94. case "the-influencers":
  95. presenceData.details = "Reading The Influencers";
  96. presenceData.smallImageKey = Assets.Heart;
  97. presenceData.state = `Page ${pathArr[3]} of 34`;
  98. presenceData.smallImageText =
  99. document.querySelector("h2").textContent;
  100. presenceData.buttons = [
  101. {
  102. label: "Read Along",
  103. url: `https://www.homestuck2.com${pathname}`,
  104. },
  105. ];
  106. break;
  107. default:
  108. break;
  109. }
  110. break;
  111. case "about":
  112. presenceData.details = "Viewing the about page";
  113. break;
  114. case "contacts":
  115. presenceData.details = "Viewing the contact information";
  116. break;
  117. case "credits":
  118. presenceData.details = "Viewing the credits";
  119. break;
  120. case "log":
  121. presenceData.details = "Viewing the adventure log";
  122. break;
  123. case "privacy-policy":
  124. presenceData.details = "Viewing the privacy policy";
  125. break;
  126. case "recap":
  127. presenceData.details = "Viewing recap";
  128. break;
  129. default:
  130. presenceData.details = "Viewing an unsupported page";
  131. break;
  132. }
  133. if (presenceData.details) presence.setActivity(presenceData);
  134. else presence.setActivity();
  135. });
  136. async function getPages() {
  137. const response = await fetch(
  138. `https://api.rss2json.com/v1/api.json?rss_url=${"https://homestuck2.com/story/rss"}`
  139. ),
  140. data = await response.json();
  141. return data.items[0].title;
  142. }