首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJs:用ES6系统写导出

NodeJs:用ES6系统写导出
EN

Stack Overflow用户
提问于 2022-12-04 00:25:59
回答 1查看 37关注 0票数 -4

我目前正在开发一个机器人,我用node.js编写

但是,我有一段代码:命令/server.js

代码语言:javascript
复制
const { SlashCommandBuilder } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("server")
    .setDescription("Provides information about the server."),
  async execute(interaction) {
    // interaction.guild is the object representing the Guild in which the command was run
    await interaction.reply(
      `This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.`
    );
  },
};

我在我的index.js里这样称呼它:

代码语言:javascript
复制
//for every command we have
for (const file of commandFiles) {
  //get the file path
  const filePath = path.join(commandsPath, file);
  //load our command_name.js
  const command = require(filePath);
  // Set a new item in the Collection with the key as the command name and the value as the exported module
  if ("data" in command && "execute" in command) {
    client.commands.set(command.data.name, command);
  } else {
    console.log(
      `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
    );
  }
}

当我将我的类型设置为模块时,在ES6语法中,什么是与server.js的等价的&&希望使用导入语法--所以我仍然得到数据并执行属性。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-04 00:29:05

下面是如何使用server.js导入语法和导出语法编写ES6文件的示例:

代码语言:javascript
复制
// Import the `SlashCommandBuilder` class from the `discord.js` module
import { SlashCommandBuilder } from 'discord.js';

// Create an object with the data and execute properties
const command = {
  // Create a new instance of the `SlashCommandBuilder` class and set its name and description properties
  data: new SlashCommandBuilder()
    .setName('server')
    .setDescription('Provides information about the server.'),

  // Define the `execute` method, which takes an `interaction` object as its argument
  async execute(interaction) {
    // Use the `interaction.guild` property to get information about the Guild in which the command was run
    await interaction.reply(
      `This server is ${interaction.guild.name} and has ${interaction.guild.memberCount} members.`
    );
  },
};

// Export the `command` object
export default command;

然后,在index.js文件中,您可以使用导入语法导入命令对象,并对其进行重构以获取数据并执行属性:

代码语言:javascript
复制
// Import the `command` object from the `server.js` file
import command from './commands/server';

// Destructure the `data` and `execute` properties from the `command` object
const { data, execute } = command;

// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
  client.commands.set(data.name, { data, execute });
} else {
  console.log(
    `[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
  );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74671946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档