Allow multiple users to do !alert without overriding each other

cshd
Dark 2020-02-17 20:04:43 +01:00
parent 034c12b2aa
commit 949a5efdb2
2 changed files with 59 additions and 8 deletions

View File

@ -169,8 +169,8 @@ class Thread {
} }
if (this.alert_id) { if (this.alert_id) {
await this.setAlert(null); await this.deleteAlerts();
await this.postSystemMessage(`<@!${this.alert_id}> New message from ${this.user_name}`); await this.postSystemMessage(`${this.alert_id} New message from ${this.user_name}`);
} }
} }
@ -449,14 +449,65 @@ class Thread {
* @param {String} userId * @param {String} userId
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async setAlert(userId) { async addAlert(userId) {
let alerts = await knex('threads')
.where('id', this.id)
.select('alert_id')
.first();
alerts = alerts.alert_id;
if (alerts == null) {
alerts = `<@!${userId}>`;
} else {
if (! alerts.includes(`<@!${userId}>`)) {
alerts += ` <@!${userId}>`;
}
}
await knex('threads') await knex('threads')
.where('id', this.id) .where('id', this.id)
.update({ .update({
alert_id: userId alert_id: alerts
}); });
} }
/**
* @param {String} userId
* @returns {Promise<void>}
*/
async removeAlert(userId) {
let alerts = await knex('threads')
.where('id', this.id)
.select('alert_id')
.first();
alerts = alerts.alert_id;
if (! (alerts == null)) {
if (alerts.startsWith(`<@!${userId}>`)) { // we do this to properly handle the spacing between @s
alerts = alerts.replace(`<@!${userId}> `, "");
} else {
alerts = alerts.replace(` <@!${userId}>`, "");
}
}
await knex('threads')
.where('id', this.id)
.update({
alert_id: alerts
});
}
/**
* @returns {Promise<void>}
*/
async deleteAlerts() {
await knex('threads')
.where('id', this.id)
.update({
alert_id: null
})
}
/** /**
* @returns {Promise<String>} * @returns {Promise<String>}
*/ */

View File

@ -1,10 +1,10 @@
module.exports = ({ bot, knex, config, commands }) => { module.exports = ({ bot, knex, config, commands }) => {
commands.addInboxThreadCommand('alert', '[opt:string]', async (msg, args, thread) => { commands.addInboxThreadCommand('alert', '[opt:string]', async (msg, args, thread) => {
if (args.opt && args.opt.startsWith('c')) { if (args.opt && args.opt.startsWith('c')) {
await thread.setAlert(null); await thread.removeAlert(msg.author.id)
await thread.postSystemMessage(`Cancelled new message alert`); await thread.postSystemMessage(`Cancelled your new message alert`);
} else { } else {
await thread.setAlert(msg.author.id); await thread.addAlert(msg.author.id);
await thread.postSystemMessage(`Pinging ${msg.author.username}#${msg.author.discriminator} when this thread gets a new reply`); await thread.postSystemMessage(`Pinging ${msg.author.username}#${msg.author.discriminator} when this thread gets a new reply`);
} }
}); });