presence.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const presence = new Presence({
  2. clientId: "651930315279040512",
  3. });
  4. /**
  5. * Truncate a string by "..." if needed
  6. * @param {String} text The string to truncate
  7. * @param {Number} length The desized length
  8. */
  9. function truncateString(text: string, length: number): string {
  10. if (text.length > length) return `${text.substring(0, length - 3)}...`;
  11. else return text;
  12. }
  13. presence.on("UpdateData", async () => {
  14. const presenceData: PresenceData = {
  15. largeImageKey:
  16. "https://cdn.rcd.gg/PreMiD/websites/J/JeuxVideo.com/assets/logo.jpg",
  17. };
  18. if (document.location.pathname === "/")
  19. presenceData.details = "Page d'accueil";
  20. else if (document.location.pathname.includes("/news/")) {
  21. presenceData.details = "Lis une actualité";
  22. presenceData.state = truncateString(
  23. document.querySelector("div.titre-wrapper").textContent,
  24. 128
  25. );
  26. } else if (document.location.pathname.includes("/videos/")) {
  27. presenceData.details = "Regarde une vidéo";
  28. presenceData.state = truncateString(
  29. document.querySelector("div.titre-video").textContent,
  30. 128
  31. );
  32. } else if (document.location.pathname.includes("/test/")) {
  33. presenceData.details = "Lis un test";
  34. presenceData.state = `${truncateString(
  35. document.querySelector(".gameHeaderBanner__title").textContent,
  36. 128
  37. )} (${
  38. document.querySelector(".bloc-avis-testeur > .note > strong").textContent
  39. }/20)`;
  40. } else if (document.location.pathname.includes("/messages-prives/"))
  41. presenceData.details = "Lis ses MP";
  42. else if (document.location.pathname.includes("/forums/0-")) {
  43. presenceData.details = truncateString(
  44. document.querySelector(
  45. "#forum-main-col > .titre-head-bloc > .titre-bloc-forum"
  46. ).textContent,
  47. 64
  48. );
  49. presenceData.state = document.querySelector(
  50. ".panel-heading > .nb-connect-fofo"
  51. ).textContent;
  52. } else if (document.location.pathname.includes("/forums/")) {
  53. presenceData.details = truncateString(
  54. document.querySelector(
  55. ".bloc-fil-ariane-crumb-forum > .fil-ariane-crumb > span:last-of-type > a"
  56. ).textContent,
  57. 64
  58. );
  59. presenceData.state = truncateString(
  60. document.querySelector(
  61. "#forum-main-col > .titre-head-bloc > .titre-bloc-forum > #bloc-title-forum"
  62. ).textContent,
  63. 128
  64. );
  65. }
  66. if (presenceData.details) presence.setActivity(presenceData);
  67. else presence.setActivity();
  68. });