guide.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. const db = require("../db.js");
  2. const util = require("../util.js");
  3. const documentation = {
  4. "make <guidetype> <NEWLINE>name: <name> <NEWLINE>keywords: <keywords> <NEWLINE>contents: <content>": {
  5. auth: true,
  6. description: "Creates a new guide of the specified type, the specified keywords and content."
  7. },
  8. "delete <guidetype> <keywords>": {
  9. auth: true,
  10. description: "Deletes a guide of the specified type."
  11. },
  12. "guides": {
  13. auth: false,
  14. description: "Lists all guides and keywords that trigger them."
  15. },
  16. "memes": {
  17. auth: false,
  18. description: "Lists all memes and keywords that trigger them."
  19. },
  20. "miscs": {
  21. auth: false,
  22. description: "Lists all additional keywords the bot reacts to."
  23. }
  24. };
  25. const VALID_GUIDE_TYPES = new Set(["meme", "guide", "misc"]);
  26. const makePattern = /^make (\w+)\s+name:(.+)\s*keywords:(.+)\s*contents:((.*[\n\r]*)+)$/i;
  27. const deletePattern = /^delete (\w+)\s+(.+)$/i;
  28. function listGuides(msg, guideType, message){
  29. let guides = db
  30. .get(guideType)
  31. .reduce((p, c) => `${p}\n${c.displayName} -- ${c.name}`, "\n")
  32. .value();
  33. msg.channel.send(`${msg.author.toString()} ${message}\n\`\`\`${guides}\`\`\`\n\nTo display the guides, ping me with one or more keywords, like \`@NoctBot sybaris com\``);
  34. }
  35. const commands = [
  36. {
  37. pattern: makePattern,
  38. action: (msg, s, match) => {
  39. if (!util.isAuthorised(msg.member)) return;
  40. let type = match[1].toLowerCase();
  41. let name = match[2].trim();
  42. let keywords = match[3].trim().toLowerCase();
  43. let contents = match[4].trim();
  44. if(contents.length == 0){
  45. msg.channel.send(
  46. `${msg.author.toString()} The guide must have some content!`
  47. );
  48. return;
  49. }
  50. if(!VALID_GUIDE_TYPES.has(type)){
  51. msg.channel.send(
  52. `${msg.author.toString()} The type ${type} is not a valid guide type!`
  53. );
  54. return;
  55. }
  56. let typeDB = `${type}s`;
  57. let guide = db.get(typeDB).find({
  58. name: keywords
  59. });
  60. if (!guide.isUndefined().value()) {
  61. guide.assign({
  62. displayName: name,
  63. content: contents
  64. }).write();
  65. } else {
  66. db.get(typeDB)
  67. .push({
  68. name: keywords,
  69. displayName: name,
  70. content: contents
  71. })
  72. .write();
  73. }
  74. msg.channel.send(
  75. `${msg.author.toString()} Added/updated "${name}" (keywords \`${keywords}\`)!`
  76. );
  77. }
  78. },
  79. {
  80. pattern: deletePattern,
  81. action: (msg, s, match) => {
  82. if (!util.isAuthorised(msg.member)) return;
  83. let type = match[1];
  84. let keywords = match[2].trim();
  85. if(!VALID_GUIDE_TYPES.has(type)){
  86. msg.channel.send(
  87. `${msg.author.toString()} The type ${type} is not a valid guide type!`
  88. );
  89. return;
  90. }
  91. let typeDB = `${type}s`;
  92. let val = db.get(typeDB).find({
  93. name: keywords
  94. });
  95. if (val.isUndefined().value()) {
  96. msg.channel.send(`${msg.author.toString()} No ${type} "${keywords}"!`);
  97. return;
  98. }
  99. db.get(typeDB)
  100. .remove({
  101. name: keywords
  102. })
  103. .write();
  104. msg.channel.send(
  105. `${msg.author.toString()} Removed ${type} "${keywords}"!`
  106. );
  107. }
  108. },
  109. { pattern: "guides", action: msg => listGuides(msg, "guides", "Here are the guides I have:") },
  110. { pattern: "memes", action: msg => listGuides(msg, "memes", "Here are some random memes I have:") },
  111. { pattern: "misc", action: msg => listGuides(msg, "misc", "These are some misc stuff I can also do:") },
  112. ];
  113. const onDirectMention = (msg, content, actionsDone) => {
  114. if (actionsDone)
  115. return false;
  116. if(msg.attachments.size > 0 || content.length == 0)
  117. return false;
  118. let parts = content.trim().split(" ");
  119. let guide = db
  120. .get("guides")
  121. .clone()
  122. .concat(db.get("memes").value(), db.get("miscs").value())
  123. .map(g => Object.assign({
  124. parts: g.name.toLowerCase().split(" ")
  125. }, g))
  126. .sortBy(g => g.parts.length)
  127. .maxBy(k => db._.intersection(parts, k.parts).length)
  128. .value();
  129. let hits =
  130. guide !== undefined &&
  131. db._.intersection(guide.name.toLowerCase().split(" "), parts).length > 0;
  132. if (hits) {
  133. msg.channel.send(guide.content);
  134. return true;
  135. }
  136. return false;
  137. };
  138. module.exports = {
  139. commands: commands,
  140. onDirectMention: onDirectMention,
  141. documentation: documentation
  142. };