const Discord = require("discord.js"); const TOKEN = require("./token.js"); const lowdb = require("lowdb"); const FileSync = require("lowdb/adapters/FileSync"); const adapter = new FileSync(__dirname + "/db.json"); const db = lowdb(adapter); db._.mixin({ randomElement: array => array[Math.floor(Math.random() * array.length)] }); db.defaults({ emotes: { angery: [ "488797492352385034", "488797455899688971", "488796668750200842", "488796396175097876", "488793566982963231", "488793511181811749", "488791172085448718", "489091926474096651" ], hug: [ "489094486132260864", "489094428410249218", "489094553148850176", "489094604935790622", "485109695846023209" ] }, specialUsers: ["141880968800763905", "307897683849510912", "335898304472678402"], guides: {}, editors: { roles: ["484046157971193866", "305844721622712322"], users: ["307897683849510912", "170116346095468545", "335898304472678402"] } }).write(); const client = new Discord.Client(); function isAuthorised(member) { if(db.get("editors.users").includes(member.id).value()) return true; if(db.get("editors.roles").intersectionWith(member.roles.keyArray()).isEmpty().value()) return false; return true; } const commands = { "make guide": (msg, s) => { if(!isAuthorised(msg.member)) return; let content = s.substring("make guide ".length); let guideName = content.substring(0, content.indexOf("\n")).trim(); let guideContent = content.substring(content.indexOf("\n")).trim(); db.get("guides") .set(guideName, guideContent) .write(); msg.channel.send( `${msg.author.toString()} Added/updated "${guideName}"!` ); }, "delete guide": (msg, s) => { if(!isAuthorised(msg.member)) return; let guideName = s.substring("delete guide ".length).trim(); if ( !db .get("guides") .has(guideName) .value() ) { msg.channel.send( `${msg.author.toString()} No guide "${guideName}"!` ); return; } db.get("guides") .unset(guideName) .write(); msg.channel.send( `${msg.author.toString()} Removed guide "${guideName}!"` ); }, help: msg => { let guides = db .get("guides") .keys() .value() .reduce((p, c) => `${p}\n${c}`, ""); msg.channel.send( `Hello! I am NoctBot! I have answers for the most common C(O)M-related questions.\nJust ping me with one of the following commands:\n\`\`\`${guides}\`\`\`` ); } }; client.on("ready", () => { console.log("Ready!"); client.user.setActivity("@NoctBot help"); }); client.on("message", m => { if (m.mentions.users.size == 0) return; if (m.mentions.users.first().id == client.user.id) { let content = m.cleanContent.substring( `@${client.user.username} `.length ); for (let c in commands) { if (content.startsWith(c)) { commands[c](m, content); return; } } let guide = db .get("guides") .get(content.trim()) .value(); if (guide) { m.channel.send(guide); return; } const emoteType = db .get("specialUsers") .includes(m.author.id) .value() ? "hug" : "angery"; const id = db .get("emotes") .get(emoteType) .randomElement() .value(); m.channel.send(client.emojis.find(e => e.id == id).toString()); } else if (m.content.includes("Noct")) { m.channel.send( client.emojis.find(e => e.name == "mukuNeighWaaaaaa").toString() ); } }); client.login(TOKEN);