123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- const db = require("../db.js");
- const util = require("../util.js");
- const documentation = {
- "add quote by \"<author>\" <NEWLINE> <quote>": {
- auth: true,
- description: "Adds a quote"
- },
- "remove quote <quote_index>": {
- auth: true,
- description: "Removes quote. Use \"quotes\" to get the <quote_index>!"
- },
- "quotes": {
- auth: true,
- description: "Lists all known quotes."
- },
- "random quote": {
- auth: false,
- description: "Shows a random quote by someone special..."
- }
- };
- const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
- function minify(str, maxLength) {
- let result = str.replace("\n", "");
- if (result.length > maxLength)
- result = `${result.substring(0, maxLength - 3)}...`;
- return result;
- }
- const commands = [
- {
- pattern: "add quote",
- action: (msg, c) => {
- if (!util.isAuthorised(msg.member))
- return;
-
- let result = quotePattern.exec(c);
-
- if (result == null)
- return;
-
- let author = result[1].trim();
- let message = result[2].trim();
-
- db.get("quotes").push({
- author: author,
- message: message
- }).write();
-
- msg.channel.send(`${msg.author.toString()} Added quote #${db.get("quotes").size().value()}!`);
- }
- },
- {
- pattern: "random quote",
- action: (msg) => {
- if (db.get("quotes").size().value() == 0) {
- msg.channel.send("I have no quotes!");
- return;
- }
- let quote = db.get("quotes").randomElement().value();
- let index = db.get("quotes").indexOf(quote).value();
- msg.channel.send(`Quote #${index + 1}:\n*"${quote.message}"*\n- ${quote.author}`);
- }
- },
- {
- pattern: "remove quote",
- action: (msg, c) => {
- let quoteNum = c.substring("remove quote".length).trim();
- let val = parseInt(quoteNum);
- if (isNaN(val) || db.get("quotes").size().value() < val - 1)
- return;
-
- db.get("quotes").pullAt(val - 1).write();
- msg.channel.send(`${msg.author.toString()} Removed quote #${val}!`);
- }
- },
- {
- pattern: "quotes",
- action: msg => {
- if (!util.isAuthorised(msg.member)) {
- msg.channel.send(`${msg.author.toString()} To prevent spamming, only bot moderators can view all quotes!`);
- return;
- }
-
- if (db.get("quotes").size().value() == 0) {
- msg.channel.send("I have no quotes!");
- return;
- }
-
- let quotes = db.get("quotes").reduce((prev, curr, i) => `${prev}[${i+1}] "${minify(curr.message, 10)}" by ${curr.author}\n`, "\n").value();
- msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotes}\`\`\``);
- }
- }
- ];
- module.exports = {
- commands: commands,
- documentation: documentation
- };
|