I’m working on implementing slash commands to my bot. For that I made a deploy-commands.js
which is located in ./src/deploy-commands.js
. My commands are located in ./src/commands/
. At the moment my only command is a simple ping.js
. With the following code I’m trying to read the commands in my commands folder and push them into an array.
require("dotenv").config()
const fs = require("fs")
const { REST } = require("@discordjs/rest")
const { Routes } = require('discord-api-types/v9')
const commands = []
const CommandFiles = fs.readdirSync("./src/commands").filter(file => file.endsWith(".js"))
CommandFiles.forEach(commandFile => {
const command = require(`.commands/${commandFile}`)
commands.push(command.data.toJSON())
})
const restClient = new REST({version: "9"}).setToken(process.env.DISCORD_BOT_TOKEN)
restClient.put(Routes.applicationGuildCommands(process.env.DISCORD_APPLICATION_ID, process.env.DISCORD_GUILD_ID),
{body: commands})
.then(() => console.log("Commands wurden geladen!"))
.catch(console.error)
Also I created a script in my package.json
to easily run the deploy-commands.js
file. Now when I run my script via npm run deployCommands
I get the following error:
Error: Cannot find module '.commands/ping.js'
Require stack:
- C:UsersnilsiDesktopDiscord Botsrcdeploy-commands.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at C:UsersnilsiDesktopDiscord Botsrcdeploy-commands.js:13:21
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:UsersnilsiDesktopDiscord Botsrcdeploy-commands.js:12:14)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\Users\nilsi\Desktop\Discord Bot\src\deploy-commands.js' ]
}