Add internal support for editing/deleting staff replies

cshd
Dragory 2020-07-19 14:11:38 +03:00
parent 6b8c7e1bdf
commit 4e9e347b04
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 171 additions and 13 deletions

View File

@ -286,18 +286,22 @@ class Thread {
/** /**
* @param {Eris.MessageContent} content * @param {Eris.MessageContent} content
* @param {Eris.MessageFile} file * @param {Eris.MessageFile} file
* @param {object} opts
* @param {boolean} opts.saveToLog
* @param {string} opts.logBody
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async postSystemMessage(content, file = null) { async postSystemMessage(content, file = null, opts = {}) {
const msg = await this._postToThreadChannel(content, file); const msg = await this._postToThreadChannel(content, file);
if (msg) { if (msg && opts.saveToLog !== false) {
const finalLogBody = opts.logBody || msg.content || '<empty message>';
await this._addThreadMessageToDB({ await this._addThreadMessageToDB({
message_type: THREAD_MESSAGE_TYPE.SYSTEM, message_type: THREAD_MESSAGE_TYPE.SYSTEM,
user_id: null, user_id: null,
user_name: '', user_name: '',
body: typeof content === 'string' ? content : content.content, body: finalLogBody,
is_anonymous: 0, is_anonymous: 0,
dm_message_id: msg.id inbox_message_id: msg.id,
}); });
} }
} }
@ -305,18 +309,24 @@ class Thread {
/** /**
* @param {Eris.MessageContent} content * @param {Eris.MessageContent} content
* @param {Eris.MessageFile} file * @param {Eris.MessageFile} file
* @param {object} opts
* @param {boolean} opts.saveToLog
* @param {string} opts.logBody
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async sendSystemMessageToUser(content, file = null) { async sendSystemMessageToUser(content, file = null, opts = {}) {
const msg = await this._sendDMToUser(content, file); const msg = await this._sendDMToUser(content, file);
await this._addThreadMessageToDB({ if (opts.saveToLog !== false) {
message_type: THREAD_MESSAGE_TYPE.SYSTEM_TO_USER, const finalLogBody = opts.logBody || msg.content || '<empty message>';
user_id: null, await this._addThreadMessageToDB({
user_name: '', message_type: THREAD_MESSAGE_TYPE.SYSTEM_TO_USER,
body: typeof content === 'string' ? content : content.content, user_id: null,
is_anonymous: 0, user_name: '',
dm_message_id: msg.id body: finalLogBody,
}); is_anonymous: 0,
dm_message_id: msg.id,
});
}
} }
/** /**
@ -529,6 +539,56 @@ class Thread {
}); });
} }
/**
* @param {Eris.Member} moderator
* @param {ThreadMessage} threadMessage
* @param {string} newText
* @param {object} opts
* @param {boolean} opts.quiet Whether to suppress edit notifications in the thread channel
* @returns {Promise<void>}
*/
async editStaffReply(moderator, threadMessage, newText, opts = {}) {
const formattedThreadMessage = formatters.formatStaffReplyThreadMessage(
moderator,
newText,
threadMessage.message_number,
{ isAnonymous: threadMessage.is_anonymous }
);
const formattedDM = formatters.formatStaffReplyDM(
moderator,
newText,
{ isAnonymous: threadMessage.is_anonymous }
);
await bot.editMessage(threadMessage.dm_channel_id, threadMessage.dm_message_id, formattedDM);
await bot.editMessage(this.channel_id, threadMessage.inbox_message_id, formattedThreadMessage);
if (! opts.quiet) {
const threadNotification = formatters.formatStaffReplyEditNotificationThreadMessage(moderator, threadMessage, newText);
const logNotification = formatters.formatStaffReplyEditNotificationLogMessage(moderator, threadMessage, newText);
await this.postSystemMessage(threadNotification, null, { logBody: logNotification });
}
}
/**
* @param {Eris.Member} moderator
* @param {ThreadMessage} threadMessage
* @param {object} opts
* @param {boolean} opts.quiet Whether to suppress edit notifications in the thread channel
* @returns {Promise<void>}
*/
async deleteStaffReply(moderator, threadMessage, opts = {}) {
await bot.deleteMessage(threadMessage.dm_channel_id, threadMessage.dm_message_id);
await bot.deleteMessage(this.channel_id, threadMessage.inbox_message_id);
if (! opts.quiet) {
const threadNotification = formatters.formatStaffReplyDeletionNotificationThreadMessage(moderator, threadMessage);
const logNotification = formatters.formatStaffReplyDeletionNotificationLogMessage(moderator, threadMessage);
await this.postSystemMessage(threadNotification, null, { logBody: logNotification });
}
}
/** /**
* @returns {Promise<String>} * @returns {Promise<String>}
*/ */

View File

@ -1,6 +1,7 @@
const Eris = require('eris'); const Eris = require('eris');
const utils = require('./utils'); const utils = require('./utils');
const config = require('./cfg'); const config = require('./cfg');
const ThreadMessage = require('./data/ThreadMessage');
/** /**
* Function to format the DM that is sent to the user when a staff member replies to them via !reply * Function to format the DM that is sent to the user when a staff member replies to them via !reply
@ -49,6 +50,7 @@ const config = require('./cfg');
*/ */
/** /**
* Function to format a user reply in a log
* @callback FormatUserReplyLogMessage * @callback FormatUserReplyLogMessage
* @param {Eris.User} user * @param {Eris.User} user
* @param {Eris.Message} msg * @param {Eris.Message} msg
@ -58,6 +60,40 @@ const config = require('./cfg');
* @return {string} Text to show in the log * @return {string} Text to show in the log
*/ */
/**
* Function to format the inbox channel notification for a staff reply edit
* @callback FormatStaffReplyEditNotificationThreadMessage
* @param {Eris.Member} moderator
* @param {ThreadMessage} threadMessage
* @param {string} newText
* @return {Eris.MessageContent} Message content to post in the thread channel
*/
/**
* Function to format the log notification for a staff reply edit
* @callback FormatStaffReplyEditNotificationLogMessage
* @param {Eris.Member} moderator
* @param {ThreadMessage} threadMessage
* @param {string} newText
* @return {string} Text to show in the log
*/
/**
* Function to format the inbox channel notification for a staff reply deletion
* @callback FormatStaffReplyDeletionNotificationThreadMessage
* @param {Eris.Member} moderator
* @param {ThreadMessage} threadMessage
* @return {Eris.MessageContent} Message content to post in the thread channel
*/
/**
* Function to format the log notification for a staff reply deletion
* @callback FormatStaffReplyDeletionNotificationLogMessage
* @param {Eris.Member} moderator
* @param {ThreadMessage} threadMessage
* @return {string} Text to show in the log
*/
/** /**
* @typedef MessageFormatters * @typedef MessageFormatters
* @property {FormatStaffReplyDM} formatStaffReplyDM * @property {FormatStaffReplyDM} formatStaffReplyDM
@ -65,6 +101,10 @@ const config = require('./cfg');
* @property {FormatStaffReplyLogMessage} formatStaffReplyLogMessage * @property {FormatStaffReplyLogMessage} formatStaffReplyLogMessage
* @property {FormatUserReplyThreadMessage} formatUserReplyThreadMessage * @property {FormatUserReplyThreadMessage} formatUserReplyThreadMessage
* @property {FormatUserReplyLogMessage} formatUserReplyLogMessage * @property {FormatUserReplyLogMessage} formatUserReplyLogMessage
* @property {FormatStaffReplyEditNotificationThreadMessage} formatStaffReplyEditNotificationThreadMessage
* @property {FormatStaffReplyEditNotificationLogMessage} formatStaffReplyEditNotificationLogMessage
* @property {FormatStaffReplyDeletionNotificationThreadMessage} formatStaffReplyDeletionNotificationThreadMessage
* @property {FormatStaffReplyDeletionNotificationLogMessage} formatStaffReplyDeletionNotificationLogMessage
*/ */
/** /**
@ -156,6 +196,32 @@ const defaultFormatters = {
return result; return result;
}, },
formatStaffReplyEditNotificationThreadMessage(moderator, threadMessage, newText) {
let content = `**${moderator.user.username}#${moderator.user.discriminator} (\`${moderator.id}\`) edited reply \`[${threadMessage.message_number}]\`:**`;
content += `\n\`B:\` ${threadMessage.body}`;
content += `\n\`A:\` ${newText}`;
return utils.disableLinkPreviews(content);
},
formatStaffReplyEditNotificationLogMessage(moderator, threadMessage, newText) {
let content = `${moderator.user.username}#${moderator.user.discriminator} (${moderator.id}) edited reply [${threadMessage.message_number}]:`;
content += `\nB: ${threadMessage.body}`;
content += `\nA: ${newText}`;
return content;
},
formatStaffReplyDeletionNotificationThreadMessage(moderator, threadMessage) {
let content = `**${moderator.user.username}#${moderator.user.discriminator} (\`${moderator.id}\`) deleted reply \`[${threadMessage.message_number}]\`:**`;
content += `\n\`B:\` ${threadMessage.body}`;
return utils.disableLinkPreviews(content);
},
formatStaffReplyDeletionNotificationLogMessage(moderator, threadMessage) {
let content = `${moderator.user.username}#${moderator.user.discriminator} (${moderator.id}) deleted reply [${threadMessage.message_number}]:`;
content += `\nB: ${threadMessage.body}`;
return content;
},
}; };
/** /**
@ -205,4 +271,36 @@ module.exports = {
setUserReplyLogMessageFormatter(fn) { setUserReplyLogMessageFormatter(fn) {
formatters.formatUserReplyLogMessage = fn; formatters.formatUserReplyLogMessage = fn;
}, },
/**
* @param {FormatStaffReplyEditNotificationThreadMessage} fn
* @return {void}
*/
setStaffReplyEditNotificationThreadMessageFormatter(fn) {
formatters.formatStaffReplyEditNotificationThreadMessage = fn;
},
/**
* @param {FormatStaffReplyEditNotificationLogMessage} fn
* @return {void}
*/
setStaffReplyEditNotificationLogMessageFormatter(fn) {
formatters.formatStaffReplyEditNotificationLogMessage = fn;
},
/**
* @param {FormatStaffReplyDeletionNotificationThreadMessage} fn
* @return {void}
*/
setStaffReplyDeletionNotificationThreadMessageFormatter(fn) {
formatters.formatStaffReplyDeletionNotificationThreadMessage = fn;
},
/**
* @param {FormatStaffReplyDeletionNotificationLogMessage} fn
* @return {void}
*/
setStaffReplyDeletionNotificationLogMessageFormatter(fn) {
formatters.formatStaffReplyDeletionNotificationLogMessage = fn;
},
}; };