【发布时间】:2022-01-07 20:31:30
【问题描述】:
因此,我正在尝试向我的不和谐机器人添加交互,该机器人已经拥有命令和事件处理程序。我遵循discord.js guide 并添加命令效果很好。但现在我正在尝试做command handling 部分,但它给出了错误:
TypeError: command.execute is not a function
我在 index.js 中的代码:
console.clear()
const fs = require('fs')
const { Collection } = require('discord.js');
const Client = require("./Structures/Client.js");
const config = require("./Data/config.json");
const client = new Client();
client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/iCommands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./iCommands/${file}`);
client.commands.set(command.data.name, command);
}
client.on("interactionCreate", async interaction => {
if(!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if(!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.log(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.start(config.token);
我在 deploy-commands.js 中的代码:
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./Data/config.json');
const commands = [];
const commandFiles = fs.readdirSync('./src/iCommands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./iCommands/${file}`);
commands.push(command.data.toJSON());
}
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log('Succesfully registerd application commands.'))
.catch(console.error);
然后对于命令本身,只是指南给出的标准代码。我也在一段空白代码中尝试过,它运行良好
【问题讨论】:
标签: discord discord.js