Add support for sending anonymous snippets. Add snippetPrefixAnon option. Fixes #82

master
Dragory 2018-09-20 22:54:47 +03:00
parent 9f72f81a73
commit 2f1b51d97a
5 changed files with 41 additions and 10 deletions

View File

@ -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();
});
};

View File

@ -47,6 +47,7 @@ const defaultConfig = {
"prefix": "!", "prefix": "!",
"snippetPrefix": "!!", "snippetPrefix": "!!",
"snippetPrefixAnon": "!!!",
"status": "Message me for help!", "status": "Message me for help!",
"responseMessage": "Thank you for your message! Our mod team will reply to you here as soon as possible.", "responseMessage": "Thank you for your message! Our mod team will reply to you here as soon as possible.",

View File

@ -3,7 +3,6 @@ const utils = require("../utils");
/** /**
* @property {String} trigger * @property {String} trigger
* @property {String} body * @property {String} body
* @property {Number} is_anonymous
* @property {String} created_by * @property {String} created_by
* @property {String} created_at * @property {String} created_at
*/ */

View File

@ -17,16 +17,14 @@ async function getSnippet(trigger) {
/** /**
* @param {String} trigger * @param {String} trigger
* @param {String} body * @param {String} body
* @param {Boolean} isAnonymous
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async function addSnippet(trigger, body, isAnonymous = false, createdBy = 0) { async function addSnippet(trigger, body, createdBy = 0) {
if (await getSnippet(trigger)) return; if (await getSnippet(trigger)) return;
return knex('snippets').insert({ return knex('snippets').insert({
trigger, trigger,
body, body,
is_anonymous: isAnonymous ? 1 : 0,
created_by: createdBy, created_by: createdBy,
created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss') created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss')
}); });

View File

@ -16,16 +16,38 @@ module.exports = bot => {
if (msg.author.bot) return; if (msg.author.bot) return;
if (! msg.content) 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); const thread = await threads.findByChannelId(msg.channel.id);
if (! thread) return; if (! thread) return;
const trigger = msg.content.replace(config.snippetPrefix, '').toLowerCase(); const trigger = msg.content.replace(snippetPrefix, '').toLowerCase();
const snippet = await snippets.get(trigger); const snippet = await snippets.get(trigger);
if (! snippet) return; if (! snippet) return;
await thread.replyToUser(msg.member, snippet.body, [], !! snippet.is_anonymous); await thread.replyToUser(msg.member, snippet.body, [], isAnonymous);
msg.delete(); 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.`); 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 { } else {
// If the snippet exists and we're NOT trying to create a new one, show info about the existing snippet // 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 { } else {
if (text) { if (text) {
// If the snippet doesn't exist and the user wants to create it, create it // 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!`); utils.postSystemMessageWithFallback(msg.channel, thread, `Snippet "${trigger}" created!`);
} else { } else {
// If the snippet doesn't exist and the user isn't trying to create it, inform them how to create it // 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.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!`); utils.postSystemMessageWithFallback(msg.channel, thread, `Snippet "${trigger}" edited!`);
}); });