diff --git a/db/migrations/20180421161550_add_alert_id_to_threads.js b/db/migrations/20180421161550_add_alert_id_to_threads.js new file mode 100644 index 0000000..5defc38 --- /dev/null +++ b/db/migrations/20180421161550_add_alert_id_to_threads.js @@ -0,0 +1,11 @@ +exports.up = async function (knex, Promise) { + await knex.schema.table('threads', table => { + table.string('alert_id', 20).nullable().defaultTo(null).after('scheduled_close_name'); + }); +}; + +exports.down = async function(knex, Promise) { + await knex.schema.table('threads', table => { + table.dropColumn('alert_id'); + }); +}; diff --git a/src/data/Thread.js b/src/data/Thread.js index 24ce90e..136343a 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -19,6 +19,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS} = require('./constants'); * @property {String} scheduled_close_at * @property {String} scheduled_close_id * @property {String} scheduled_close_name + * @property {String} alert_id * @property {String} created_at */ class Thread { @@ -145,10 +146,12 @@ class Thread { if (this.scheduled_close_at) { await this.cancelScheduledClose(); - await this.postSystemMessage({ - content: `<@!${this.scheduled_close_id}> Thread that was scheduled to be closed got a new reply. Cancelling.`, - disableEveryone: false - }); + await this.postSystemMessage(`<@!${this.scheduled_close_id}> Thread that was scheduled to be closed got a new reply. Cancelling.`); + } + + if (this.alert_id) { + await this.setAlert(null); + await this.postSystemMessage(`<@!${this.alert_id}> New message from ${this.user_name}`); } } @@ -368,6 +371,18 @@ class Thread { }); } + /** + * @param {String} userId + * @returns {Promise} + */ + async setAlert(userId) { + await knex('threads') + .where('id', this.id) + .update({ + alert_id: userId + }); + } + /** * @returns {Promise} */ diff --git a/src/main.js b/src/main.js index 92acf67..0b23fd9 100644 --- a/src/main.js +++ b/src/main.js @@ -20,6 +20,7 @@ const typingProxy = require('./modules/typingProxy'); const version = require('./modules/version'); const newthread = require('./modules/newthread'); const idModule = require('./modules/id'); +const alert = require('./modules/alert'); const attachments = require("./data/attachments"); const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants'); @@ -183,6 +184,7 @@ module.exports = { await version(bot); await newthread(bot); await idModule(bot); + await alert(bot); // Connect to Discord console.log('Connecting to Discord...'); diff --git a/src/modules/alert.js b/src/modules/alert.js new file mode 100644 index 0000000..1db7f03 --- /dev/null +++ b/src/modules/alert.js @@ -0,0 +1,17 @@ +const threadUtils = require('../threadUtils'); + +module.exports = bot => { + const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args); + + addInboxServerCommand('alert', async (msg, args, thread) => { + if (! thread) return; + + if (args[0] && args[0].startsWith('c')) { + await thread.setAlert(null); + await thread.postSystemMessage(`Cancelled new message alert`); + } else { + await thread.setAlert(msg.author.id); + await thread.postSystemMessage(`Pinging ${msg.author.username}#${msg.author.discriminator} when this thread gets a new reply`); + } + }); +};