react.js 4.0 KB

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