简体   繁体   中英

Exporting modules in Nodejs

I need to export the variable adminOptionsRow so I can access it in my InteractionCreate file and disable the buttons. However, I'm unsure of how to do it, since it's inside async execute , and it has to stay inside of the block so I'm able to access the interaction options.

This is the file I want to export adminOptionsRow from:

const { Constants, CommandInteraction, MessageEmbed, MessageActionRow, MessageButton, Message } = require("discord.js");
const config = require("../config.json");
const bugreportSchema = require("../Schemas/bugreport-schema");
const { sendError } = require("../Utils/common.util");

module.exports = {
    name: "bug",
    description: "Report a bug you found in the bot",
    options: [
        {
            name: "bug",
            description: "Please describe the bug as well as the steps needed to recreate it.",
            type: Constants.ApplicationCommandOptionTypes.STRING,
            required: true,
        },
    ],

    /**
     *
     * @param {CommandInteraction} interaction
     */
    async execute(interaction, client) {

        const record = await bugreportSchema.create({
            time: Date.now(),
            userId: interaction.member.user.id,
            content: interaction.options.getString("bug"),
            status: "sent",
        });

        let replyEmbed = new MessageEmbed()
        .setColor(config.neutral_color)
        .setTitle("Thank you!")
        .setDescription(`Thanks for helping improve AC! We're really grateful for your support. Your report has been submitted and will be reviewed by Bananos as soon as possible. This is your bug report:\n\`\`${interaction.options.getString("bug")}\`\``)
        .setFooter({iconURL: 'https://cdn.discordapp.com/avatars/602150578935562250/d7d011fd7adf6704bf1ddf2924380c99.png?size=128', text: "Coded by Bananos #1873" });

        let dmEmbed = new MessageEmbed()
        .setColor(config.neutral_color)
        .setTitle(`Submitted report #${record.id}`)
        .setDescription(`Your bug report has been successfully submitted and will be reviewed by Bananos as soon as possible. You will be notified of the outcome. This is your bug report:\n\`\`${interaction.options.getString("bug")}\`\``)
        .setFooter({iconURL: 'https://cdn.discordapp.com/avatars/602150578935562250/d7d011fd7adf6704bf1ddf2924380c99.png?size=128', text: "Coded by Bananos #1873" });

        let reportEmbed = new MessageEmbed()
        .setColor(config.neutral_color)
        .setTitle(`Bug report #${record.id}`)
        .addFields(
            {name: "Report ID", value: `${record.id}`, inline: true},
            {name: "User", value: `${interaction.member.user.tag} (${interaction.member.user.id})`, inline: true},
            {name: "Time of report", value: `<t:${Date.now()}:d> at <t:${Date.now()}:T>`, inline: true},
            {name: "Content of report", value: `${interaction.options.getString("bug")}`, inline: false},
        )

        let adminOptionsRow = new MessageActionRow().addComponents(
            new MessageButton()
            .setCustomId("reviewingBugReport")
            .setLabel("Reviewing")
            .setStyle("PRIMARY"),

            new MessageButton()
            .setCustomId("acceptBugReport")
            .setLabel("Accept")
            .setStyle("SUCCESS"),
            
            new MessageButton()
            .setCustomId("declineBugReport")
            .setLabel("Decline")
            .setStyle("DANGER")
        )

        //Send all the notifications
        try {
            interaction.reply({embeds: [replyEmbed]});
            client.users.cache.get(interaction.member.user.id).send({ embeds: [dmEmbed] });
            client.channels.cache.get("956680298563780609").send({ embeds: [reportEmbed], components: [adminOptionsRow]});
        } catch(error) {
            sendError(error)
        }
    },
    
};

This is the file I want to access adminOptionsRow in:

