main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const Discord = require("discord.js");
  2. const TOKEN = require("./token.js");
  3. const lowdb = require("lowdb");
  4. const FileSync = require("lowdb/adapters/FileSync");
  5. const adapter = new FileSync(__dirname + "/db.json");
  6. const db = lowdb(adapter);
  7. db._.mixin({
  8. randomElement: array => array[Math.floor(Math.random() * array.length)]
  9. });
  10. db.defaults({
  11. emotes: {
  12. angery: [
  13. "488797492352385034",
  14. "488797455899688971",
  15. "488796668750200842",
  16. "488796396175097876",
  17. "488793566982963231",
  18. "488793511181811749",
  19. "488791172085448718",
  20. "489091926474096651"
  21. ],
  22. hug: [
  23. "489094486132260864",
  24. "489094428410249218",
  25. "489094553148850176",
  26. "489094604935790622",
  27. "485109695846023209"
  28. ]
  29. },
  30. specialUsers: [
  31. "141880968800763905",
  32. "307897683849510912",
  33. "335898304472678402"
  34. ],
  35. reactableMentionedUsers: ["307897683849510912", "335898304472678402"],
  36. guides: [],
  37. editors: {
  38. roles: ["484046157971193866", "305844721622712322"],
  39. users: [
  40. "307897683849510912",
  41. "170116346095468545",
  42. "335898304472678402"
  43. ]
  44. }
  45. }).write();
  46. const client = new Discord.Client();
  47. function isAuthorised(member) {
  48. if (
  49. db
  50. .get("editors.users")
  51. .includes(member.id)
  52. .value()
  53. )
  54. return true;
  55. if (
  56. db
  57. .get("editors.roles")
  58. .intersectionWith(member.roles.keyArray())
  59. .isEmpty()
  60. .value()
  61. )
  62. return false;
  63. return true;
  64. }
  65. const commands = {
  66. "make guide": msg => {
  67. if (!isAuthorised(msg.member)) return;
  68. let content = msg.content.substring(msg.content.indexOf("make guide") + "make guide ".length);
  69. let guideName = content.substring(0, content.indexOf("\n")).trim();
  70. let guideContent = content.substring(content.indexOf("\n")).trim();
  71. let guide = db.get("guides").find({ name: guideName });
  72. if (!guide.isUndefined().value()) {
  73. guide.assign({ content: guideContent }).write();
  74. } else {
  75. db.get("guides")
  76. .push({
  77. name: guideName,
  78. content: guideContent
  79. })
  80. .write();
  81. }
  82. msg.channel.send(
  83. `${msg.author.toString()} Added/updated "${guideName}"!`
  84. );
  85. },
  86. "delete guide": (msg, s) => {
  87. if (!isAuthorised(msg.member)) return;
  88. let guideName = s.substring("delete guide ".length).trim();
  89. let val = db.get("guides").find({ name: guideName });
  90. if (val.isUndefined().value()) {
  91. msg.channel.send(`${msg.author.toString()} No guide "${guideName}"!`);
  92. return;
  93. }
  94. db.get("guides")
  95. .remove({ name: guideName })
  96. .write();
  97. msg.channel.send(
  98. `${msg.author.toString()} Removed guide "${guideName}!"`
  99. );
  100. },
  101. help: msg => {
  102. let guides = db
  103. .get("guides")
  104. .map(g => g.name)
  105. .reduce((p, c) => `${p}\n${c}`, "")
  106. .value();
  107. msg.channel.send(
  108. `Hello! I am NoctBot! I have answers for the most common C(O)M-related questions.\nJust ping me with one of the following keywords:\n\`\`\`${guides}\`\`\``
  109. );
  110. }
  111. };
  112. client.on("ready", () => {
  113. console.log("Ready!");
  114. client.user.setActivity("@NoctBot help", { type: "PLAYING" });
  115. });
  116. client.on("message", m => {
  117. if (m.author.id == client.user.id || m.mentions.users.size == 0) return;
  118. if (!db.get("reactableMentionedUsers").intersectionWith(m.mentions.users.map(u => u.id)).isEmpty().value()) {
  119. const emoteId = db
  120. .get("emotes")
  121. .get("angery")
  122. .randomElement()
  123. .value();
  124. m.react(client.emojis.find(e => e.id == emoteId));
  125. return;
  126. }
  127. if (m.mentions.users.first().id == client.user.id) {
  128. let content = m.cleanContent
  129. .substring(`@${client.user.username} `.length);
  130. let lowerCaseContent = content.toLowerCase().trim();
  131. for (let c in commands) {
  132. if (lowerCaseContent.startsWith(c)) {
  133. commands[c](m, content);
  134. return;
  135. }
  136. }
  137. if (lowerCaseContent.length > 0) {
  138. let parts = lowerCaseContent.trim().split(" ");
  139. let guide = db
  140. .get("guides")
  141. .map(g => Object.assign({parts: g.name.toLowerCase().split(" ")}, g))
  142. .sortBy(g => g.parts.length)
  143. .maxBy(k => db._.intersection(parts, k.parts).length)
  144. .value();
  145. let hits =
  146. guide !== undefined &&
  147. db._.intersection(guide.name.toLowerCase().split(" "), parts).length > 0;
  148. if (hits) {
  149. m.channel.send(guide.content);
  150. return;
  151. }
  152. }
  153. const emoteType = db
  154. .get("specialUsers")
  155. .includes(m.author.id)
  156. .value()
  157. ? "hug"
  158. : "angery";
  159. const id = db
  160. .get("emotes")
  161. .get(emoteType)
  162. .randomElement()
  163. .value();
  164. m.channel.send(client.emojis.find(e => e.id == id).toString());
  165. } else if (m.content.includes("Noct")) {
  166. m.channel.send(
  167. client.emojis.find(e => e.name == "mukuNeighWaaaaaa").toString()
  168. );
  169. }
  170. });
  171. client.login(TOKEN);