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.6KB

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