quote.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const db = require("../db.js");
  2. const util = require("../util.js");
  3. const documentation = {
  4. "add quote by \"<author>\" <NEWLINE> <quote>": {
  5. auth: true,
  6. description: "Adds a quote"
  7. },
  8. "remove quote <quote_index>": {
  9. auth: true,
  10. description: "Removes quote. Use \"quotes\" to get the <quote_index>!"
  11. },
  12. "quotes": {
  13. auth: true,
  14. description: "Lists all known quotes."
  15. },
  16. "random quote": {
  17. auth: false,
  18. description: "Shows a random quote by someone special..."
  19. }
  20. };
  21. const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
  22. function minify(str, maxLength) {
  23. let result = str.replace("\n", "");
  24. if (result.length > maxLength)
  25. result = `${result.substring(0, maxLength - 3)}...`;
  26. return result;
  27. }
  28. const commands = [
  29. {
  30. pattern: "add quote",
  31. action: (msg, c) => {
  32. if (!util.isAuthorised(msg.member))
  33. return;
  34. let result = quotePattern.exec(c);
  35. if (result == null)
  36. return;
  37. let author = result[1].trim();
  38. let message = result[2].trim();
  39. db.get("quotes").push({
  40. author: author,
  41. message: message
  42. }).write();
  43. msg.channel.send(`${msg.author.toString()} Added quote #${db.get("quotes").size().value()}!`);
  44. }
  45. },
  46. {
  47. pattern: "random quote",
  48. action: (msg) => {
  49. if (db.get("quotes").size().value() == 0) {
  50. msg.channel.send("I have no quotes!");
  51. return;
  52. }
  53. let quote = db.get("quotes").randomElement().value();
  54. let index = db.get("quotes").indexOf(quote).value();
  55. msg.channel.send(`Quote #${index + 1}:\n*"${quote.message}"*\n- ${quote.author}`);
  56. }
  57. },
  58. {
  59. pattern: "remove quote",
  60. action: (msg, c) => {
  61. let quoteNum = c.substring("remove quote".length).trim();
  62. let val = parseInt(quoteNum);
  63. if (isNaN(val) || db.get("quotes").size().value() < val - 1)
  64. return;
  65. db.get("quotes").pullAt(val - 1).write();
  66. msg.channel.send(`${msg.author.toString()} Removed quote #${val}!`);
  67. }
  68. },
  69. {
  70. pattern: "quotes",
  71. action: msg => {
  72. if (!util.isAuthorised(msg.member)) {
  73. msg.channel.send(`${msg.author.toString()} To prevent spamming, only bot moderators can view all quotes!`);
  74. return;
  75. }
  76. if (db.get("quotes").size().value() == 0) {
  77. msg.channel.send("I have no quotes!");
  78. return;
  79. }
  80. let quotes = db.get("quotes").reduce((prev, curr, i) => `${prev}[${i+1}] "${minify(curr.message, 10)}" by ${curr.author}\n`, "\n").value();
  81. msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotes}\`\`\``);
  82. }
  83. }
  84. ];
  85. module.exports = {
  86. commands: commands,
  87. documentation: documentation
  88. };