The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Introductions.cs 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. {
  84. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  85. int num = 0;
  86. int ret = 0;
  87. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  88. if (int.TryParse(command.Arguments["ID"], out num))
  89. {
  90. if (results.Count >= num && num > 0)
  91. {
  92. ret = num;
  93. }
  94. }
  95. return ret;
  96. }
  97. private void EditIntroduction(CommandMessage command)
  98. {
  99. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  100. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  101. int num = HasValidIntroductionID( command );
  102. if (num > 0){
  103. int id = Convert.ToInt32(results[num - 1]["id"]);
  104. string query = "UPDATE `introductions` SET " +
  105. "`message` = {0} " +
  106. "WHERE `id` = {1}";
  107. Bot.Database.Execute(query, new object[] { command.Arguments["Message"], id });
  108. string introMessage = string.Format("Introduction #\u0002{0}\u0002 is now: {1}", num, command.Arguments["Message"]);
  109. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  110. }
  111. else
  112. {
  113. string invalid = "Invalid introduction ID.";
  114. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  115. }
  116. }
  117. private void DeleteIntroduction(CommandMessage command)
  118. {
  119. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  120. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  121. int num = HasValidIntroductionID( command );
  122. if (num > 0){
  123. int id = Convert.ToInt32(results[num - 1]["id"]);
  124. string query = "DELETE FROM `introductions` " +
  125. "WHERE `id` = {0}";
  126. Bot.Database.Execute(query, new object[] { id });
  127. string introMessage = string.Format("Introduction #\u0002{0}\u0002 has been deleted.", num);
  128. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  129. }
  130. else
  131. {
  132. string invalid = "Invalid introduction ID.";
  133. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  134. }
  135. }
  136. private void ViewIntroduction(CommandMessage command)
  137. {
  138. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  139. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  140. int num = 0;
  141. if (command.Arguments.ContainsKey("ID"))
  142. {
  143. num = HasValidIntroductionID( command );
  144. if ( num > 0 ){
  145. string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", num, results[num - 1]["message"]);
  146. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  147. }
  148. else
  149. {
  150. string invalid = "Invalid introduction ID.";
  151. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  152. }
  153. }
  154. else
  155. {
  156. if (results.Any())
  157. {
  158. for (int i = 0; i < results.Count; i++)
  159. {
  160. string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", i + 1, results[i]["message"]);
  161. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, introMessage);
  162. }
  163. }
  164. else
  165. {
  166. string invalid = "You do not have any introductions set.";
  167. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  168. }
  169. }
  170. }
  171. private List<Dictionary<string, object>> GetIntroductionList(string channel, string nickname)
  172. {
  173. // Check to see if they have reached the max number of introductions
  174. string search = "SELECT `introductions`.`id`, `introductions`.`message` FROM `introductions` " +
  175. "INNER JOIN `nicks` " +
  176. "ON `introductions`.`nick_id` = `nicks`.`id` " +
  177. "INNER JOIN `channels` " +
  178. "ON `introductions`.`channel_id` = `channels`.`id` " +
  179. "INNER JOIN `servers` " +
  180. "ON `nicks`.`server_id` = `servers`.`id` " +
  181. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}";
  182. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  183. }
  184. }
  185. }