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.

Quotes.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Quotes : 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 "Quote":
  19. GetQuote(command);
  20. break;
  21. }
  22. }
  23. private void GetQuote(CommandMessage command)
  24. {
  25. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  26. List<Dictionary<string, object>> results = new List<Dictionary<string, object>>();
  27. if (command.Arguments.ContainsKey("Nickname"))
  28. {
  29. results = GetQuote(channel, command.Arguments["Nickname"]);
  30. }
  31. else
  32. {
  33. results = GetQuote(channel);
  34. }
  35. if (results.Any())
  36. {
  37. Dictionary<string, object> quote = results.First();
  38. string quoteMessage = string.Format("[{0}] {1}", quote["nickname"], quote["message"]);
  39. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, quoteMessage);
  40. }
  41. else
  42. {
  43. if (command.Arguments.ContainsKey("Nickname"))
  44. {
  45. string quoteMessage = string.Format("There are no quotes for \u0002{0}\u0002", command.Arguments["Nickname"]);
  46. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, quoteMessage);
  47. }
  48. else
  49. {
  50. string quoteMessage = string.Format("There are no quotes for \u0002{0}\u0002.", channel);
  51. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, quoteMessage);
  52. }
  53. }
  54. }
  55. private List<Dictionary<string, object>> GetQuoteList(string channel)
  56. {
  57. string search = "SELECT `channelmessages`.`id` FROM `channelmessages` " +
  58. "INNER JOIN `channels` " +
  59. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  60. "INNER JOIN `servers` " +
  61. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  62. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1}";
  63. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  64. }
  65. private List<Dictionary<string, object>> GetQuoteList(string channel, string nickname)
  66. {
  67. string search = "SELECT `channelmessages`.`id` FROM `channelmessages` " +
  68. "INNER JOIN `nicks` " +
  69. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  70. "INNER JOIN `channels` " +
  71. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  72. "INNER JOIN `servers` " +
  73. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  74. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}";
  75. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  76. }
  77. private List<Dictionary<string, object>> GetQuote(int id)
  78. {
  79. string search = "SELECT `channelmessages`.`message`, `nicks`.`nickname` FROM `channelmessages` " +
  80. "INNER JOIN `nicks` " +
  81. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  82. "WHERE `channelmessages`.`id` = {0}";
  83. return Bot.Database.Query(search, new object[] { id });
  84. }
  85. private List<Dictionary<string, object>> GetQuote(string channel)
  86. {
  87. string search = @"SELECT r1.`message`, `nicks`.`nickname`
  88. FROM `channelmessages` AS r1 JOIN
  89. (SELECT CEIL(RAND() *
  90. (SELECT MAX(id)
  91. FROM `channelmessages`)) AS id)
  92. AS r2
  93. INNER JOIN `nicks`
  94. ON r1.`nick_id` = `nicks`.`id`
  95. INNER JOIN `channels`
  96. ON r1.`channel_id` = `channels`.`id`
  97. INNER JOIN `servers`
  98. ON r1.`server_id` = `servers`.`id`
  99. WHERE r1.id >= r2.id AND `servers`.`name` = {0} AND `channels`.`name` = {1}
  100. ORDER BY r1.id ASC
  101. LIMIT 1";
  102. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  103. }
  104. private List<Dictionary<string, object>> GetQuote(string channel, string nickname)
  105. {
  106. string search = @"SELECT r1.`message`, `nicks`.`nickname`
  107. FROM `channelmessages` AS r1 JOIN
  108. (SELECT CEIL(RAND() *
  109. (SELECT MAX(id)
  110. FROM `channelmessages`)) AS id)
  111. AS r2
  112. INNER JOIN `nicks`
  113. ON r1.`nick_id` = `nicks`.`id`
  114. INNER JOIN `channels`
  115. ON r1.`channel_id` = `channels`.`id`
  116. INNER JOIN `servers`
  117. ON r1.`server_id` = `servers`.`id`
  118. WHERE r1.id >= r2.id AND `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}
  119. ORDER BY r1.id ASC
  120. LIMIT 1";
  121. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  122. }
  123. }
  124. }