pissbot-9000/labscore/utils/message.js
2024-12-26 23:17:46 +01:00

88 lines
No EOL
4.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { Permissions, MessageFlags } = require("detritus-client/lib/constants")
const { basecamp, formatErrorMessage } = require("../logging")
const { COLORS, MESSAGE_BLOCK_REASONS } = require("#constants")
const { icon, link } = require("./markdown")
module.exports.editOrReply = function(context, message, disableReference = false){
// Apply message_reference
if(!message.content && !message.embed && !message.embeds && !message.components && !message.files && typeof(message) == "object") message = {embeds: [message]};
else if(typeof(message) == "string") message = { content: message };
if(!message.message_reference && !disableReference) message.reference = true
// Disable mentions
if(!message.allowedMentions) message.allowedMentions = {parse: [], repliedUser: false}
let flags = 0;
// Special labsCore context clues for the command.
// Currently only used to identify incognito requests
// on user slash commands.
if(context._meta){
if(context._meta.isIncognito) flags = MessageFlags.EPHEMERAL
}
message.flags = flags;
// Handle responses for interaction context
if(context.editOrRespond){
if(context._meta?.replacementMessageId){
return context.editMessage(context._meta.replacementMessageId, message).catch((e)=>{
basecamp(formatErrorMessage(3, "SHARD_MESSAGE_ERROR", `\`[Shard ${context.client.shardId}]\` Command \`${context.command.name}\` failed to respond:\nGuild: \`${context.guild?.id}\`\nChannel: \`${context.channel?.id}\`\nUser: \`${context.user?.id}\`\`\`\`js\n${e}\`\`\``), message);
});
}
if(context._meta?.incognitoReason){
// TODO: make this an applyIncognitoNotice helper
if(message.content){
if(message.embeds && message.embeds.length <= 4){
message.embeds.unshift({
description: `${icon("flask_incognito")} This response has been made incognito due to ${MESSAGE_BLOCK_REASONS[context._meta.incognitoReason].message}.`,
color: COLORS.incognito
})
}
} else {
// Uses new subtext formatting to look more "native"
message.content = `-# ${icon("flask_mini")} This response has been made incognito due to ${MESSAGE_BLOCK_REASONS[context._meta.incognitoReason].message}${link("https://support.discord.com/hc/en-us/articles/" + MESSAGE_BLOCK_REASONS[context._meta.incognitoReason].support_article, "Learn More", "Support Article")}`
}
}
return context.editOrRespond(message).catch(async (e)=>{
const errorData = await e.response.json();
if(MESSAGE_BLOCK_REASONS[errorData.code]){
// Delete the public response
await context.deleteResponse();
message.flags = MessageFlags.EPHEMERAL
// Create a notice
if(message.content){
if(message.embeds && message.embeds.length <= 4){
message.embeds.unshift({
description: `${icon("flask_incognito")} This response has been made incognito due to ${MESSAGE_BLOCK_REASONS[errorData.code].message}.`,
color: COLORS.incognito
})
}
} else {
// Uses new subtext formatting to look more "native"
message.content = `-# ${icon("flask_mini")} This response has been made incognito due to ${MESSAGE_BLOCK_REASONS[errorData.code].message}${link("https://support.discord.com/hc/en-us/articles/" + MESSAGE_BLOCK_REASONS[errorData.code].support_article, "Learn More", "Support Article")}`
}
let replacementMessage = await context.createMessage(message);
if(!context._meta) context._meta = {}
context._meta.replacementMessageId = replacementMessage.id;
return replacementMessage;
}
basecamp(formatErrorMessage(3, "SHARD_MESSAGE_ERROR", `\`[Shard ${context.client.shardId}]\` Command \`${context.command.name}\` failed to respond: @ \`${Date.now()}\`\nGuild: \`${context.guild?.id}\`\nChannel: \`${context.channel?.id}\`\nUser: \`${context.user?.id}\`\`\`\`js\n${e}\`\`\``), message);
})
}
// Only respond if the command is still available and we have permissions to respond.
if(!context.message.deleted && context.channel.can(Permissions.SEND_MESSAGES)) return context.editOrReply(message).catch((e)=>{
console.log(e.status)
basecamp(formatErrorMessage(3, "SHARD_MESSAGE_ERROR", `\`[Shard ${context.client.shardId}]\` Command \`${context.message?.content}\` failed to respond:\nGuild: \`${context.guild?.id}\`\nChannel: \`${context.channel?.id}\`\nUser: \`${context.user?.id}\`\`\`\`js\n${e}\`\`\``), message);
})
}