Discord Bot命令执行报错:TypeError: command.execute is not a function
Discord Bot 命令执行报错:TypeError: command.execute is not a function
机器人成功上线,但执行命令时抛出如下错误:
TypeError: command.execute is not a function
at Object.execute (C:\Users\lachl\Desktop\Coding\Discord Bot\src\events\client\interactionCreate.js:11:25)
at Client.(C:\Users\lachl\Desktop\Coding\Discord Bot\src\functions\handlers\handleEvents.js:15:63)
at Client.emit (node:events:513:28)
相关代码文件
handleEvents.js
const fs = require("fs"); module.exports = (client) => { client.handleEvents = async () => { const eventFolders = fs.readdirSync(`./src/events`); for (const folder of eventFolders) { const eventFiles = fs .readdirSync(`./src/events/${folder}`) .filter((file) => file.endsWith(".js")); switch (folder) { case 'client': for (const file of eventFiles) { const event = require(`../../events/${folder}/${file}`); if (event.once) client.once(event.name, (...args) => event.execute(...args, client)); else client.on(event.name, (...args) => event.execute(...args, client)); } break; default: break; } } }; };
interactionCreate.js
module.exports = { name: "interactionCreate", async execute(interaction, client) { if (interaction.isChatInputCommand()) { const { commands } = client; const { commandName } = interaction; const command = commands.get(commandName); if (!command) return; try { await command.execute(interaction, client); } catch (error) { console.error(error); await interaction.reply({ content: `Something went wrong while executing this command...`, ephemeral: true, }); } } }, };
handleCommands.js
const { REST } = require("@discordjs/rest"); const { Routes } = require("discord-api-types/v9"); const fs = require("fs"); module.exports = (client) => { client.handleCommands = async () => { const commandFolders = fs.readdirSync(`./src/commands`); for (const folder of commandFolders) { const commandFiles = fs .readdirSync(`./src/commands/${folder}`) .filter((file) => file.endsWith(".js")); const { commands, commandArray } = client; for (const file of commandFiles) { const command = require(`../../commands/${folder}/${file}`); commands.set(command.data.name, command); commandArray.push(command.data.toJSON()); console.log( `Command ${command.data.name} has passed through the handler` ); } } const clientId = "1050985494537834606"; const guildId = "1041260203112411197"; const rest = new REST({ version: "9" }).setToken(process.env.token); try { console.log("Started refreshing application [/] commands"); await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: client.commandArray, }); console.log("Successfully reloaded application [/] commands."); } catch (error) { console.error(error); } }; };
ping.js
const { SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('ping') .setDescription("Ping Command!"), async exectute(interaction, client) { const message = await interaction.deferReply({ fetchReply: true }); const newMessage = `API LATENCY: ${client.ws.ping}\nClient Ping: ${message.createdTimeStamp - interaction.createdTimeStamp}` await interaction.editReply({ content: newMessage }); } }
问题原因及修复方案
核心问题是拼写错误:ping.js中你把命令执行方法名execute拼成了exectute(多了一个t)。当interactionCreate.js尝试调用command.execute()时,找不到对应方法,因此抛出TypeError。
额外还有一个小问题:message.createdTimeStamp应为message.createdTimestamp(注意大写S),否则计算客户端延迟时会得到NaN,建议一并修正。
修正后的ping.js:
const { SlashCommandBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('ping') .setDescription("Ping Command!"), async execute(interaction, client) { const message = await interaction.deferReply({ fetchReply: true }); const newMessage = `API LATENCY: ${client.ws.ping}\nClient Ping: ${message.createdTimestamp - interaction.createdTimestamp}` await interaction.editReply({ content: newMessage }); } }
内容的提问来源于stack exchange,提问作者Panda Monium
相关产品推荐
相关产品推荐

