client.once("ready", () =>{
console.log("Bot is online");
const CLIENT_ID = client.user.id;
const rest = new REST({
version: "9"
}).setToken(process.env.TOKEN);
(async () => {
try {
if (process.env.ENV === "production") {
await rest.put(Routes.applicationCommands(CLIENT_ID), {
body: commands
});
console.log("sucessfully resgistered (/) commamds globally");
} else {
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {
body: commands
});
console.log("sucessfully resgistered (/) commands locally");
}
} catch (err) {
if (err) console.log(err);
}
})();
});
client.on("interactionCreate", async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName)
if (!command) return;
try{
// await command.execute(interaction);
throw new Error("Command not implemented.");
} catch(err) {
if (err) console.error(err);
await interaction.reply({
content: "An error occurred while executing that command.",
emphemeral:true
})
}
});
client.login(process.env.TOKEN);
错误:
ReferenceError: Routes is not defined
at C:\DiscordBot\index.js:43:28
at Client.<anonymous> (C:\DiscordBot\index.js:51:7)
at Object.onceWrapper (node:events:510:26)
at Client.emit (node:events:390:28)
at WebSocketManager.triggerClientReady (C:\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:17)
at WebSocketManager.checkShardsReady (C:\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:367:10)
at WebSocketShard.<anonymous> (C:\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:189:14)
at WebSocketShard.emit (node:events:390:28)
at WebSocketShard.checkReady (C:\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12)
at WebSocketShard.onPacket (C:\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16)
回答1
将此添加到代码的顶部并在需要时安装它。
const {
Routes,
} = require('discord-api-types/v9');
您在此处指定了 Routes 但已定义:
await rest.put(Routes.applicationCommands(CLIENT_ID), {
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), {