react.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 documentation = {
  6. "react to \"<message>\" with <emote>": {
  7. auth: true,
  8. description: "React to <message> with <emote>."
  9. },
  10. "remove reaction to <message>": {
  11. auth: true,
  12. description: "Stops reacting to <message>."
  13. },
  14. "reactions": {
  15. auth: false,
  16. description: "Lists all known messages this bot can react to."
  17. }
  18. };
  19. const commands = {
  20. "react to": (msg, s) => {
  21. if (!util.isAuthorised(msg.member)) return;
  22. let contents = pattern.exec(s);
  23. if (contents != null) {
  24. let reactable = contents[1].trim().toLowerCase();
  25. let reactionEmoji = contents[2];
  26. if (!client.emojis.has(reactionEmoji)) {
  27. msg.channel.send(`${msg.author.toString()} I cannot react with this emoji :(`);
  28. return;
  29. }
  30. db.get("messageReactions").set(reactable, reactionEmoji).write();
  31. msg.channel.send(`${msg.author.toString()} Added reaction!`);
  32. }
  33. },
  34. "remove reaction to": (msg, s) => {
  35. if (!util.isAuthorised(msg.member)) return;
  36. let content = s.substring("remove reaction to ".length).trim().toLowerCase();
  37. if (!db.get("messageReactions").has(content).value()) {
  38. msg.channel.send(`${msg.author.toString()} No such reaction available!`);
  39. return;
  40. }
  41. db.get("messageReactions").unset(content).write();
  42. msg.channel.send(`${msg.author.toString()} Removed reaction!`);
  43. },
  44. "reactions": msg => {
  45. let reactions = db.get("messageReactions").keys().value().reduce((p, c) => `${p}\n${c}`, "");
  46. msg.channel.send(`I'll react to the following messages:\n\`\`\`${reactions}\`\`\``);
  47. }
  48. };
  49. const onMessage = (msg, content, actionsDone) => {
  50. if (actionsDone)
  51. return false;
  52. let lowerContent = content.toLowerCase();
  53. if (db.get("messageReactions").has(lowerContent).value()) {
  54. msg.react(client.emojis.get(db.get("messageReactions").get(lowerContent).value()));
  55. return true;
  56. }
  57. if (msg.mentions.users.size == 0)
  58. return false;
  59. if (!db.get("reactableMentionedUsers").intersectionWith(msg.mentions.users.map(u => u.id)).isEmpty().value()) {
  60. const emoteId = db
  61. .get("emotes")
  62. .get("angery")
  63. .randomElement()
  64. .value();
  65. msg.react(client.emojis.find(e => e.id == emoteId));
  66. return true;
  67. }
  68. return false;
  69. };
  70. const onIndirectMention = (msg, actionsDone) => {
  71. if(actionsDone)
  72. return false;
  73. let emoteType = "angery";
  74. if (db.get("specialUsers").includes(msg.author.id).value())
  75. emoteType = "hug";
  76. else if (db.get("bigUsers").includes(msg.author.id).value())
  77. emoteType = "big";
  78. else if (db.get("dedUsers").includes(msg.author.id).value())
  79. emoteType = "ded";
  80. let id = db
  81. .get("emotes")
  82. .get(emoteType)
  83. .randomElement()
  84. .value();
  85. let emote = client.emojis.find(e => e.id == id);
  86. if(!emote) {
  87. console.log(`WARNING: Emote ${id} no longer is valid. Deleting invalid emojis from the list...`);
  88. db.get("emotes")
  89. .get(emoteType)
  90. .remove(id => !client.emojis.has(id))
  91. .write();
  92. id = db
  93. .get("emotes")
  94. .get(emoteType)
  95. .randomElement()
  96. .value();
  97. emote = client.emojis.find(e => e.id == id);
  98. }
  99. if(!emote)
  100. return false;
  101. msg.channel.send(emote.toString());
  102. return true;
  103. };
  104. module.exports = {
  105. commands: commands,
  106. onMessage: onMessage,
  107. onIndirectMention: onIndirectMention,
  108. documentation: documentation
  109. };