const { EmbedBuilder, MessageFlags } = require("discord.js"); const { client, mClient } = require("../../../index"); module.exports = { name: "quotesadd", description: "Adds a quote to the database by replying to a message.", aliases: ["qadd", "qa"], async execute(message) { // Ensure the command is used as a reply. if (!message.reference?.messageId) { return message.reply({ content: "Please reply to the message you want to quote.", flags: MessageFlags.Ephemeral }); } const { messageId, channelId, guildId } = message.reference; let quoteMessage; let quoteChannel; let quoteGuild; try { quoteGuild = await client.guilds.fetch(guildId); quoteChannel = await quoteGuild.channels.fetch(channelId); quoteMessage = await quoteChannel.messages.fetch(messageId); } catch (err) { console.error(err); return message.reply({ content: "Unable to fetch the quoted message.", flags: MessageFlags.Ephemeral }); } const msgData = { content: quoteMessage.content, author: quoteMessage.author, hasEmbed: quoteMessage.embeds.length > 0, reference: quoteMessage.reference }; // Only populated if the quoted message is itself a reply. let refData = null; if (msgData.reference?.messageId) { try { const refGuild = await client.guilds.fetch(msgData.reference.guildId); const refChannel = await refGuild.channels.fetch(msgData.reference.channelId); const refMessage = await refChannel.messages.fetch(msgData.reference.messageId); refData = { content: refMessage.content, author: refMessage.author, hasEmbed: refMessage.embeds.length > 0 }; } catch (err) { // Referenced message couldn't be fetched (deleted, inaccessible, etc.) refData = null; } } const db = mClient.db(process.env.M_DB); const quotesColl = db.collection("items_quotes"); const existing = await quotesColl.findOne({ messageID: quoteMessage.id }); if (existing) { return message.reply({ content: "Quote already exists in the database.", flags: MessageFlags.Ephemeral }); } await quotesColl.findOneAndUpdate( { messageID: quoteMessage.id }, { $set: { messageID: quoteMessage.id, channelID: quoteChannel.id, guildID: quoteGuild.id, userID: msgData.author.id, count: 0 } }, { upsert: true } ); const messageLink = `https://discord.com/channels/${quoteGuild.id}/${quoteChannel.id}/${quoteMessage.id}`; let description = ""; // Only show the replied-to message if one actually exists. if (refData) { description += `> ${refData.author} `; if (refData.hasEmbed) { description += "*an embed / a command*"; } else if (refData.content) { description += refData.content; } else { description += "*no text*"; } description += "\n↳ "; } description += `${msgData.author} `; if (msgData.hasEmbed && !msgData.content) { description += "*an embed / a command*"; } else { description += msgData.content || "*no text*"; } description += `\n\n${messageLink}`; const footer = `${new Date(quoteMessage.createdTimestamp).toDateString()} #${quoteMessage.id}`; const embed = new EmbedBuilder() .setTitle("- Quote Added! -") .setURL(messageLink) .setThumbnail(msgData.author.displayAvatarURL()) .setDescription(description) .setFooter({ text: footer }); return message.reply({ embeds: [embed] }); } };