Gamecraft-Discord connection. It can send and receive messages in a specific channel.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

151 wiersze
4.9KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Threading;
  8. using GamecraftModdingAPI.Engines;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. using RobocraftX.Common.Input;
  12. using RobocraftX.Common.Utilities;
  13. using RobocraftX.StateSync;
  14. using Svelto.ECS;
  15. using Unity.Jobs;
  16. using uREPL;
  17. namespace GCDC
  18. {
  19. public class DiscordClient
  20. {
  21. private string _token;
  22. private bool _running;
  23. private Thread _rect;
  24. private readonly Queue<string> messages = new Queue<string>();
  25. private readonly GCDCPlugin plugin;
  26. public DiscordClient(GCDCPlugin plugin) => this.plugin = plugin;
  27. public void Ready()
  28. {
  29. if (File.Exists("gcdc.json"))
  30. {
  31. var jo = JObject.Load(new JsonTextReader(File.OpenText("gcdc.json")));
  32. _token = jo["token"]?.Value<string>();
  33. }
  34. if (_token != null)
  35. Start();
  36. }
  37. public void Setup(string tokenOrChannel)
  38. {
  39. if (!tokenOrChannel.Contains("-"))
  40. {
  41. if (!long.TryParse(tokenOrChannel, out _))
  42. {
  43. Log.Error("Bad format for channel ID.");
  44. return;
  45. }
  46. Process.Start(
  47. "https://discordapp.com/oauth2/authorize?client_id=680138144812892371&redirect_uri=https%3A%2F%2Fgcdc.herokuapp.com%2Fapi%2Fusers%2Fregister&response_type=code&scope=identify&state=" +
  48. tokenOrChannel);
  49. Log.Output(
  50. "Please authorize the GCDC app on the page that should open. This connection is only used to avoid account spam and to display your Discord name.");
  51. }
  52. else
  53. {
  54. try
  55. {
  56. if (JObject.Parse(WebUtils.Request("users/get?token=" + tokenOrChannel))["response"]?.Value<string>() == "OK")
  57. {
  58. _token = tokenOrChannel;
  59. var jo = new JObject {["token"] = tokenOrChannel};
  60. File.WriteAllText("gcdc.json", jo.ToString());
  61. Start();
  62. Log.Output(
  63. "Successfully logged in. You can now use a text block named Discord and the dc command.");
  64. }
  65. else
  66. Log.Error("Failed to verify login. Please try again.");
  67. }
  68. catch (Exception e)
  69. {
  70. Log.Error("Failed to verify login. Please try again. (Error logged.)");
  71. Console.WriteLine(e);
  72. }
  73. }
  74. }
  75. public void SendMessage(string message)
  76. {
  77. if (!_running)
  78. {
  79. Log.Error("Run dcsetup first.");
  80. return;
  81. }
  82. try
  83. {
  84. var parameters = "token=" + _token + "&message=" + message;
  85. var resp = JObject.Parse(WebUtils.Request("messages/send?" + parameters, ""));
  86. if (resp["response"]?.Value<string>() == "OK")
  87. {
  88. AddMessage("<nobr><" + resp["username"] + "> " + message);
  89. Log.Output("Message sent");
  90. }
  91. else
  92. Log.Error("Failed to send message");
  93. }
  94. catch (Exception e)
  95. {
  96. Log.Error("Failed to send message (error logged).");
  97. Console.WriteLine(e);
  98. }
  99. }
  100. public void Start()
  101. {
  102. if (_running) return;
  103. _running = true;
  104. _rect = new Thread(() =>
  105. {
  106. Console.WriteLine("Starting DC receiver thread...");
  107. while (_running)
  108. {
  109. try
  110. {
  111. string resp = WebUtils.Request("messages/get?token=" + _token);
  112. var jo = JObject.Parse(resp);
  113. AddMessage("<nobr><" + jo["username"] + "> " + jo["message"]);
  114. }
  115. catch (WebException)
  116. {
  117. // ignored
  118. }
  119. catch (ThreadInterruptedException)
  120. {
  121. break;
  122. }
  123. }
  124. }) {Name = "DC Receiver Thread"};
  125. _rect.Start();
  126. }
  127. public void AddMessage(string message)
  128. {
  129. messages.Enqueue(message);
  130. if (messages.Count > 10)
  131. messages.Dequeue();
  132. plugin.Update(messages);
  133. }
  134. public void Stop()
  135. {
  136. _running = false;
  137. _rect.Interrupt();
  138. }
  139. public void Update() => plugin.Update(messages);
  140. }
  141. }