Add !alert

Using !alert in a modmail thread will ping you the next time the thread
gets a new reply. Use !alert cancel to cancel.
master
Dragory 2018-04-21 16:39:38 +03:00
parent 93e856aa18
commit 5e1b5112a2
4 changed files with 49 additions and 4 deletions

View File

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

View File

@ -19,6 +19,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS} = require('./constants');
* @property {String} scheduled_close_at * @property {String} scheduled_close_at
* @property {String} scheduled_close_id * @property {String} scheduled_close_id
* @property {String} scheduled_close_name * @property {String} scheduled_close_name
* @property {String} alert_id
* @property {String} created_at * @property {String} created_at
*/ */
class Thread { class Thread {
@ -145,10 +146,12 @@ class Thread {
if (this.scheduled_close_at) { if (this.scheduled_close_at) {
await this.cancelScheduledClose(); await this.cancelScheduledClose();
await this.postSystemMessage({ await this.postSystemMessage(`<@!${this.scheduled_close_id}> Thread that was scheduled to be closed got a new reply. Cancelling.`);
content: `<@!${this.scheduled_close_id}> Thread that was scheduled to be closed got a new reply. Cancelling.`, }
disableEveryone: false
}); 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<void>}
*/
async setAlert(userId) {
await knex('threads')
.where('id', this.id)
.update({
alert_id: userId
});
}
/** /**
* @returns {Promise<String>} * @returns {Promise<String>}
*/ */

View File

@ -20,6 +20,7 @@ const typingProxy = require('./modules/typingProxy');
const version = require('./modules/version'); const version = require('./modules/version');
const newthread = require('./modules/newthread'); const newthread = require('./modules/newthread');
const idModule = require('./modules/id'); const idModule = require('./modules/id');
const alert = require('./modules/alert');
const attachments = require("./data/attachments"); const attachments = require("./data/attachments");
const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants'); const {ACCIDENTAL_THREAD_MESSAGES} = require('./data/constants');
@ -183,6 +184,7 @@ module.exports = {
await version(bot); await version(bot);
await newthread(bot); await newthread(bot);
await idModule(bot); await idModule(bot);
await alert(bot);
// Connect to Discord // Connect to Discord
console.log('Connecting to Discord...'); console.log('Connecting to Discord...');

17
src/modules/alert.js Normal file
View File

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