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.

Channel_Rules.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. namespace Combot.Modules.Plugins
  6. {
  7. public class Channel_Rules : Module
  8. {
  9. public override void Initialize()
  10. {
  11. Bot.CommandReceivedEvent += HandleCommandEvent;
  12. }
  13. public override void ParseCommand(CommandMessage command)
  14. {
  15. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  16. switch (foundCommand.Name)
  17. {
  18. case "Rules":
  19. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  20. if (command.Arguments.ContainsKey("Action"))
  21. {
  22. string action = command.Arguments["Action"].ToString();
  23. switch (action.ToLower())
  24. {
  25. case "add":
  26. AddRule(command);
  27. break;
  28. case "edit":
  29. EditRule(command);
  30. break;
  31. case "del":
  32. DeleteRule(command);
  33. break;
  34. }
  35. }
  36. else
  37. {
  38. List<Dictionary<string, object>> foundRules = GetRuleList(channel);
  39. if (foundRules.Any())
  40. {
  41. int index = 1;
  42. foundRules.ForEach(rule =>
  43. {
  44. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Rule \u0002#{0}\u0002: {1}", index, rule["rule"]));
  45. index++;
  46. });
  47. }
  48. else
  49. {
  50. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("There are no rules for \u0002{0}\u0002", channel));
  51. }
  52. }
  53. break;
  54. }
  55. }
  56. private void AddRule(CommandMessage command)
  57. {
  58. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  59. List<Dictionary<string, object>> results = GetRuleList(channel);
  60. AddChannel(channel);
  61. Database database = new Database(Bot.ServerConfig.Database);
  62. string query = "INSERT INTO `channelrules` SET " +
  63. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  64. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  65. "`rule` = {3}, " +
  66. "`date_added` = {4}";
  67. database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, channel, command.Arguments["Rule"], command.TimeStamp });
  68. string ruleMessage = string.Format("Rule Added. \u0002{0}\u0002 now has \u0002{1}\u0002 rules.", channel, results.Count + 1);
  69. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  70. }
  71. private void EditRule(CommandMessage command)
  72. {
  73. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  74. List<Dictionary<string, object>> results = GetRuleList(channel);
  75. int num = 0;
  76. if (int.TryParse(command.Arguments["ID"], out num))
  77. {
  78. if (results.Count >= num)
  79. {
  80. int id = Convert.ToInt32(results[num - 1]["id"]);
  81. Database database = new Database(Bot.ServerConfig.Database);
  82. string query = "UPDATE `channelrules` SET " +
  83. "`rule` = {0} " +
  84. "WHERE `id` = {1}";
  85. database.Execute(query, new object[] { command.Arguments["Rule"], id });
  86. string ruleMessage = string.Format("Rule \u0002#{0}\u0002 for \u0002{1}\u0002 is now: {2}", num, channel, command.Arguments["Rule"]);
  87. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  88. }
  89. else
  90. {
  91. string invalid = "Invalid Rule ID.";
  92. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  93. }
  94. }
  95. else
  96. {
  97. string invalid = "Invalid Rule ID.";
  98. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  99. }
  100. }
  101. private void DeleteRule(CommandMessage command)
  102. {
  103. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  104. List<Dictionary<string, object>> results = GetRuleList(channel);
  105. int num = 0;
  106. if (int.TryParse(command.Arguments["ID"], out num))
  107. {
  108. if (results.Count >= num)
  109. {
  110. int id = Convert.ToInt32(results[num - 1]["id"]);
  111. Database database = new Database(Bot.ServerConfig.Database);
  112. string query = "DELETE FROM `channelrules` " +
  113. "WHERE `id` = {0}";
  114. database.Execute(query, new object[] { id });
  115. string ruleMessage = string.Format("Rule \u0002#{0}\u0002 for \u0002{1}\u0002 has been deleted.", num, channel);
  116. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  117. }
  118. else
  119. {
  120. string invalid = "Invalid Rule ID.";
  121. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  122. }
  123. }
  124. else
  125. {
  126. string invalid = "Invalid Rule ID.";
  127. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  128. }
  129. }
  130. private List<Dictionary<string, object>> GetRuleList(string channel)
  131. {
  132. Database database = new Database(Bot.ServerConfig.Database);
  133. string search = "SELECT `channelrules`.`id`, `channelrules`.`rule` FROM `channelrules` " +
  134. "INNER JOIN `channels` " +
  135. "ON `channelrules`.`channel_id` = `channels`.`id` " +
  136. "INNER JOIN `servers` " +
  137. "ON `channelrules`.`server_id` = `servers`.`id` " +
  138. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} " +
  139. "ORDER BY date_added ASC";
  140. return database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  141. }
  142. }
  143. }