DiscordIntegrationModule.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using DiscordRPC;
  2. using Ryujinx.Common;
  3. using Ryujinx.Ui.Common.Configuration;
  4. namespace Ryujinx.Ui.Common
  5. {
  6. public static class DiscordIntegrationModule
  7. {
  8. private const string Description = "A simple, experimental Nintendo Switch emulator.";
  9. private const string CliendId = "568815339807309834";
  10. private static DiscordRpcClient _discordClient;
  11. private static RichPresence _discordPresenceMain;
  12. public static void Initialize()
  13. {
  14. _discordPresenceMain = new RichPresence
  15. {
  16. Assets = new Assets
  17. {
  18. LargeImageKey = "ryujinx",
  19. LargeImageText = Description
  20. },
  21. Details = "Main Menu",
  22. State = "Idling",
  23. Timestamps = Timestamps.Now,
  24. Buttons = new Button[]
  25. {
  26. new Button()
  27. {
  28. Label = "Website",
  29. Url = "https://ryujinx.org/"
  30. }
  31. }
  32. };
  33. ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
  34. }
  35. private static void Update(object sender, ReactiveEventArgs<bool> evnt)
  36. {
  37. if (evnt.OldValue != evnt.NewValue)
  38. {
  39. // If the integration was active, disable it and unload everything
  40. if (evnt.OldValue)
  41. {
  42. _discordClient?.Dispose();
  43. _discordClient = null;
  44. }
  45. // If we need to activate it and the client isn't active, initialize it
  46. if (evnt.NewValue && _discordClient == null)
  47. {
  48. _discordClient = new DiscordRpcClient(CliendId);
  49. _discordClient.Initialize();
  50. _discordClient.SetPresence(_discordPresenceMain);
  51. }
  52. }
  53. }
  54. public static void SwitchToPlayingState(string titleId, string titleName)
  55. {
  56. _discordClient?.SetPresence(new RichPresence
  57. {
  58. Assets = new Assets
  59. {
  60. LargeImageKey = "game",
  61. LargeImageText = titleName,
  62. SmallImageKey = "ryujinx",
  63. SmallImageText = Description,
  64. },
  65. Details = $"Playing {titleName}",
  66. State = (titleId == "0000000000000000") ? "Homebrew" : titleId.ToUpper(),
  67. Timestamps = Timestamps.Now,
  68. Buttons = new Button[]
  69. {
  70. new Button()
  71. {
  72. Label = "Website",
  73. Url = "https://ryujinx.org/"
  74. }
  75. }
  76. });
  77. }
  78. public static void SwitchToMainMenu()
  79. {
  80. _discordClient?.SetPresence(_discordPresenceMain);
  81. }
  82. public static void Exit()
  83. {
  84. _discordClient?.Dispose();
  85. }
  86. }
  87. }