【问题标题】:Using client in other files than main file在主文件以外的其他文件中使用客户端
【发布时间】:2022-10-13 01:06:46
【问题描述】:

我是一个非常新的 discord.js 编码器。我一直在尝试在另一个文件\modules\verifyForm.js 中使用此代码,并且它总是出现client is not defined。我已经环顾四周,它总是提出一些过于过时的东西,一些非常先进的东西或者一些不起作用的东西。

我在\index.js 中有一个单独的主文件,这段代码在其中工作。客户端是在该文件或课程中定义的,但是因为您不能有两个实例,所以我无法重新定义它。我只是愚蠢还是有更好的方法来做到这一点。收集或使用导出有帮助吗?

const { ActionRowBuilder, Events, InteractionType, ModalBuilder, TextInputBuilder, TextInputStyle, } = require('discord.js');

client.on(Events.InteractionCreate, async (interaction) => {
    if (interaction.isButton()) {
        if (interaction.customId === 'verification-button') {
        const modal = new ModalBuilder()
            .setCustomId('verification-modal')
            .setTitle('Verify yourself')
            .addComponents([
            new ActionRowBuilder().addComponents(
                new TextInputBuilder()
                .setCustomId('verification-input')
                .setLabel('Answer')
                .setStyle(TextInputStyle.Short)
                .setMinLength(0)
                .setMaxLength(512)
                .setPlaceholder('ABCDEF')
                .setRequired(true),
            ),
            ]);

        await interaction.showModal(modal);
        }
}

if (interaction.type === InteractionType.ModalSubmit) {
    if (interaction.customId === 'verification-modal') {
    const response =
        interaction.fields.getTextInputValue('verification-input');
    interaction.channel(`Yay, your answer is submitted: "${response}"`);
    }
}
});

我确实有一个单独的嵌入按钮。

如果您确实有办法绕过使用客户端,或者如果您有办法修复错误,谢谢。

【问题讨论】:

  • 将一个函数从 verifyForm 导出到索引中,然后将客户端作为参数传递给该函数并调用它

标签: javascript node.js discord.js


【解决方案1】:

使客户端成为全局变量,以便您可以在任何地方使用它,基本上是global.client = new Client()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 2021-04-09
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 2019-12-04
    • 2020-05-29
    相关资源
    最近更新 更多