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

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