Add formatters for system messages
This has backwards-compatibility-breaking changes to the signature of thread.postSystemMessage() and thread.sendSystemMessageToUser().cshd
parent
c9e0dbf040
commit
f9671c385d
|
@ -359,8 +359,7 @@ class Thread {
|
||||||
// Interrupt scheduled closing, if in progress
|
// Interrupt scheduled closing, if in progress
|
||||||
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.`,
|
|
||||||
allowedMentions: {
|
allowedMentions: {
|
||||||
users: [this.scheduled_close_id],
|
users: [this.scheduled_close_id],
|
||||||
},
|
},
|
||||||
|
@ -372,8 +371,7 @@ class Thread {
|
||||||
const mentionsStr = ids.map(id => `<@!${id}> `).join("");
|
const mentionsStr = ids.map(id => `<@!${id}> `).join("");
|
||||||
|
|
||||||
await this.deleteAlerts();
|
await this.deleteAlerts();
|
||||||
await this.postSystemMessage({
|
await this.postSystemMessage(`${mentionsStr}New message from ${this.user_name}`, {
|
||||||
content: `${mentionsStr}New message from ${this.user_name}`,
|
|
||||||
allowedMentions: {
|
allowedMentions: {
|
||||||
users: ids,
|
users: ids,
|
||||||
},
|
},
|
||||||
|
@ -389,47 +387,64 @@ class Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Eris.MessageContent} content
|
* @param {string} text
|
||||||
* @param {Eris.MessageFile} file
|
|
||||||
* @param {object} opts
|
* @param {object} opts
|
||||||
* @param {boolean} opts.saveToLog
|
* @param {object} [allowedMentions] Allowed mentions for the thread channel message
|
||||||
* @param {string} opts.logBody
|
* @param {boolean} [allowedMentions.everyone]
|
||||||
|
* @param {boolean|string[]} [allowedMentions.roles]
|
||||||
|
* @param {boolean|string[]} [allowedMentions.users]
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async postSystemMessage(content, file = null, opts = {}) {
|
async postSystemMessage(text, opts = {}) {
|
||||||
const msg = await this._postToThreadChannel(content, file);
|
const threadMessage = new ThreadMessage({
|
||||||
if (msg && opts.saveToLog !== false) {
|
message_type: THREAD_MESSAGE_TYPE.SYSTEM,
|
||||||
await this._addThreadMessageToDB({
|
user_id: null,
|
||||||
message_type: THREAD_MESSAGE_TYPE.SYSTEM,
|
user_name: "",
|
||||||
user_id: null,
|
body: text,
|
||||||
user_name: "",
|
is_anonymous: 0,
|
||||||
body: msg.content || "<empty message>",
|
});
|
||||||
is_anonymous: 0,
|
|
||||||
inbox_message_id: msg.id,
|
const content = await formatters.formatSystemThreadMessage(threadMessage);
|
||||||
});
|
|
||||||
}
|
const finalContent = typeof content === "string" ? { content } : content;
|
||||||
|
finalContent.allowedMentions = opts.allowedMentions;
|
||||||
|
const msg = await this._postToThreadChannel(finalContent);
|
||||||
|
|
||||||
|
threadMessage.inbox_message_id = msg.id;
|
||||||
|
await this._addThreadMessageToDB(threadMessage.getSQLProps());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Eris.MessageContent} content
|
* @param {string} text
|
||||||
* @param {Eris.MessageFile} file
|
|
||||||
* @param {object} opts
|
* @param {object} opts
|
||||||
* @param {boolean} opts.saveToLog
|
* @param {object} [allowedMentions] Allowed mentions for the thread channel message
|
||||||
* @param {string} opts.logBody
|
* @param {boolean} [allowedMentions.everyone]
|
||||||
|
* @param {boolean|string[]} [allowedMentions.roles]
|
||||||
|
* @param {boolean|string[]} [allowedMentions.users]
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async sendSystemMessageToUser(content, file = null, opts = {}) {
|
async sendSystemMessageToUser(text, opts = {}) {
|
||||||
const msg = await this._sendDMToUser(content, file);
|
const threadMessage = new ThreadMessage({
|
||||||
if (opts.saveToLog !== false) {
|
message_type: THREAD_MESSAGE_TYPE.SYSTEM_TO_USER,
|
||||||
await this._addThreadMessageToDB({
|
user_id: null,
|
||||||
message_type: THREAD_MESSAGE_TYPE.SYSTEM_TO_USER,
|
user_name: "",
|
||||||
user_id: null,
|
body: text,
|
||||||
user_name: "",
|
is_anonymous: 0,
|
||||||
body: msg.content || "<empty message>",
|
});
|
||||||
is_anonymous: 0,
|
|
||||||
dm_message_id: msg.id,
|
const dmContent = await formatters.formatSystemToUserDM(threadMessage);
|
||||||
});
|
const dmMsg = await this._sendDMToUser(dmContent);
|
||||||
}
|
|
||||||
|
const inboxContent = await formatters.formatSystemToUserThreadMessage(threadMessage);
|
||||||
|
const finalInboxContent = typeof inboxContent === "string" ? { content: inboxContent } : inboxContent;
|
||||||
|
finalInboxContent.allowedMentions = opts.allowedMentions;
|
||||||
|
const inboxMsg = await this._postToThreadChannel(inboxContent);
|
||||||
|
|
||||||
|
threadMessage.inbox_message_id = inboxMsg.id;
|
||||||
|
threadMessage.dm_channel_id = dmMsg.channel.id;
|
||||||
|
threadMessage.dm_message_id = dmMsg.id;
|
||||||
|
|
||||||
|
await this._addThreadMessageToDB(threadMessage.getSQLProps());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,6 +43,27 @@ const moment = require("moment");
|
||||||
* @return {Eris.MessageContent} Message content to post in the thread channel
|
* @return {Eris.MessageContent} Message content to post in the thread channel
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to format a system message in a thread channel
|
||||||
|
* @callback FormatSystemThreadMessage
|
||||||
|
* @param {ThreadMessage} threadMessage
|
||||||
|
* @return {Eris.MessageContent} Message content to post in the thread channel
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to format a system message sent to the user in a thread channel
|
||||||
|
* @callback FormatSystemToUserThreadMessage
|
||||||
|
* @param {ThreadMessage} threadMessage
|
||||||
|
* @return {Eris.MessageContent} Message content to post in the thread channel
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to format the DM that is sent to the user when the bot sends a system message to the user
|
||||||
|
* @callback FormatSystemToUserDM
|
||||||
|
* @param {ThreadMessage} threadMessage
|
||||||
|
* @return {Eris.MessageContent} Message content to send as a DM
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} FormatLogOptions
|
* @typedef {Object} FormatLogOptions
|
||||||
* @property {Boolean?} simple
|
* @property {Boolean?} simple
|
||||||
|
@ -71,6 +92,9 @@ const moment = require("moment");
|
||||||
* @property {FormatUserReplyThreadMessage} formatUserReplyThreadMessage
|
* @property {FormatUserReplyThreadMessage} formatUserReplyThreadMessage
|
||||||
* @property {FormatStaffReplyEditNotificationThreadMessage} formatStaffReplyEditNotificationThreadMessage
|
* @property {FormatStaffReplyEditNotificationThreadMessage} formatStaffReplyEditNotificationThreadMessage
|
||||||
* @property {FormatStaffReplyDeletionNotificationThreadMessage} formatStaffReplyDeletionNotificationThreadMessage
|
* @property {FormatStaffReplyDeletionNotificationThreadMessage} formatStaffReplyDeletionNotificationThreadMessage
|
||||||
|
* @property {FormatSystemThreadMessage} formatSystemThreadMessage
|
||||||
|
* @property {FormatSystemToUserThreadMessage} formatSystemToUserThreadMessage
|
||||||
|
* @property {FormatSystemToUserDM} formatSystemToUserDM
|
||||||
* @property {FormatLog} formatLog
|
* @property {FormatLog} formatLog
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -148,6 +172,36 @@ const defaultFormatters = {
|
||||||
return content;
|
return content;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
formatSystemThreadMessage(threadMessage) {
|
||||||
|
let result = threadMessage.body;
|
||||||
|
|
||||||
|
for (const link of threadMessage.attachments) {
|
||||||
|
result += `\n\n${link}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
formatSystemToUserThreadMessage(threadMessage) {
|
||||||
|
let result = `**[BOT TO USER]** ${threadMessage.body}`;
|
||||||
|
|
||||||
|
for (const link of threadMessage.attachments) {
|
||||||
|
result += `\n\n${link}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
formatSystemToUserDM(threadMessage) {
|
||||||
|
let result = threadMessage.body;
|
||||||
|
|
||||||
|
for (const link of threadMessage.attachments) {
|
||||||
|
result += `\n\n${link}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
formatLog(thread, threadMessages, opts = {}) {
|
formatLog(thread, threadMessages, opts = {}) {
|
||||||
if (opts.simple) {
|
if (opts.simple) {
|
||||||
threadMessages = threadMessages.filter(message => {
|
threadMessages = threadMessages.filter(message => {
|
||||||
|
@ -242,6 +296,9 @@ const formatters = { ...defaultFormatters };
|
||||||
* @property {function(FormatUserReplyThreadMessage): void} setUserReplyThreadMessageFormatter
|
* @property {function(FormatUserReplyThreadMessage): void} setUserReplyThreadMessageFormatter
|
||||||
* @property {function(FormatStaffReplyEditNotificationThreadMessage): void} setStaffReplyEditNotificationThreadMessageFormatter
|
* @property {function(FormatStaffReplyEditNotificationThreadMessage): void} setStaffReplyEditNotificationThreadMessageFormatter
|
||||||
* @property {function(FormatStaffReplyDeletionNotificationThreadMessage): void} setStaffReplyDeletionNotificationThreadMessageFormatter
|
* @property {function(FormatStaffReplyDeletionNotificationThreadMessage): void} setStaffReplyDeletionNotificationThreadMessageFormatter
|
||||||
|
* @property {function(FormatSystemThreadMessage): void} setSystemThreadMessageFormatter
|
||||||
|
* @property {function(FormatSystemToUserThreadMessage): void} setSystemToUserThreadMessageFormatter
|
||||||
|
* @property {function(FormatSystemToUserDM): void} setSystemToUserDMFormatter
|
||||||
* @property {function(FormatLog): void} setLogFormatter
|
* @property {function(FormatLog): void} setLogFormatter
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -275,6 +332,18 @@ module.exports = {
|
||||||
formatters.formatStaffReplyDeletionNotificationThreadMessage = fn;
|
formatters.formatStaffReplyDeletionNotificationThreadMessage = fn;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
setSystemThreadMessageFormatter(fn) {
|
||||||
|
formatters.formatSystemThreadMessage = fn;
|
||||||
|
},
|
||||||
|
|
||||||
|
setSystemToUserThreadMessageFormatter(fn) {
|
||||||
|
formatters.formatSystemToUserThreadMessage = fn;
|
||||||
|
},
|
||||||
|
|
||||||
|
setSystemToUserDMFormatter(fn) {
|
||||||
|
formatters.formatSystemToUserDM = fn;
|
||||||
|
},
|
||||||
|
|
||||||
setLogFormatter(fn) {
|
setLogFormatter(fn) {
|
||||||
formatters.formatLog = fn;
|
formatters.formatLog = fn;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue