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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. string query = "INSERT INTO `channelrules` SET " +
  62. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  63. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  64. "`rule` = {3}, " +
  65. "`date_added` = {4}";
  66. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, channel, command.Arguments["Rule"], command.TimeStamp });
  67. string ruleMessage = string.Format("Rule Added. \u0002{0}\u0002 now has \u0002{1}\u0002 rules.", channel, results.Count + 1);
  68. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  69. }
  70. private void EditRule(CommandMessage command)
  71. {
  72. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  73. List<Dictionary<string, object>> results = GetRuleList(channel);
  74. int num = 0;
  75. if (int.TryParse(command.Arguments["ID"], out num))
  76. {
  77. if (results.Count >= num)
  78. {
  79. int id = Convert.ToInt32(results[num - 1]["id"]);
  80. string query = "UPDATE `channelrules` SET " +
  81. "`rule` = {0} " +
  82. "WHERE `id` = {1}";
  83. Bot.Database.Execute(query, new object[] { command.Arguments["Rule"], id });
  84. string ruleMessage = string.Format("Rule \u0002#{0}\u0002 for \u0002{1}\u0002 is now: {2}", num, channel, command.Arguments["Rule"]);
  85. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  86. }
  87. else
  88. {
  89. string invalid = "Invalid Rule ID.";
  90. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  91. }
  92. }
  93. else
  94. {
  95. string invalid = "Invalid Rule ID.";
  96. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  97. }
  98. }
  99. private void DeleteRule(CommandMessage command)
  100. {
  101. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  102. List<Dictionary<string, object>> results = GetRuleList(channel);
  103. int num = 0;
  104. if (int.TryParse(command.Arguments["ID"], out num))
  105. {
  106. if (results.Count >= num)
  107. {
  108. int id = Convert.ToInt32(results[num - 1]["id"]);
  109. string query = "DELETE FROM `channelrules` " +
  110. "WHERE `id` = {0}";
  111. Bot.Database.Execute(query, new object[] { id });
  112. string ruleMessage = string.Format("Rule \u0002#{0}\u0002 for \u0002{1}\u0002 has been deleted.", num, channel);
  113. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  114. }
  115. else
  116. {
  117. string invalid = "Invalid Rule ID.";
  118. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  119. }
  120. }
  121. else
  122. {
  123. string invalid = "Invalid Rule ID.";
  124. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  125. }
  126. }
  127. private List<Dictionary<string, object>> GetRuleList(string channel)
  128. {
  129. string search = "SELECT `channelrules`.`id`, `channelrules`.`rule` FROM `channelrules` " +
  130. "INNER JOIN `channels` " +
  131. "ON `channelrules`.`channel_id` = `channels`.`id` " +
  132. "INNER JOIN `servers` " +
  133. "ON `channelrules`.`server_id` = `servers`.`id` " +
  134. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} " +
  135. "ORDER BY date_added ASC";
  136. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  137. }
  138. }
  139. }