const { Client, CommandInteraction, MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const config = require("../../config.json");
const {messages} = require("../../Commands/bug.cmd");

module.exports = {
    name: "interactionCreate",
    /**
     * 
     * @param {CommandInteraction} interaction 
     * @param {Client} client 
     */
    async execute(interaction, client) {
        if(interaction.isCommand()) {
            const command = client.commands.get(interaction.commandName);

            try {
                command.execute(interaction, client);
            } catch(error) {
                console.log(error)
                const commandExecutionErrorEmbed = new MessageEmbed()
                .setTitle("Error")
                .setColor(config.error_color)
                .setDescription("There was an error while executing this command.")

                client.guilds.cache.get(config.guild_id).channels.cache.get(config.error_log_channel_id).send({
                    embeds: {
                        commandExecutionErrorEmbed
                    }
                });
            }
        }
            if(interaction.isButton()) {
            if(interaction.customId === "reviewingBugReport") {
                messages().adminOptionsRow.components[0].setDisabled(true)
                interaction.update({components: [messages().adminOptionsRow]})
            }
        }
    }
}
const { Constants, CommandInteraction, MessageEmbed, MessageActionRow, MessageButton, Message } = require("discord.js");
const config = require("../config.json");
const bugreportSchema = require("../Schemas/bugreport-schema");
const { sendError } = require("../Utils/common.util");

let adminOptionsRow = new MessageActionRow().addComponents(
            new MessageButton()
            .setCustomId("reviewingBugReport")
            .setLabel("Reviewing")
            .setStyle("PRIMARY"),

            new MessageButton()
            .setCustomId("acceptBugReport")
            .setLabel("Accept")
            .setStyle("SUCCESS"),
            
            new MessageButton()
            .setCustomId("declineBugReport")
            .setLabel("Decline")
            .setStyle("DANGER")
        )

module.exports = {
    adminOptionsRow,
    name: "bug",
    description: "Report a bug you found in the bot",
    options: [
        {
            name: "bug",
            description: "Please describe the bug as well as the steps needed to recreate it.",
            type: Constants.ApplicationCommandOptionTypes.STRING,
            required: true,
        },
    ],

    /**
     *
     * @param {CommandInteraction} interaction
     */
    async execute(interaction, client) {

        const record = await bugreportSchema.create({
            time: Date.now(),
            userId: interaction.member.user.id,
            content: interaction.options.getString("bug"),
            status: "sent",
        });

        let replyEmbed = new MessageEmbed()
        .setColor(config.neutral_color)
        .setTitle("Thank you!")
        .setDescription(`Thanks for helping improve AC! We're really grateful for your support. Your report has been submitted and will be reviewed by Bananos as soon as possible. This is your bug report:\n\`\`${interaction.options.getString("bug")}\`\``)
        .setFooter({iconURL: 'https://cdn.discordapp.com/avatars/602150578935562250/d7d011fd7adf6704bf1ddf2924380c99.png?size=128', text: "Coded by Bananos #1873" });

        let dmEmbed = new MessageEmbed()
        .setColor(config.neutral_color)
        .setTitle(`Submitted report #${record.id}`)
        .setDescription(`Your bug report has been successfully submitted and will be reviewed by Bananos as soon as possible. You will be notified of the outcome. This is your bug report:\n\`\`${interaction.options.getString("bug")}\`\``)
        .setFooter({iconURL: 'https://cdn.discordapp.com/avatars/602150578935562250/d7d011fd7adf6704bf1ddf2924380c99.png?size=128', text: "Coded by Bananos #1873" });

        let reportEmbed = new MessageEmbed()
        .setColor(config.neutral_color)
        .setTitle(`Bug report #${record.id}`)
        .addFields(
            {name: "Report ID", value: `${record.id}`, inline: true},
            {name: "User", value: `${interaction.member.user.tag} (${interaction.member.user.id})`, inline: true},
            {name: "Time of report", value: `<t:${Date.now()}:d> at <t:${Date.now()}:T>`, inline: true},
            {name: "Content of report", value: `${interaction.options.getString("bug")}`, inline: false},
        )

        //Send all the notifications
        try {
            interaction.reply({embeds: [replyEmbed]});
            client.users.cache.get(interaction.member.user.id).send({ embeds: [dmEmbed] });
            client.channels.cache.get("956680298563780609").send({ embeds: [reportEmbed], components: [adminOptionsRow]});
        } catch(error) {
            sendError(error)
        }
    },
    
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM