简体   繁体   English

在 Nodejs 中导出模块

[英]Exporting modules in Nodejs

I need to export the variable adminOptionsRow so I can access it in my InteractionCreate file and disable the buttons.我需要导出变量adminOptionsRow以便我可以在我的 InteractionCreate 文件中访问它并禁用按钮。 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.但是,我不确定该怎么做,因为它在async execute内部,而且它必须留在块内,这样我才能访问交互选项。

This is the file I want to export adminOptionsRow from:这是我要从中导出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");

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:这是我要访问adminOptionsRow的文件:

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)
        }
    },
    
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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