Discord integration for Gamecraft
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyPlugin.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Reflection;
  3. using IllusionPlugin;
  4. // using GamecraftModdingAPI;
  5. using GamecraftModdingAPI.Commands;
  6. using DiscordRPC;
  7. namespace GamecraftRPC
  8. {
  9. public class MyPlugin : IPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin'
  10. {
  11. public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
  12. public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  13. private static readonly string CLIENT_ID = "692733325902872619";
  14. private DiscordRpcClient discordRPC;
  15. // called when Gamecraft shuts down
  16. public void OnApplicationQuit()
  17. {
  18. // Shutdown this mod
  19. GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has shutdown");
  20. // Shutdown the Gamecraft modding API last
  21. GamecraftModdingAPI.Main.Shutdown();
  22. }
  23. // called when Gamecraft starts up
  24. public void OnApplicationStart()
  25. {
  26. // Initialize the Gamecraft modding API first
  27. GamecraftModdingAPI.Main.Init();
  28. // Initialize this mod
  29. SimpleCustomCommandEngine rpcCommand = new SimpleCustomCommandEngine(
  30. () => { }, // TODO: command action
  31. // also try using CommandLogWarning or CommandLogError instead of CommandLog
  32. "RPC", // command name (used to invoke it in the console)
  33. "DiscordRPC Experimental command" // command description (displayed when help command is executed)
  34. ); // this command can also be executed using the Command Computer
  35. // register the command so the modding API knows about it
  36. CommandManager.AddCommand(rpcCommand);
  37. discordRPC = new DiscordRpcClient(CLIENT_ID);
  38. discordRPC.OnReady += (sender, e) =>
  39. {
  40. GamecraftModdingAPI.Utility.Logging.LogDebug($"Received Ready from user {e.User.Username}");
  41. };
  42. discordRPC.OnPresenceUpdate += (sender, e) =>
  43. {
  44. GamecraftModdingAPI.Utility.Logging.LogDebug($"Received Update! {e.Presence}");
  45. };
  46. discordRPC.Initialize();
  47. discordRPC.SetPresence(new RichPresence()
  48. {
  49. Details = "Gamecraft Rich Presence",
  50. State = "Gamecraft RPC test",
  51. Assets = new DiscordRPC.Assets()
  52. {
  53. LargeImageKey = "image_large",
  54. LargeImageText = "Testing stuff",
  55. SmallImageKey = "image_small"
  56. }
  57. });
  58. GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has started up");
  59. }
  60. // unused methods
  61. public void OnFixedUpdate() { } // called once per physics update
  62. public void OnLevelWasInitialized(int level) { } // called after a level is initialized
  63. public void OnLevelWasLoaded(int level) { } // called after a level is loaded
  64. public void OnUpdate() // called once per rendered frame (frame update)
  65. {
  66. if (discordRPC != null ) discordRPC.Invoke();
  67. }
  68. }
  69. }