0

I am new in discord.js v14.0.3 and trying to start a simple command but I got an error. I checked my BOT_TOKEN, BOT_CLIENT_ID, GUILD_ID is valid and I think the problem is not related with those token, how can I fix it?

Error Message

> node register.js

/register.js:11
(async () => {
^

TypeError: (intermediate value).setToken(...) is not a function
    at Object.<anonymous> ....

My code

require("dotenv").config()

const { REST } = require('@discordjs/rest')
const { Routes } = require('discord.js')

const commands = [
  {
    name: 'ping',
    description: 'Replies with Pong!',
  },
];

const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN)

(async () => {
  try {
    console.log('Started refreshing application (/) commands.')

    await rest.put(Routes.applicationGuildCommands(process.env.BOT_CLIENT_ID, process.env.DC_GUILD_ID), { body: commands });

    console.log('Successfully reloaded application (/) commands.')
  } catch (error) {
    console.error(error)
  }
})()

package.json

{
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js",
    "build": "node register.js"
  },
  "dependencies": {
    "discord.js": "^14.0.3",
    "dotenv": "^16.0.1",
    "ytdl-core": "^4.11.0"
  }
}
Yuu
  • 159
  • 1
  • 10
  • 1
    Does this answer your question? [Uncaught TypeError: (intermediate value)(...) is not a function](https://stackoverflow.com/questions/42036349/uncaught-typeerror-intermediate-value-is-not-a-function) – Zsolt Meszaros Aug 07 '22 at 07:26

1 Answers1

3

Two things:

  1. Looks like your package.json is missing @discordjs/rest. Install it with npm install @discordjs/rest or whatever your package manager is. Since you have no error on the require it may be is installed as phantom dependency.
  2. You simply missing a semicolon in the line const rest = new REST({ version: '10' }).setToken(process.env.BOT_TOKEN) therfore the whole block with ...setToken...(async () is considered to be one(!) expression.
Enak
  • 546
  • 3
  • 15
  • for point1, try to install the module and still getting error – Yuu Aug 07 '22 at 08:06
  • and for point2, yup it works after I insert `;` :) thanks! – Yuu Aug 07 '22 at 08:06
  • 1
    You were using a package in your app which was not present in `package.json`. Due to the nature of package resolution and flattening of `node_modules` this can work because of dependencies of your dependencies. Like A uses B and B uses C. When A uses C it just works as long as B has C as dependency. Imagine an update for B where C is dropped => A get's errors by just updating packages which seems to be totally unrelated. This is a phantom dependency or phantom child and bad practice. Since you had no error on the require, I was sure that it will not solve your problem but should be done. – Enak Aug 07 '22 at 15:33