Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Newtonsoft.Json;
  4. using Oxide.Core;
  5. using Oxide.Core.Libraries.Covalence;
  6. using Oxide.Ext.Discord;
  7. using Oxide.Ext.Discord.Attributes;
  8. using Oxide.Ext.Discord.DiscordEvents;
  9. using Oxide.Ext.Discord.DiscordObjects;
  10. using Oxide.Ext.Discord.Exceptions;
  11. using Random = System.Random;
  12.  
  13. namespace Oxide.Plugins
  14. {
  15. [Info("Discord Rewards", "birthdates", "1.1.6")]
  16. [Description("Get rewards for joining a discord!")]
  17. public class DiscordRewards : CovalencePlugin
  18. {
  19. #region Variables
  20.  
  21. [DiscordClient] private DiscordClient Client;
  22. private Role role;
  23. private const string Perm = "discordrewards.use";
  24. private Data data;
  25.  
  26. #endregion
  27.  
  28. #region Hooks
  29.  
  30. private void Init()
  31. {
  32. LoadConfig();
  33. data = Interface.Oxide.DataFileSystem.ReadObject<Data>("Discord Rewards");
  34. permission.RegisterPermission(Perm, this);
  35. AddCovalenceCommand(_config.command, "ChatCMD");
  36. if (!_config.Wipe) Unsubscribe("OnNewSave");
  37. }
  38.  
  39. private void OnNewSave()
  40. {
  41. data = new Data();
  42. SaveData();
  43. PrintWarning("All verification data wiped.");
  44. }
  45.  
  46. private void OnServerInitialized()
  47. {
  48. try
  49. {
  50. Discord.CreateClient(this, _config.botKey);
  51. }
  52. catch (LimitedClientException)
  53. {
  54. PrintError("Too many bots open!");
  55. }
  56. }
  57.  
  58. private void Discord_Ready(Ready ready)
  59. {
  60. role = Client?.DiscordServer?.roles?.Find(a => a.id.Equals(_config.role) || a.name.Equals(_config.role));
  61. }
  62.  
  63. private void ChatCMD(IPlayer player, string command, string[] args)
  64. {
  65. if (!permission.UserHasPermission(player.Id, Perm))
  66. {
  67. player.Message(lang.GetMessage("NoPermission", this, player.Id));
  68. return;
  69. }
  70.  
  71. if (data.verified.Contains(player.Id))
  72. {
  73. player.Message(lang.GetMessage("AlreadyVerified", this, player.Id));
  74. return;
  75. }
  76.  
  77. if (data.codes.ContainsValue(player.Id))
  78. {
  79. player.Message(string.Format(lang.GetMessage("YouAlreadyHaveACodeOut", this, player.Id),
  80. data.codes.First(x => x.Value == player.Id).Key));
  81. return;
  82. }
  83.  
  84. var code = RandomString(_config.codeLength);
  85. data.codes.Add(code, player.Id);
  86. player.Message(string.Format(lang.GetMessage("Verify", this, player.Id), code));
  87. }
  88.  
  89. private readonly Random random = new Random();
  90.  
  91. public string RandomString(int length)
  92. {
  93. return new string(Enumerable.Repeat(_config.CharSet, length)
  94. .Select(s => s[random.Next(s.Length)]).ToArray());
  95. }
  96.  
  97.  
  98. private void DiscordSocket_Initialized()
  99. {
  100. if (Client == null)
  101. {
  102. PrintError("Discord bot not connected correcty!");
  103. return;
  104. }
  105.  
  106. Puts("Discord bot connected!");
  107. }
  108.  
  109. private void Discord_MessageCreate(Message message)
  110. {
  111. if (message.author.bot == true) return;
  112. Channel.GetChannel(Client, message.channel_id, c =>
  113. {
  114. if (c.type != ChannelType.DM)
  115. return;
  116. if (data.verified2.Contains(message.author.id))
  117. {
  118. message.Reply(Client, lang.GetMessage("AlreadyVerified", this));
  119. return;
  120. }
  121.  
  122. if (!data.codes.ContainsKey(message.content))
  123. {
  124. message.Reply(Client, lang.GetMessage("NotAValidCode", this));
  125. return;
  126. }
  127.  
  128. var p = players.FindPlayer(data.codes[message.content]);
  129. data.verified.Add(p.Id);
  130. data.verified2.Add(message.author.id);
  131. foreach (var s in _config.commands) server.Command(string.Format(s, p.Id));
  132. message.Reply(Client, lang.GetMessage("Success", this));
  133. data.codes.Remove(message.content);
  134. p.Message(lang.GetMessage("VerifiedInGame", this, p.Id));
  135. SaveData();
  136. if (role != null) Client.DiscordServer.AddGuildMemberRole(Client, message.author.id, role.id);
  137. });
  138. }
  139.  
  140. private void Unload()
  141. {
  142. Discord.CloseClient(Client);
  143. SaveData();
  144. }
  145.  
  146. #endregion
  147.  
  148. #region Configuration & Language
  149.  
  150. public ConfigFile _config;
  151.  
  152. protected override void LoadDefaultMessages()
  153. {
  154. lang.RegisterMessages(new Dictionary<string, string>
  155. {
  156. {"NotAValidCode", "That is not a valid code!"},
  157. {"Success", "Success you are now verified, check for you rewards in game!"},
  158. {"AlreadyVerified", "You are already verified."},
  159. {"NoPermission", "You dont have permission to do this!"},
  160. {"YouAlreadyHaveACodeOut", "You already have a code out, it is {0}"},
  161. {"Verify", "Please message the bot on our discord with {0}"},
  162. {"VerifiedInGame", "Thank you for supporting the server, here are your rewards!"}
  163. }, this);
  164. }
  165.  
  166. private void SaveData()
  167. {
  168. Interface.Oxide.DataFileSystem.WriteObject("Discord Rewards", data);
  169. }
  170.  
  171. public class Data
  172. {
  173. public Dictionary<string, string> codes = new Dictionary<string, string>();
  174. public List<string> verified = new List<string>();
  175. public List<string> verified2 = new List<string>();
  176. }
  177.  
  178. public class ConfigFile
  179. {
  180. [JsonProperty("Discord bot key (Look at documentation for how to get this)")]
  181. public string botKey;
  182.  
  183. [JsonProperty("Amount of characters in the code")]
  184. public int codeLength;
  185.  
  186. [JsonProperty("Command")] public string command;
  187.  
  188. [JsonProperty("Commands to execute when player is verified (use {0} for the player's steamid)")]
  189. public List<string> commands;
  190.  
  191. [JsonProperty("Verification Role (role given when verified)")]
  192. public string role;
  193.  
  194. [JsonProperty("Erase all verification data on wipe (new map save)?")]
  195. public bool Wipe;
  196.  
  197. [JsonProperty("Code character set (code will be generated randomly with these characters)")]
  198. public string CharSet;
  199.  
  200. public static ConfigFile DefaultConfig()
  201. {
  202. return new ConfigFile
  203. {
  204. command = "verify",
  205. botKey = "INSERT_BOT_KEY_HERE",
  206. role = "enter_role_here",
  207. commands = new List<string>
  208. {
  209. "inventory.giveto {0} stones 1000"
  210. },
  211. codeLength = 6,
  212. CharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz",
  213. Wipe = false
  214. };
  215. }
  216. }
  217.  
  218. protected override void LoadConfig()
  219. {
  220. base.LoadConfig();
  221. _config = Config.ReadObject<ConfigFile>();
  222. if (_config == null) LoadDefaultConfig();
  223. }
  224.  
  225. protected override void LoadDefaultConfig()
  226. {
  227. _config = ConfigFile.DefaultConfig();
  228. PrintWarning("Default configuration has been loaded.");
  229. }
  230.  
  231. protected override void SaveConfig()
  232. {
  233. Config.WriteObject(_config);
  234. }
  235.  
  236. #endregion
  237. }
  238. }
  239. //Generated with birthdates' Plugin Maker
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement