【问题标题】:Get variables from another file node js从另一个文件节点js获取变量
【发布时间】:2022-10-14 03:26:35
【问题描述】:

我正在尝试使用 Discord.js 制作一个 Discord 机器人,并且在文档中的指南中说要为每个命令创建一个单独的文件。

我目前遇到的问题是,当我运行命令部署时,文件中的所有数据都不可用。我已经尝试从指南中复制所有内容。

这是我的代码和输出:

ping.js

const { SlashCommandBuilder } = require('discord.js');

const data = new SlashCommandBuilder()
    .setName('ping')
    .setDescription('Replies with pong!')

部署命令.js

const { REST, Routes } = require('discord.js');
require('dotenv').config();
const fs = require('node:fs');
const { ClientID, GuildID } = require("./config.json");

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

console.log("Hello World!")

// This is where the error happens
for (const file of commandFiles) {
    console.log(file)
    const command = require(`./commands/${file}`);
    console.log(command); // I dont know why this is empty
    console.log(command.data); // command.data should not be undefined
    commands.push(command.data.toJSON());
}

const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);

(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        const data = await rest.put(
            Routes.applicationGuildCommands(ClientID, GuildID),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        console.error(error);
    }
})();

输出

Hello World!
echo.js
{}
undefined
C:\Users\danho\Coding\node\DiscordBot\deploy-commands.js:16
        commands.push(command.data.toJSON());
                                   ^

TypeError: Cannot read properties of undefined (reading 'toJSON')
    at Object.<anonymous> (C:\Users\danho\Coding\node\DiscordBot\deploy-commands.js:16:29)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

正如我所说,这几乎是从他们的官方示例here 中获取的代码的精确副本

我不知道是什么导致了这个问题,所以任何提示/帮助将不胜感激!

【问题讨论】:

  • ${file} 的内容是什么?
  • @KarmaBlackshaw 那将是 ping.js 的内容
  • 你能做吗module.exports = new SlashCommandBuilder()
  • 我认为问题在于ping.js 文件中没有任何内容被导出。您所做的只是在其中声明一个变量。首先,您很可能希望将 ping.js 文件中的代码从 const data = new SlashCommandBuilder() 更改为 module.exports = new SlashCommandBuilder()

标签: node.js discord.js


【解决方案1】:

ping.js 中,使用:

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('ping')
    .setDescription('Replies with pong!');
};

【讨论】:

    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多