The easy to use and full featured Irc Bot everyone is talking about!
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.

Custom_Commands.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.IRCServices;
  5. using Combot.IRCServices.Messaging;
  6. namespace Combot.Modules.Plugins
  7. {
  8. public class Custom_Commands : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.CommandReceivedEvent += HandleCommandEvent;
  13. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  14. Bot.IRC.Message.PrivateMessageReceivedEvent += HandlePrivateMessage;
  15. Bot.IRC.Message.ChannelNoticeReceivedEvent += HandleChannelNotice;
  16. Bot.IRC.Message.PrivateNoticeReceivedEvent += HandlePrivateNotice;
  17. }
  18. public override void ParseCommand(CommandMessage command)
  19. {
  20. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  21. switch (foundCommand.Name)
  22. {
  23. case "Custom Command":
  24. string action = command.Arguments["Action"];
  25. switch (action.ToLower())
  26. {
  27. case "add":
  28. string addTrigger = command.Arguments["Trigger"];
  29. string addResponse = command.Arguments["Response"];
  30. AddCommand(command, addTrigger, addResponse);
  31. break;
  32. case "del":
  33. string delTrigger = command.Arguments["Trigger"];
  34. DeleteCommand(command, delTrigger);
  35. break;
  36. case "edit":
  37. string editTrigger = command.Arguments["Trigger"];
  38. string editResponse = command.Arguments["Response"];
  39. EditCommand(command, editTrigger, editResponse);
  40. break;
  41. case "view":
  42. if (command.Arguments.ContainsKey("Trigger"))
  43. {
  44. ViewTrigger(command, command.Arguments["trigger"]);
  45. }
  46. else
  47. {
  48. ViewTriggers(command);
  49. }
  50. break;
  51. }
  52. break;
  53. }
  54. }
  55. private void HandleChannelMessage(object sender, ChannelMessage message)
  56. {
  57. if (Enabled && !Bot.IsCommand(message.Message))
  58. {
  59. string command = Bot.GetCommand(message.Message);
  60. if (!string.IsNullOrEmpty(command))
  61. {
  62. Dictionary<string, object> foundTrigger = GetTrigger(message.Sender, command);
  63. if (foundTrigger != null)
  64. {
  65. ExecuteCommand(MessageType.Channel, message.Channel, message.Sender, foundTrigger);
  66. }
  67. }
  68. }
  69. }
  70. private void HandlePrivateMessage(object sender, PrivateMessage message)
  71. {
  72. if (Enabled && !Bot.IsCommand(message.Message))
  73. {
  74. string command = Bot.GetCommand(message.Message);
  75. if (!string.IsNullOrEmpty(command))
  76. {
  77. Dictionary<string, object> foundTrigger = GetTrigger(message.Sender, command);
  78. if (foundTrigger != null)
  79. {
  80. ExecuteCommand(MessageType.Query, message.Sender.Nickname, message.Sender, foundTrigger);
  81. }
  82. }
  83. }
  84. }
  85. private void HandleChannelNotice(object sender, ChannelNotice message)
  86. {
  87. if (Enabled && !Bot.IsCommand(message.Message))
  88. {
  89. string command = Bot.GetCommand(message.Message);
  90. if (!string.IsNullOrEmpty(command))
  91. {
  92. Dictionary<string, object> foundTrigger = GetTrigger(message.Sender, command);
  93. if (foundTrigger != null)
  94. {
  95. ExecuteCommand(MessageType.Notice, message.Channel, message.Sender, foundTrigger);
  96. }
  97. }
  98. }
  99. }
  100. private void HandlePrivateNotice(object sender, PrivateNotice message)
  101. {
  102. if (Enabled && !Bot.IsCommand(message.Message))
  103. {
  104. string command = Bot.GetCommand(message.Message);
  105. if (!string.IsNullOrEmpty(command))
  106. {
  107. Dictionary<string, object> foundTrigger = GetTrigger(message.Sender, command);
  108. if (foundTrigger != null)
  109. {
  110. ExecuteCommand(MessageType.Notice, message.Sender.Nickname, message.Sender, foundTrigger);
  111. }
  112. }
  113. }
  114. }
  115. private void AddCommand(CommandMessage command, string trigger, string response)
  116. {
  117. if (!Bot.IsCommand(trigger))
  118. {
  119. int maxTriggers = Convert.ToInt32(GetOptionValue("Max Commands"));
  120. List<Dictionary<string, object>> currentCommands = GetTriggers(command.Nick);
  121. if (currentCommands.Count < maxTriggers)
  122. {
  123. Dictionary<string, object> foundTrigger = GetTrigger(command.Nick, trigger);
  124. if (foundTrigger == null)
  125. {
  126. AddNick(command.Nick);
  127. string query = "INSERT INTO `customcommands` SET " +
  128. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  129. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nicks`.`nickname` = {2}), " +
  130. "`trigger` = {3}, " +
  131. "`response` = {4}, " +
  132. "`date_added` = {5}";
  133. Bot.Database.Execute(query, new object[] {Bot.ServerConfig.Name, Bot.ServerConfig.Name, command.Nick.Nickname, trigger, response, command.TimeStamp});
  134. string message = string.Format("You now have \u0002{0}\u0002 custom commands set.", currentCommands.Count + 1);
  135. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, message);
  136. }
  137. else
  138. {
  139. string errorMessage = string.Format("You already have a command set for \u0002{0}\u0002.", trigger);
  140. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  141. }
  142. }
  143. else
  144. {
  145. string errorMessage = string.Format("You can not have more than \u0002{0}\u0002 custom commands set. Please delete one before adding another.", maxTriggers);
  146. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  147. }
  148. }
  149. else
  150. {
  151. string errorMessage = string.Format("There is already a command for \u0002{0}\u0002.", trigger);
  152. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  153. }
  154. }
  155. private void DeleteCommand(CommandMessage command, string trigger)
  156. {
  157. Dictionary<string, object> foundTrigger = GetTrigger(command.Nick, trigger);
  158. if (foundTrigger != null)
  159. {
  160. string query = "DELETE FROM `customcommands` " +
  161. "WHERE `id` = {0}";
  162. Bot.Database.Execute(query, new object[] { foundTrigger["id"] });
  163. string message = string.Format("\u0002{0}\u0002 has been deleted.", trigger);
  164. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, message);
  165. }
  166. else
  167. {
  168. string errorMessage = string.Format("You do not have a command set for \u0002{0}\u0002.", trigger);
  169. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  170. }
  171. }
  172. private void EditCommand(CommandMessage command, string trigger, string response)
  173. {
  174. Dictionary<string, object> foundTrigger = GetTrigger(command.Nick, trigger);
  175. if (foundTrigger != null)
  176. {
  177. string query = "UPDATE `customcommands` SET " +
  178. "`response` = {0} " +
  179. "WHERE `id` = {1}";
  180. Bot.Database.Execute(query, new object[] { response, foundTrigger["id"] });
  181. string message = string.Format("\u0002{0}\u0002 now has the response: {1}", trigger, response);
  182. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, message);
  183. }
  184. else
  185. {
  186. string errorMessage = string.Format("You do not have a command set for \u0002{0}\u0002.", trigger);
  187. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  188. }
  189. }
  190. private void ViewTriggers(CommandMessage command)
  191. {
  192. List<Dictionary<string, object>> foundTriggers = GetTriggers(command.Nick);
  193. if (foundTriggers.Any())
  194. {
  195. int index = 1;
  196. foreach (Dictionary<string, object> foundTrigger in foundTriggers)
  197. {
  198. string response = string.Format("Command #{0} \u0002{1}{2}\u0002: {3}", index, Bot.ServerConfig.CommandPrefix, foundTrigger["trigger"], foundTrigger["response"]);
  199. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, response, true);
  200. index++;
  201. }
  202. }
  203. else
  204. {
  205. string errorMessage = "You do not have any custom commands.";
  206. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  207. }
  208. }
  209. private void ViewTrigger(CommandMessage command, string trigger)
  210. {
  211. Dictionary<string, object> foundTrigger = GetTrigger(command.Nick, trigger);
  212. if (foundTrigger != null)
  213. {
  214. string response = string.Format("\u0002{0}{1}\u0002: {2}", Bot.ServerConfig.CommandPrefix, foundTrigger["trigger"], foundTrigger["response"]);
  215. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, response, true);
  216. }
  217. else
  218. {
  219. string errorMessage = string.Format("You do not have a command set for \u0002{0}\u0002.", trigger);
  220. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorMessage, true);
  221. }
  222. }
  223. private List<Dictionary<string, object>> GetTriggers(Nick caller)
  224. {
  225. string search = "SELECT * FROM `customcommands`" +
  226. " WHERE" +
  227. " `server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0})" +
  228. " AND " +
  229. " `nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2})";
  230. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, caller.Nickname });
  231. }
  232. private Dictionary<string, object> GetTrigger(Nick caller, string trigger)
  233. {
  234. string search = "SELECT * FROM `customcommands`" +
  235. " WHERE" +
  236. " `server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0})" +
  237. " AND " +
  238. " `nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2})" +
  239. " AND `trigger` = {3}";
  240. List<Dictionary<string, object>> results = Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, caller.Nickname, trigger });
  241. if (results.Any())
  242. return results.First();
  243. return null;
  244. }
  245. private void ExecuteCommand(MessageType messageType, string location, Nick nick, Dictionary<string, object> trigger)
  246. {
  247. string message = trigger["response"].ToString();
  248. if (message.StartsWith(Bot.ServerConfig.CommandPrefix))
  249. {
  250. Bot.ExecuteCommand(message, location, messageType, nick);
  251. }
  252. else
  253. {
  254. message = "\u200B" + message;
  255. SendResponse(messageType, location, nick.Nickname, message);
  256. }
  257. }
  258. }
  259. }