diff --git a/db/migrations/20180920224224_remove_is_anonymous_from_snippets.js b/db/migrations/20180920224224_remove_is_anonymous_from_snippets.js new file mode 100644 index 0000000..ac33267 --- /dev/null +++ b/db/migrations/20180920224224_remove_is_anonymous_from_snippets.js @@ -0,0 +1,11 @@ +exports.up = async function (knex, Promise) { + await knex.schema.table('snippets', table => { + table.dropColumn('is_anonymous'); + }); +}; + +exports.down = async function(knex, Promise) { + await knex.schema.table('snippets', table => { + table.integer('is_anonymous').unsigned().notNullable(); + }); +}; diff --git a/src/config.js b/src/config.js index 45ec2a7..bae358f 100644 --- a/src/config.js +++ b/src/config.js @@ -47,6 +47,7 @@ const defaultConfig = { "prefix": "!", "snippetPrefix": "!!", + "snippetPrefixAnon": "!!!", "status": "Message me for help!", "responseMessage": "Thank you for your message! Our mod team will reply to you here as soon as possible.", diff --git a/src/data/Snippet.js b/src/data/Snippet.js index 5a8982e..4d5b684 100644 --- a/src/data/Snippet.js +++ b/src/data/Snippet.js @@ -3,7 +3,6 @@ const utils = require("../utils"); /** * @property {String} trigger * @property {String} body - * @property {Number} is_anonymous * @property {String} created_by * @property {String} created_at */ diff --git a/src/data/snippets.js b/src/data/snippets.js index b0d98aa..a95b2b4 100644 --- a/src/data/snippets.js +++ b/src/data/snippets.js @@ -17,16 +17,14 @@ async function getSnippet(trigger) { /** * @param {String} trigger * @param {String} body - * @param {Boolean} isAnonymous * @returns {Promise} */ -async function addSnippet(trigger, body, isAnonymous = false, createdBy = 0) { +async function addSnippet(trigger, body, createdBy = 0) { if (await getSnippet(trigger)) return; return knex('snippets').insert({ trigger, body, - is_anonymous: isAnonymous ? 1 : 0, created_by: createdBy, created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss') }); diff --git a/src/modules/snippets.js b/src/modules/snippets.js index 4108e2c..5fcec44 100644 --- a/src/modules/snippets.js +++ b/src/modules/snippets.js @@ -16,16 +16,38 @@ module.exports = bot => { if (msg.author.bot) return; if (! msg.content) return; - if (! msg.content.startsWith(config.snippetPrefix)) return; + if (! msg.content.startsWith(config.snippetPrefix) && ! msg.content.startsWith(config.snippetPrefixAnon)) return; + + let snippetPrefix, isAnonymous; + + if (config.snippetPrefixAnon.length > config.snippetPrefix.length) { + // Anonymous prefix is longer -> check it first + if (msg.content.startsWith(config.snippetPrefixAnon)) { + snippetPrefix = config.snippetPrefixAnon; + isAnonymous = true; + } else { + snippetPrefix = config.snippetPrefix; + isAnonymous = false; + } + } else { + // Regular prefix is longer -> check it first + if (msg.content.startsWith(config.snippetPrefix)) { + snippetPrefix = config.snippetPrefix; + isAnonymous = false; + } else { + snippetPrefix = config.snippetPrefixAnon; + isAnonymous = true; + } + } const thread = await threads.findByChannelId(msg.channel.id); if (! thread) return; - const trigger = msg.content.replace(config.snippetPrefix, '').toLowerCase(); + const trigger = msg.content.replace(snippetPrefix, '').toLowerCase(); const snippet = await snippets.get(trigger); if (! snippet) return; - await thread.replyToUser(msg.member, snippet.body, [], !! snippet.is_anonymous); + await thread.replyToUser(msg.member, snippet.body, [], isAnonymous); msg.delete(); }); @@ -43,12 +65,12 @@ module.exports = bot => { utils.postSystemMessageWithFallback(msg.channel, thread, `Snippet "${trigger}" already exists! You can edit or delete it with ${config.prefix}edit_snippet and ${config.prefix}delete_snippet respectively.`); } else { // If the snippet exists and we're NOT trying to create a new one, show info about the existing snippet - utils.postSystemMessageWithFallback(msg.channel, thread, `\`${config.snippetPrefix}${trigger}\` replies ${snippet.is_anonymous ? 'anonymously ' : ''}with:\n${snippet.body}`); + utils.postSystemMessageWithFallback(msg.channel, thread, `\`${config.snippetPrefix}${trigger}\` replies with:\n${snippet.body}`); } } else { if (text) { // If the snippet doesn't exist and the user wants to create it, create it - await snippets.add(trigger, text, false); + await snippets.add(trigger, text, msg.author.id); utils.postSystemMessageWithFallback(msg.channel, thread, `Snippet "${trigger}" created!`); } else { // If the snippet doesn't exist and the user isn't trying to create it, inform them how to create it @@ -89,7 +111,7 @@ module.exports = bot => { } await snippets.del(trigger); - await snippets.add(trigger, text, snippet.isAnonymous); + await snippets.add(trigger, text, msg.author.id); utils.postSystemMessageWithFallback(msg.channel, thread, `Snippet "${trigger}" edited!`); });