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 9.6KB

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