The easy to use and full featured Irc Bot everyone is talking about!
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  16. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  17. switch (foundCommand.Name)
  18. {
  19. case "Rules Display":
  20. List<Dictionary<string, object>> foundRules = GetRuleList(channel);
  21. if (foundRules.Any())
  22. {
  23. int index = 1;
  24. foundRules.ForEach(rule =>
  25. {
  26. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Rule \u0002#{0}\u0002: {1}", index, rule["rule"]));
  27. index++;
  28. });
  29. }
  30. else
  31. {
  32. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("There are no rules for \u0002{0}\u0002", channel));
  33. }
  34. break;
  35. case "Rules Modification":
  36. if (Bot.CheckChannelAccess(channel, command.Nick.Nickname, command.Access))
  37. {
  38. string action = command.Arguments["Action"].ToString();
  39. switch (action.ToLower())
  40. {
  41. case "add":
  42. AddRule(command);
  43. break;
  44. case "edit":
  45. EditRule(command);
  46. break;
  47. case "del":
  48. DeleteRule(command);
  49. break;
  50. }
  51. }
  52. else
  53. {
  54. string noAccessMessage = string.Format("You do not have access to \u0002{0}\u000F on \u0002{1}\u000F.", command.Command, channel);
  55. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noAccessMessage);
  56. }
  57. break;
  58. }
  59. }
  60. private void AddRule(CommandMessage command)
  61. {
  62. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  63. List<Dictionary<string, object>> results = GetRuleList(channel);
  64. AddChannel(channel);
  65. string query = "INSERT INTO `channelrules` 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. "`rule` = {3}, " +
  69. "`date_added` = {4}";
  70. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, channel, command.Arguments["Rule"], command.TimeStamp });
  71. string ruleMessage = string.Format("Rule Added. \u0002{0}\u0002 now has \u0002{1}\u0002 rules.", channel, results.Count + 1);
  72. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  73. }
  74. private void EditRule(CommandMessage command)
  75. {
  76. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  77. List<Dictionary<string, object>> results = GetRuleList(channel);
  78. int num = 0;
  79. if (int.TryParse(command.Arguments["ID"], out num))
  80. {
  81. if (results.Count >= num)
  82. {
  83. int id = Convert.ToInt32(results[num - 1]["id"]);
  84. string query = "UPDATE `channelrules` SET " +
  85. "`rule` = {0} " +
  86. "WHERE `id` = {1}";
  87. Bot.Database.Execute(query, new object[] { command.Arguments["Rule"], id });
  88. string ruleMessage = string.Format("Rule \u0002#{0}\u0002 for \u0002{1}\u0002 is now: {2}", num, channel, command.Arguments["Rule"]);
  89. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  90. }
  91. else
  92. {
  93. string invalid = "Invalid Rule ID.";
  94. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  95. }
  96. }
  97. else
  98. {
  99. string invalid = "Invalid Rule ID.";
  100. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  101. }
  102. }
  103. private void DeleteRule(CommandMessage command)
  104. {
  105. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  106. List<Dictionary<string, object>> results = GetRuleList(channel);
  107. int num = 0;
  108. if (int.TryParse(command.Arguments["ID"], out num))
  109. {
  110. if (results.Count >= num)
  111. {
  112. int id = Convert.ToInt32(results[num - 1]["id"]);
  113. string query = "DELETE FROM `channelrules` " +
  114. "WHERE `id` = {0}";
  115. Bot.Database.Execute(query, new object[] { id });
  116. string ruleMessage = string.Format("Rule \u0002#{0}\u0002 for \u0002{1}\u0002 has been deleted.", num, channel);
  117. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, ruleMessage);
  118. }
  119. else
  120. {
  121. string invalid = "Invalid Rule ID.";
  122. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  123. }
  124. }
  125. else
  126. {
  127. string invalid = "Invalid Rule ID.";
  128. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, invalid);
  129. }
  130. }
  131. private List<Dictionary<string, object>> GetRuleList(string channel)
  132. {
  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 Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  141. }
  142. }
  143. }