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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = GetQuoteList(channel, command.Arguments["Nickname"]);
  30. }
  31. else
  32. {
  33. results = GetQuoteList(channel);
  34. }
  35. if (results.Any())
  36. {
  37. Random randNum = new Random();
  38. int index = randNum.Next(results.Count - 1);
  39. Dictionary<string, object> quote = GetQuote(Convert.ToInt32(results[index]["id"])).First();
  40. string quoteMessage = string.Format("[{0}] {1}", quote["nickname"], quote["message"]);
  41. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, quoteMessage);
  42. }
  43. else
  44. {
  45. if (command.Arguments.ContainsKey("Nickname"))
  46. {
  47. string quoteMessage = string.Format("There are no quotes for \u0002{0}\u0002", command.Arguments["Nickname"]);
  48. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, quoteMessage);
  49. }
  50. else
  51. {
  52. string quoteMessage = string.Format("There are no quotes for \u0002{0}\u0002.", channel);
  53. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, quoteMessage);
  54. }
  55. }
  56. }
  57. private List<Dictionary<string, object>> GetQuoteList(string channel)
  58. {
  59. string search = @"SELECT `channelmessages`.`id` FROM `channelmessages`
  60. INNER JOIN `channels`
  61. ON `channelmessages`.`channel_id` = `channels`.`id`
  62. INNER JOIN `servers`
  63. ON `channelmessages`.`server_id` = `servers`.`id`
  64. WHERE `servers`.`name` = {0} AND `channels`.`name` = {1}";
  65. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  66. }
  67. private List<Dictionary<string, object>> GetQuoteList(string channel, string nickname)
  68. {
  69. string search = @"SELECT `channelmessages`.`id` FROM `channelmessages`
  70. INNER JOIN `nicks`
  71. ON `channelmessages`.`nick_id` = `nicks`.`id`
  72. INNER JOIN `channels`
  73. ON `channelmessages`.`channel_id` = `channels`.`id`
  74. INNER JOIN `servers`
  75. ON `channelmessages`.`server_id` = `servers`.`id`
  76. WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}";
  77. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  78. }
  79. private List<Dictionary<string, object>> GetQuote(int id)
  80. {
  81. string search = @"SELECT `channelmessages`.`message`, `nicks`.`nickname` FROM `channelmessages`
  82. INNER JOIN `nicks`
  83. ON `channelmessages`.`nick_id` = `nicks`.`id`
  84. WHERE `channelmessages`.`id` = {0}";
  85. return Bot.Database.Query(search, new object[] { id });
  86. }
  87. private List<Dictionary<string, object>> GetQuote(string channel)
  88. {
  89. string search = @"
  90. set max_heap_table_size = 33554432;
  91. CREATE TEMPORARY TABLE IF NOT EXISTS tempQuotes
  92. (tempID BIGINT NOT NULL AUTO_INCREMENT,
  93. id INT(11) NOT NULL,
  94. PRIMARY KEY tempID (tempID), UNIQUE KEY id (id))
  95. ENGINE=memory
  96. AS (
  97. SELECT `channelmessages`.`id`
  98. FROM `channelmessages`
  99. INNER JOIN `channels`
  100. ON `channelmessages`.`channel_id` = `channels`.`id`
  101. INNER JOIN `servers`
  102. ON `channelmessages`.`server_id` = `servers`.`id`
  103. WHERE `channels`.`name` = {1} AND `servers`.`name` = {0}
  104. );
  105. CREATE TEMPORARY TABLE tempQuotes2 LIKE tempQuotes;
  106. INSERT INTO tempQuotes2 SELECT * FROM tempQuotes;
  107. SELECT r1.id
  108. FROM tempQuotes AS r1 JOIN
  109. (SELECT CEIL(RAND() *
  110. (SELECT MAX(tempID)
  111. FROM tempQuotes2)) AS tempID)
  112. AS r2
  113. WHERE r1.tempID >= r2.tempID
  114. ORDER BY r1.tempID ASC
  115. LIMIT 1;";
  116. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel });
  117. }
  118. private List<Dictionary<string, object>> GetQuote(string channel, string nickname)
  119. {
  120. string search = @"
  121. set max_heap_table_size = 33554432;
  122. CREATE TEMPORARY TABLE IF NOT EXISTS tempQuotes
  123. (tempID BIGINT NOT NULL AUTO_INCREMENT,
  124. id INT(11) NOT NULL,
  125. PRIMARY KEY tempID (tempID), UNIQUE KEY id (id))
  126. ENGINE=memory
  127. AS (
  128. SELECT `channelmessages`.`id`
  129. FROM `channelmessages`
  130. INNER JOIN `nicks`
  131. ON `channelmessages`.`nick_id` = `nicks`.`id`
  132. INNER JOIN `channels`
  133. ON `channelmessages`.`channel_id` = `channels`.`id`
  134. INNER JOIN `servers`
  135. ON `channelmessages`.`server_id` = `servers`.`id`
  136. WHERE `nicks`.`nickname` = {2} AND `channels`.`name` = {1} AND `servers`.`name` = {0}
  137. );
  138. CREATE TEMPORARY TABLE tempQuotes2 LIKE tempQuotes;
  139. INSERT INTO tempQuotes2 SELECT * FROM tempQuotes;
  140. SELECT r1.id
  141. FROM tempQuotes AS r1 JOIN
  142. (SELECT CEIL(RAND() *
  143. (SELECT MAX(tempID)
  144. FROM tempQuotes2)) AS tempID)
  145. AS r2
  146. WHERE r1.tempID >= r2.tempID
  147. ORDER BY r1.tempID ASC
  148. LIMIT 1;";
  149. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  150. }
  151. }
  152. }