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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }
  88. }