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.

Introductions.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. using Combot.IRCServices.Messaging;
  6. using System.IO;
  7. namespace Combot.Modules.Plugins
  8. {
  9. public class Introductions : Module
  10. {
  11. public override void Initialize()
  12. {
  13. InitializeTable();
  14. Bot.CommandReceivedEvent += HandleCommandEvent;
  15. Bot.IRC.Message.JoinChannelEvent += HandleJoinEvent;
  16. }
  17. private void InitializeTable()
  18. {
  19. string sqlPath = Path.Combine(Directory.GetCurrentDirectory(), ConfigPath, "CreateTable.sql");
  20. if (File.Exists(sqlPath))
  21. {
  22. string query = File.ReadAllText(sqlPath);
  23. Bot.Database.Execute(query);
  24. }
  25. }
  26. public override void ParseCommand(CommandMessage command)
  27. {
  28. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  29. switch (foundCommand.Name)
  30. {
  31. case "Introduction":
  32. string method = command.Arguments["Method"];
  33. switch (method.ToLower())
  34. {
  35. case "add":
  36. AddIntroduction(command);
  37. break;
  38. case "edit":
  39. EditIntroduction(command);
  40. break;
  41. case "delete":
  42. case "del":
  43. DeleteIntroduction(command);
  44. break;
  45. case "view":
  46. ViewIntroduction(command);
  47. break;
  48. }
  49. break;
  50. }
  51. }
  52. private void HandleJoinEvent(object sender, JoinChannelInfo info)
  53. {
  54. if (Enabled
  55. && !Bot.ServerConfig.ChannelBlacklist.Contains(info.Channel)
  56. && !Bot.ServerConfig.NickBlacklist.Contains(info.Nick.Nickname)
  57. && !ChannelBlacklist.Contains(info.Channel)
  58. && !NickBlacklist.Contains(info.Nick.Nickname))
  59. {
  60. List<Dictionary<string, object>> results = GetIntroductionList(info.Channel, info.Nick.Nickname);
  61. if (results.Any())
  62. {
  63. Random randNum = new Random();
  64. int index = randNum.Next(0, results.Count);
  65. Dictionary<string, object> intro = results[index];
  66. Bot.IRC.Command.SendPrivateMessage(info.Channel, string.Format("\u200B{0}", intro["message"]));
  67. }
  68. }
  69. }
  70. private void AddIntroduction(CommandMessage command)
  71. {
  72. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  73. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  74. if (results.Count < Convert.ToInt32(GetOptionValue("Max Introductions")))
  75. {
  76. AddChannel(channel);
  77. AddNick(command.Nick);
  78. string query = "INSERT INTO `introductions` SET " +
  79. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  80. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  81. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  82. "`message` = {5}, " +
  83. "`date_added` = {6}";
  84. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, channel, Bot.ServerConfig.Name, command.Nick.Nickname, command.Arguments["Message"], command.TimeStamp });
  85. string introMessage = string.Format("Added introduction. You now have \u0002{0}\u0002 introductions set.", results.Count + 1);
  86. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  87. }
  88. else
  89. {
  90. string maxMessage = "You already have the maximum number of introductions for this channel. Delete one before trying to add another.";
  91. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, maxMessage, true);
  92. }
  93. }
  94. /* Returns the parsed ID field if valid, otherwise returns 0 */
  95. private int HasValidIntroductionID(CommandMessage command)
  96. {
  97. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  98. int num = 0;
  99. int ret = 0;
  100. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  101. if (int.TryParse(command.Arguments["ID"], out num))
  102. {
  103. if (results.Count >= num && num > 0)
  104. {
  105. ret = num;
  106. }
  107. }
  108. return ret;
  109. }
  110. private void EditIntroduction(CommandMessage command)
  111. {
  112. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  113. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  114. int num = HasValidIntroductionID( command );
  115. if (num > 0){
  116. int id = Convert.ToInt32(results[num - 1]["id"]);
  117. string query = "UPDATE `introductions` SET " +
  118. "`message` = {0} " +
  119. "WHERE `id` = {1}";
  120. Bot.Database.Execute(query, new object[] { command.Arguments["Message"], id });
  121. string introMessage = string.Format("Introduction #\u0002{0}\u0002 is now: {1}", num, command.Arguments["Message"]);
  122. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  123. }
  124. else
  125. {
  126. string invalid = "Invalid introduction ID.";
  127. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  128. }
  129. }
  130. private void DeleteIntroduction(CommandMessage command)
  131. {
  132. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  133. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  134. int num = HasValidIntroductionID( command );
  135. if (num > 0){
  136. int id = Convert.ToInt32(results[num - 1]["id"]);
  137. string query = "DELETE FROM `introductions` " +
  138. "WHERE `id` = {0}";
  139. Bot.Database.Execute(query, new object[] { id });
  140. string introMessage = string.Format("Introduction #\u0002{0}\u0002 has been deleted.", num);
  141. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  142. }
  143. else
  144. {
  145. string invalid = "Invalid introduction ID.";
  146. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  147. }
  148. }
  149. private void ViewIntroduction(CommandMessage command)
  150. {
  151. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  152. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  153. int num = 0;
  154. if (command.Arguments.ContainsKey("ID"))
  155. {
  156. num = HasValidIntroductionID( command );
  157. if ( num > 0 ){
  158. string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", num, results[num - 1]["message"]);
  159. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  160. }
  161. else
  162. {
  163. string invalid = "Invalid introduction ID.";
  164. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  165. }
  166. }
  167. else
  168. {
  169. if (results.Any())
  170. {
  171. for (int i = 0; i < results.Count; i++)
  172. {
  173. string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", i + 1, results[i]["message"]);
  174. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage, true);
  175. }
  176. }
  177. else
  178. {
  179. string invalid = "You do not have any introductions set.";
  180. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid, true);
  181. }
  182. }
  183. }
  184. private List<Dictionary<string, object>> GetIntroductionList(string channel, string nickname)
  185. {
  186. // Check to see if they have reached the max number of introductions
  187. string search = "SELECT `introductions`.`id`, `introductions`.`message` FROM `introductions` " +
  188. "INNER JOIN `nicks` " +
  189. "ON `introductions`.`nick_id` = `nicks`.`id` " +
  190. "INNER JOIN `channels` " +
  191. "ON `introductions`.`channel_id` = `channels`.`id` " +
  192. "INNER JOIN `servers` " +
  193. "ON `nicks`.`server_id` = `servers`.`id` " +
  194. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}";
  195. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  196. }
  197. }
  198. }