react.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const db = require("../db.js");
  2. const util = require("../util.js");
  3. const client = require("../client.js");
  4. const pattern = /^react to\s+"([^"]+)"\s+with\s+\<:[^:]+:([^\>]+)\>$/i;
  5. const commands = {
  6. "react to": (msg, s) => {
  7. if (!util.isAuthorised(msg.member)) return;
  8. let contents = pattern.exec(s);
  9. if (contents != null) {
  10. let reactable = contents[1].trim().toLowerCase();
  11. let reactionEmoji = contents[2];
  12. if (!client.emojis.has(reactionEmoji)) {
  13. msg.channel.send(`${msg.author.toString()} I cannot react with this emoji :(`);
  14. return;
  15. }
  16. db.get("messageReactions").set(reactable, reactionEmoji).write();
  17. msg.channel.send(`${msg.author.toString()} Added reaction!`);
  18. }
  19. },
  20. "remove reaction to": (msg, s) => {
  21. if (!util.isAuthorised(msg.member)) return;
  22. let content = s.substring("remove reaction to ".length).trim().toLowerCase();
  23. if (!db.get("messageReactions").has(content).value()) {
  24. msg.channel.send(`${msg.author.toString()} No such reaction available!`);
  25. return;
  26. }
  27. db.get("messageReactions").unset(content).write();
  28. msg.channel.send(`${msg.author.toString()} Removed reaction!`);
  29. },
  30. "reactions": msg => {
  31. let reactions = db.get("messageReactions").keys().value().reduce((p, c) => `${p}\n${c}`, "");
  32. msg.channel.send(`I'll react to the following messages:\n\`\`\`${reactions}\`\`\``);
  33. }
  34. };
  35. const onMessage = (msg, content, actionsDone) => {
  36. if (actionsDone)
  37. return false;
  38. let lowerContent = content.toLowerCase();
  39. if (db.get("messageReactions").has(lowerContent).value()) {
  40. msg.react(client.emojis.get(db.get("messageReactions").get(lowerContent).value()));
  41. return true;
  42. }
  43. if (msg.mentions.users.size == 0)
  44. return false;
  45. if (!db.get("reactableMentionedUsers").intersectionWith(msg.mentions.users.map(u => u.id)).isEmpty().value()) {
  46. const emoteId = db
  47. .get("emotes")
  48. .get("angery")
  49. .randomElement()
  50. .value();
  51. msg.react(client.emojis.find(e => e.id == emoteId));
  52. return true;
  53. }
  54. return false;
  55. };
  56. const onIndirectMention = (msg, actionsDone) => {
  57. if(actionsDone)
  58. return false;
  59. let emoteType = "angery";
  60. if (db.get("specialUsers").includes(msg.author.id).value())
  61. emoteType = "hug";
  62. else if (db.get("bigUsers").includes(msg.author.id).value())
  63. emoteType = "big";
  64. else if (db.get("dedUsers").includes(msg.author.id).value())
  65. emoteType = "ded";
  66. const id = db
  67. .get("emotes")
  68. .get(emoteType)
  69. .randomElement()
  70. .value();
  71. console.log(id);
  72. msg.channel.send(client.emojis.find(e => e.id == id).toString());
  73. return true;
  74. };
  75. module.exports = {
  76. commands: commands,
  77. onMessage: onMessage,
  78. onIndirectMention: onIndirectMention
  79. };