Create new message types for staff reply edits/deletions

This has backwards-compatibility-breaking changes to the formatters
of staff reply edits/deletions, which now only receive the
thread message for the edit/deletion with the original data in
the thread message's metadata.
cshd
Dragory 2020-10-12 20:54:42 +03:00
parent 5de750bc3e
commit 5b0f9d31b7
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
3 changed files with 55 additions and 20 deletions

View File

@ -745,8 +745,20 @@ class Thread {
await bot.editMessage(this.channel_id, threadMessage.inbox_message_id, formattedThreadMessage); await bot.editMessage(this.channel_id, threadMessage.inbox_message_id, formattedThreadMessage);
if (! opts.quiet) { if (! opts.quiet) {
const threadNotification = formatters.formatStaffReplyEditNotificationThreadMessage(threadMessage, newText, moderator); const editThreadMessage = new ThreadMessage({
await this.postSystemMessage(threadNotification); message_type: THREAD_MESSAGE_TYPE.REPLY_EDITED,
user_id: null,
user_name: "",
body: "",
is_anonymous: 0,
});
editThreadMessage.setMetadataValue("originalThreadMessage", threadMessage);
editThreadMessage.setMetadataValue("newBody", newText);
const threadNotification = formatters.formatStaffReplyEditNotificationThreadMessage(editThreadMessage);
const inboxMessage = await this._postToThreadChannel(threadNotification);
editThreadMessage.inbox_message_id = inboxMessage.id;
await this._addThreadMessageToDB(editThreadMessage.getSQLProps());
} }
await this._updateThreadMessage(threadMessage.id, { body: newText }); await this._updateThreadMessage(threadMessage.id, { body: newText });
@ -764,8 +776,19 @@ class Thread {
await bot.deleteMessage(this.channel_id, threadMessage.inbox_message_id); await bot.deleteMessage(this.channel_id, threadMessage.inbox_message_id);
if (! opts.quiet) { if (! opts.quiet) {
const threadNotification = formatters.formatStaffReplyDeletionNotificationThreadMessage(threadMessage, moderator); const deletionThreadMessage = new ThreadMessage({
await this.postSystemMessage(threadNotification); message_type: THREAD_MESSAGE_TYPE.REPLY_DELETED,
user_id: null,
user_name: "",
body: "",
is_anonymous: 0,
});
deletionThreadMessage.setMetadataValue("originalThreadMessage", threadMessage);
const threadNotification = formatters.formatStaffReplyDeletionNotificationThreadMessage(deletionThreadMessage);
const inboxMessage = await this._postToThreadChannel(threadNotification);
deletionThreadMessage.inbox_message_id = inboxMessage.id;
await this._addThreadMessageToDB(deletionThreadMessage.getSQLProps());
} }
await this._deleteThreadMessage(threadMessage.id); await this._deleteThreadMessage(threadMessage.id);

View File

@ -12,7 +12,9 @@ module.exports = {
TO_USER: 4, TO_USER: 4,
LEGACY: 5, LEGACY: 5,
COMMAND: 6, COMMAND: 6,
SYSTEM_TO_USER: 7 SYSTEM_TO_USER: 7,
REPLY_EDITED: 8,
REPLY_DELETED: 9,
}, },
// https://discord.com/developers/docs/resources/channel#channel-object-channel-types // https://discord.com/developers/docs/resources/channel#channel-object-channel-types

View File

@ -30,8 +30,6 @@ const moment = require("moment");
* Function to format the inbox channel notification for a staff reply edit * Function to format the inbox channel notification for a staff reply edit
* @callback FormatStaffReplyEditNotificationThreadMessage * @callback FormatStaffReplyEditNotificationThreadMessage
* @param {ThreadMessage} threadMessage * @param {ThreadMessage} threadMessage
* @param {string} newText
* @param {Eris.Member} moderator Moderator that edited the message
* @return {Eris.MessageContent} Message content to post in the thread channel * @return {Eris.MessageContent} Message content to post in the thread channel
*/ */
@ -39,7 +37,6 @@ const moment = require("moment");
* Function to format the inbox channel notification for a staff reply deletion * Function to format the inbox channel notification for a staff reply deletion
* @callback FormatStaffReplyDeletionNotificationThreadMessage * @callback FormatStaffReplyDeletionNotificationThreadMessage
* @param {ThreadMessage} threadMessage * @param {ThreadMessage} threadMessage
* @param {Eris.Member} moderator Moderator that deleted the message
* @return {Eris.MessageContent} Message content to post in the thread channel * @return {Eris.MessageContent} Message content to post in the thread channel
*/ */
@ -142,31 +139,35 @@ const defaultFormatters = {
return result; return result;
}, },
formatStaffReplyEditNotificationThreadMessage(threadMessage, newText, moderator) { formatStaffReplyEditNotificationThreadMessage(threadMessage) {
let content = `**${moderator.user.username}#${moderator.user.discriminator}** (\`${moderator.id}\`) edited reply \`${threadMessage.message_number}\``; const originalThreadMessage = threadMessage.getMetadataValue("originalThreadMessage");
const newBody = threadMessage.getMetadataValue("newBody");
if (threadMessage.body.length < 200 && newText.length < 200) { let content = `**${originalThreadMessage.user_name}** (\`${originalThreadMessage.user_id}\`) edited reply \`${originalThreadMessage.message_number}\``;
if (originalThreadMessage.body.length < 200 && newBody.length < 200) {
// Show edits of small messages inline // Show edits of small messages inline
content += ` from \`${utils.disableInlineCode(threadMessage.body)}\` to \`${newText}\``; content += ` from \`${utils.disableInlineCode(originalThreadMessage.body)}\` to \`${newBody}\``;
} else { } else {
// Show edits of long messages in two code blocks // Show edits of long messages in two code blocks
content += ":"; content += ":";
content += `\n\nBefore:\n\`\`\`${utils.disableCodeBlocks(threadMessage.body)}\`\`\``; content += `\n\nBefore:\n\`\`\`${utils.disableCodeBlocks(originalThreadMessage.body)}\`\`\``;
content += `\nAfter:\n\`\`\`${utils.disableCodeBlocks(newText)}\`\`\``; content += `\nAfter:\n\`\`\`${utils.disableCodeBlocks(newBody)}\`\`\``;
} }
return content; return content;
}, },
formatStaffReplyDeletionNotificationThreadMessage(threadMessage, moderator) { formatStaffReplyDeletionNotificationThreadMessage(threadMessage) {
let content = `**${moderator.user.username}#${moderator.user.discriminator}** (\`${moderator.id}\`) deleted reply \`${threadMessage.message_number}\``; const originalThreadMessage = threadMessage.getMetadataValue("originalThreadMessage");
let content = `**${originalThreadMessage.user_name}** (\`${originalThreadMessage.user_id}\`) deleted reply \`${originalThreadMessage.message_number}\``;
if (threadMessage.body.length < 200) { if (originalThreadMessage.body.length < 200) {
// Show the original content of deleted small messages inline // Show the original content of deleted small messages inline
content += ` (message content: \`${utils.disableInlineCode(threadMessage.body)}\`)`; content += ` (message content: \`${utils.disableInlineCode(originalThreadMessage.body)}\`)`;
} else { } else {
// Show the original content of deleted large messages in a code block // Show the original content of deleted large messages in a code block
content += ":\n```" + utils.disableCodeBlocks(threadMessage.body) + "```"; content += ":\n```" + utils.disableCodeBlocks(originalThreadMessage.body) + "```";
} }
return content; return content;
@ -183,7 +184,7 @@ const defaultFormatters = {
}, },
formatSystemToUserThreadMessage(threadMessage) { formatSystemToUserThreadMessage(threadMessage) {
let result = `**[BOT TO USER]** ${threadMessage.body}`; let result = `**[SYSTEM TO USER]** ${threadMessage.body}`;
for (const link of threadMessage.attachments) { for (const link of threadMessage.attachments) {
result += `\n\n${link}`; result += `\n\n${link}`;
@ -265,6 +266,15 @@ const defaultFormatters = {
line += ` [CHAT] [${message.user_name}] ${message.body}`; line += ` [CHAT] [${message.user_name}] ${message.body}`;
} else if (message.message_type === THREAD_MESSAGE_TYPE.COMMAND) { } else if (message.message_type === THREAD_MESSAGE_TYPE.COMMAND) {
line += ` [COMMAND] [${message.user_name}] ${message.body}`; line += ` [COMMAND] [${message.user_name}] ${message.body}`;
} else if (message.message_type === THREAD_MESSAGE_TYPE.REPLY_EDITED) {
const originalThreadMessage = message.getMetadataValue("originalThreadMessage");
line += ` [REPLY EDITED] ${originalThreadMessage.user_name} edited reply ${originalThreadMessage.message_number}:`;
line += `\n\nBefore:\n${originalThreadMessage.body}`;
line += `\n\nAfter:\n${message.getMetadataValue("newBody")}`;
} else if (message.message_type === THREAD_MESSAGE_TYPE.REPLY_DELETED) {
const originalThreadMessage = message.getMetadataValue("originalThreadMessage");
line += ` [REPLY DELETED] ${originalThreadMessage.user_name} deleted reply ${originalThreadMessage.message_number}:`;
line += `\n\n${originalThreadMessage.body}`;
} else { } else {
line += ` [${message.user_name}] ${message.body}`; line += ` [${message.user_name}] ${message.body}`;
} }