presence.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const presence = new Presence({
  2. //The client ID of the Application created at https://discordapp.com/developers/applications
  3. clientId: "000000000000000000",
  4. }),
  5. //You can use this to get translated strings in their browser language
  6. strings = presence.getStrings({
  7. play: "presence.playback.playing",
  8. pause: "presence.playback.paused",
  9. }),
  10. browsingTimestamp = Math.floor(Date.now() / 1000); // Here you generate the time someone is spending on the page. You divivde the miliseconds to seconds (/ 1000)
  11. const enum Assets { // An Enum for collecting all images (that aren't loaded on the site or are better quality for usage for the presence.
  12. Logo = "", // You should the logo link in here
  13. }
  14. /*
  15. function myOutsideHeavyLiftingFunction(){
  16. //Grab and process all your data here
  17. // element grabs //
  18. // api calls //
  19. // variable sets //
  20. }
  21. setInterval(myOutsideHeavyLiftingFunction, 10000);
  22. //Run the function separate from the UpdateData event every 10 seconds to get and set the variables which UpdateData picks up
  23. */
  24. presence.on("UpdateData", async () => {
  25. /*UpdateData is always firing, and therefore should be used as your refresh cycle, or `tick`. This is called several times a second where possible.
  26. It is recommended to set up another function outside of this event function which will change variable values and do the heavy lifting if you call data from an API.*/
  27. const presenceData: PresenceData = {
  28. //The large image on the presence. This can be a key of an image that has been added to the enum Assets.
  29. largeImageKey: Assets.Logo, // Assets enum key or image url
  30. //The small image on the presence. This can be a key of an image that has been added to the enum Assets.
  31. smallImageKey: "", // Assets enum key or image url.
  32. //The text which is displayed when hovering over the small image
  33. smallImageText: "Some hover text",
  34. //The upper section of the presence text
  35. details: "Browsing Page Name",
  36. //The lower section of the presence text
  37. state: "Reading section A",
  38. //The unix epoch timestamp for when to start counting from
  39. startTimestamp: browsingTimestamp,
  40. //If you want to show Time Left instead of Elapsed, this is the unix epoch timestamp at which the timer ends
  41. endTimestamp: 3133700400000,
  42. //Optionally you can set a largeImageKey here and change the rest as variable subproperties, for example presenceData.type = "blahblah"; type examples: details, state, etc.
  43. };
  44. //Update the presence with all the values from the presenceData object
  45. if (presenceData.details) presence.setActivity(presenceData);
  46. //Update the presence with no data, therefore clearing it and making the large image the Discord Application icon, and the text the Discord Application name
  47. else presence.setActivity();
  48. });