guide.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const db = require("../db.js");
  2. const util = require("../util.js");
  3. const commands = {
  4. "make guide": msg => {
  5. if (!util.isAuthorised(msg.member)) return;
  6. let content = msg.content.substring(msg.content.indexOf("make guide") + "make guide ".length);
  7. let guideName = content.substring(0, content.indexOf("\n")).trim();
  8. let guideContent = content.substring(content.indexOf("\n")).trim();
  9. let guide = db.get("guides").find({
  10. name: guideName
  11. });
  12. if (!guide.isUndefined().value()) {
  13. guide.assign({
  14. content: guideContent
  15. }).write();
  16. } else {
  17. db.get("guides")
  18. .push({
  19. name: guideName,
  20. content: guideContent
  21. })
  22. .write();
  23. }
  24. msg.channel.send(
  25. `${msg.author.toString()} Added/updated "${guideName}"!`
  26. );
  27. },
  28. "delete guide": (msg, s) => {
  29. if (!util.isAuthorised(msg.member)) return;
  30. let guideName = s.substring("delete guide ".length).trim();
  31. let val = db.get("guides").find({
  32. name: guideName
  33. });
  34. if (val.isUndefined().value()) {
  35. msg.channel.send(`${msg.author.toString()} No guide "${guideName}"!`);
  36. return;
  37. }
  38. db.get("guides")
  39. .remove({
  40. name: guideName
  41. })
  42. .write();
  43. msg.channel.send(
  44. `${msg.author.toString()} Removed guide "${guideName}"!`
  45. );
  46. }
  47. };
  48. const onDirectMention = (msg, content, actionsDone) => {
  49. if (actionsDone)
  50. return false;
  51. let parts = content.trim().split(" ");
  52. let guide = db
  53. .get("guides")
  54. .map(g => Object.assign({
  55. parts: g.name.toLowerCase().split(" ")
  56. }, g))
  57. .sortBy(g => g.parts.length)
  58. .maxBy(k => db._.intersection(parts, k.parts).length)
  59. .value();
  60. let hits =
  61. guide !== undefined &&
  62. db._.intersection(guide.name.toLowerCase().split(" "), parts).length > 0;
  63. if (hits) {
  64. msg.channel.send(guide.content);
  65. return true;
  66. }
  67. return false;
  68. };
  69. module.exports = {
  70. commands: commands,
  71. onDirectMention: onDirectMention
  72. };