presence.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const presence = new Presence({
  2. clientId: '864573021762224129',
  3. })
  4. const startTimestamp = Math.floor(Date.now() / 1e3)
  5. const whitespaceRegex = /^\s*|\n/gm
  6. presence.on('UpdateData', () => {
  7. const [, pathCheck, mainPath, namespace] = window.location.pathname.split('/')
  8. const presenceData: PresenceData = {
  9. largeImageKey: 'https://cdn.rcd.gg/PreMiD/websites/T/TV%20Tropes/assets/logo.png',
  10. startTimestamp,
  11. }
  12. function mainWikiPathDetails(namespace: string) {
  13. if (namespace === 'Main') {
  14. presenceData.details = 'Viewing a TV Trope'
  15. presenceData.state = document
  16. .querySelector('.entry-title')
  17. ?.textContent
  18. ?.replace(whitespaceRegex, '')
  19. }
  20. else {
  21. const titleElement = document.querySelector('.entry-title')
  22. presenceData.details = 'Viewing a page'
  23. presenceData.state = `${titleElement
  24. ?.querySelector('strong')
  25. ?.textContent
  26. ?.replace(
  27. / \/ $/,
  28. '',
  29. )} / ${titleElement?.childNodes[2]?.textContent?.replace(
  30. whitespaceRegex,
  31. '',
  32. )}`
  33. }
  34. }
  35. if (pathCheck === 'pmwiki') {
  36. switch (mainPath) {
  37. case 'pmwiki.php': {
  38. mainWikiPathDetails(namespace!)
  39. break
  40. }
  41. case 'profile.php': {
  42. presenceData.details = 'Viewing User Profile'
  43. break
  44. }
  45. case 'topics.php': {
  46. presenceData.details = 'Browsing the Forum Homepage'
  47. break
  48. }
  49. case 'conversations.php': {
  50. presenceData.details = 'Browsing Forum Category'
  51. presenceData.state = document
  52. .querySelector('.entry-title')
  53. ?.childNodes[2]
  54. ?.textContent
  55. ?.replace(whitespaceRegex, '')
  56. break
  57. }
  58. case 'posts.php': {
  59. presenceData.details = 'Browsing Forum Post'
  60. presenceData.state = document
  61. .querySelector('.entry-title')
  62. ?.textContent
  63. ?.replace(whitespaceRegex, '')
  64. break
  65. }
  66. case 'wysiwyg_source_editor.php': {
  67. const searchParams = new URLSearchParams(window.location.search)
  68. presenceData.details = 'Editing TV Trope Page'
  69. presenceData.state = `${searchParams.get(
  70. 'groupname',
  71. )} / ${searchParams.get('title')}`
  72. break
  73. }
  74. case 'createconversation.php': {
  75. presenceData.details = 'Creating a Forum Post'
  76. break
  77. }
  78. default: {
  79. presenceData.details = 'Browsing TV Tropes'
  80. presenceData.state = document.title
  81. }
  82. }
  83. }
  84. else if (pathCheck) {
  85. mainWikiPathDetails(pathCheck)
  86. }
  87. else {
  88. presenceData.details = 'Browsing TV Tropes Homepage'
  89. }
  90. if (presenceData.details)
  91. presence.setActivity(presenceData)
  92. })