vrobot3 is a chat bot for IRC and Discord.
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.

98 lines
2.6KB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // Copyright © 2016 Project Golan
  4. //
  5. // See "LICENSE" for more information.
  6. //
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Doominati Quote DB interface command.
  10. // .quote
  11. //
  12. //-----------------------------------------------------------------------------
  13. using System;
  14. using System.Net;
  15. using System.Threading;
  16. using System.Text.RegularExpressions;
  17. using Newtonsoft.Json;
  18. namespace ProjectGolan.Vrobot3.Modules
  19. {
  20. //
  21. // Mod_Quote
  22. //
  23. public class Mod_Quote : IBotModule
  24. {
  25. private struct QDBInterface
  26. {
  27. public int numQuotes;
  28. }
  29. const String APIURI = "http://www.greyserv.net/qdb/q/";
  30. const String InterfaceURI = "http://www.greyserv.net/qdb/interface.cgi";
  31. private Random rnd = Utils.GetRND();
  32. //
  33. // Mod_Quote constructor
  34. //
  35. public Mod_Quote(Bot bot_) :
  36. base(bot_)
  37. {
  38. commands["quote"] = new BotCommandStructure{
  39. cmd = cmdQuote,
  40. help = "Get a quote from the Doominati Quote DB.\n" +
  41. "Syntax: .quote [id]\n" +
  42. "Example: .quote 536"
  43. };
  44. postSetup();
  45. }
  46. //
  47. // cmdQuote
  48. //
  49. public void cmdQuote(User usr, Channel channel, String msg)
  50. {
  51. var inter = JsonConvert.DeserializeObject<QDBInterface>(
  52. Utils.GetResponseString(InterfaceURI, 64));
  53. int id;
  54. if(String.IsNullOrEmpty(msg?.Trim()) || !int.TryParse(msg, out id))
  55. id = rnd.Next(inter.numQuotes);
  56. else if(id < 0 || id > inter.numQuotes)
  57. throw new CommandArgumentException("invalid quote ID");
  58. var quote = Utils.GetResponseString(APIURI + id.ToString(),
  59. bot.clientInfo.messageSafeMaxLen);
  60. if(String.IsNullOrEmpty(quote))
  61. throw new CommandArgumentException("QDB exploded try again later");
  62. if(bot.clientInfo.shortMessages)
  63. quote = Regex.Replace(quote, "\n+", "\n").Trim();
  64. var lines = quote.Split('\n');
  65. if(bot.clientInfo.shortMessages &&
  66. (lines.Length > 5 || quote.Length > 600))
  67. {
  68. bot.reply(usr, channel, "Quote is too long.");
  69. return;
  70. }
  71. if(bot.clientInfo.hasNewlines)
  72. bot.message(channel, quote);
  73. else
  74. foreach(var ln_ in lines)
  75. {
  76. var ln = ln_.Trim();
  77. if(ln.Length > 0)
  78. bot.message(channel, ln);
  79. }
  80. }
  81. }
  82. }