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.

96 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. }
  45. //
  46. // cmdQuote
  47. //
  48. public void cmdQuote(User usr, Channel channel, String msg)
  49. {
  50. var inter = JsonConvert.DeserializeObject<QDBInterface>(
  51. Utils.GetResponseString(InterfaceURI, 64));
  52. int id;
  53. if(String.IsNullOrEmpty(msg?.Trim()) || !int.TryParse(msg, out id))
  54. id = rnd.Next(inter.numQuotes);
  55. else if(id < 0 || id > inter.numQuotes)
  56. throw new CommandArgumentException("invalid quote ID");
  57. var quote = Utils.GetResponseString(APIURI + id.ToString(),
  58. bot.serverInfo.messageSafeMaxLen);
  59. if(String.IsNullOrEmpty(quote))
  60. throw new CommandArgumentException("QDB exploded try again later");
  61. if(bot.serverInfo.shortMessages)
  62. quote = Regex.Replace(quote, "\n+", "\n").Trim();
  63. var lines = quote.Split('\n');
  64. if(bot.serverInfo.shortMessages &&
  65. (lines.Length > 5 || quote.Length > 600))
  66. {
  67. bot.reply(usr, channel, "Quote is too long.");
  68. return;
  69. }
  70. if(bot.serverInfo.hasNewlines)
  71. bot.message(channel, quote);
  72. else
  73. foreach(var ln_ in lines)
  74. {
  75. var ln = ln_.Trim();
  76. if(ln.Length > 0)
  77. bot.message(channel, ln);
  78. }
  79. }
  80. }
  81. }