我目前正在开发一个机器人,我用node.js编写
但是,我有一段代码:命令/server.js
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里这样称呼它:
//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的等价的&&希望使用导入语法--所以我仍然得到数据并执行属性。
发布于 2022-12-04 00:29:05
下面是如何使用server.js导入语法和导出语法编写ES6文件的示例:
// 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文件中,您可以使用导入语法导入命令对象,并对其进行重构以获取数据并执行属性:
// 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.`
);
}https://stackoverflow.com/questions/74671946
复制相似问题