presence.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const presence = new Presence({
  2. clientId: "966711989533544580",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. const enum Assets {
  6. Logo = "https://cdn.rcd.gg/PreMiD/websites/L/LiveLinkBio/assets/logo.png",
  7. }
  8. async function getStrings() {
  9. return presence.getStrings(
  10. {
  11. browse: "general.browsing",
  12. },
  13. await presence.getSetting<string>("lang").catch(() => "en")
  14. );
  15. }
  16. let strings: Awaited<ReturnType<typeof getStrings>>,
  17. oldLang: string = null;
  18. presence.on("UpdateData", async () => {
  19. const presenceData: PresenceData = {
  20. largeImageKey: Assets.Logo,
  21. startTimestamp: browsingTimestamp,
  22. },
  23. [newLang, privacy, buttons] = await Promise.all([
  24. presence.getSetting<string>("lang").catch(() => "en"),
  25. presence.getSetting<boolean>("privacy"),
  26. presence.getSetting<boolean>("buttons"),
  27. ]),
  28. { href, pathname } = document.location;
  29. if (oldLang !== newLang || !strings) {
  30. oldLang = newLang;
  31. strings = await getStrings();
  32. }
  33. if (privacy) {
  34. presenceData.details = strings.browse;
  35. presence.setActivity(presenceData);
  36. return;
  37. }
  38. let title: string;
  39. const type = document.querySelector(
  40. "body > main > section > div.row.mb-4 > div.col-12.col-lg.d-flex.align-items-center.mb-3.mb-lg-0 > h1"
  41. ),
  42. active =
  43. document.querySelector('[class="nav-link active"]') ??
  44. document.querySelector('[class="active"]');
  45. switch (pathname.split("/")[1]) {
  46. case "": {
  47. presenceData.details = "Homepage";
  48. break;
  49. }
  50. case "affiliate": {
  51. presenceData.buttons = [
  52. {
  53. label: "View Affiliates",
  54. url: href,
  55. },
  56. ];
  57. presenceData.details = "Viewing affiliates";
  58. break;
  59. }
  60. case "dashboard": {
  61. presenceData.details = "Viewing the dashboard";
  62. presenceData.buttons = [
  63. {
  64. label: "View Dashboard",
  65. url: href,
  66. },
  67. ];
  68. break;
  69. }
  70. case "link": {
  71. presenceData.state =
  72. document.querySelector('[aria-expanded="true"]')?.textContent ?? "";
  73. if (pathname.endsWith("statistics")) {
  74. presenceData.details = `Viewing statistics of link: ${
  75. document.querySelector('[id="link_url"]')?.textContent
  76. }`;
  77. } else {
  78. presenceData.details = `Editing link: ${
  79. document.querySelector("#link_url").textContent
  80. }`;
  81. }
  82. break;
  83. }
  84. case "tools": {
  85. if (active) presenceData.details = `Using ${active.textContent}`;
  86. else presenceData.details = "Viewing all tools";
  87. break;
  88. }
  89. default: {
  90. if (document.querySelector("body").className.includes("open")) {
  91. title = document
  92. .querySelectorAll('[class="modal-title"]')[1]
  93. .textContent.replace("Edit", "");
  94. presenceData.details = title;
  95. presenceData.state =
  96. document.querySelector<HTMLInputElement>("#update_name").value;
  97. } else if (type && !active) {
  98. title = type.textContent.trim();
  99. presenceData.buttons = [
  100. {
  101. label: "View All",
  102. url: href,
  103. },
  104. ];
  105. presenceData.details = `Viewing all ${title}`;
  106. } else if (active) presenceData.details = active.textContent.trim();
  107. }
  108. }
  109. if (!buttons && presenceData.buttons) delete presenceData.buttons;
  110. if (presenceData.details) presence.setActivity(presenceData);
  111. else presence.setActivity();
  112. });