简体   繁体   中英

Discord.js v13 (intermediate value).setToken(...) is not a function

Im new to Discod.js v13. I get this error

    (async () => {
    ^

TypeError: (intermediate value) is not a function

This is the code i think its the problem

  const rest = new REST({
        version: "9"
    }).setToken("MyToken")
    (async () => {

If you need the full code:

const { Client, Intents, Collection } = require("discord.js")
const fs = require("fs")
const {REST} = require("@discordjs/rest");
const {Routes} = require("discord-api-types/v9");
const client = new Client({
    intents:[
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES
    ]
})
const BotName = "TestBot"

const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));

const commands = []

client.commands = new Collection()

for(const file of commandFiles){
    const command = require("./commands/"+file)
    commands.push(command.data.toJSON());
    client.commands.set(command.data.name, command)
}
client.once("ready", applicationId =>{
    console.log(`${BotName} is now online`)

    const CLIENT_ID = client.user.id

    const rest = new REST({
        version: "9"
    }).setToken("MyToken")
    (async () => {
        try{
            if(process.env.ENV === "production"){
               await rest.put(Routes.applicationCommands(CLIENT_ID), {
                   body: commands
               })
                console.log("Success command register globally")
            }else{
                await rest.put(Routes.applicationGuildCommands(CLIENT_ID, process.env.GUILD_ID), {
                    body: commands
                })
                console.log("Success command register locally")
            }
        }catch(err){
         console.log(err)
        }
    })();
})

client.on("interaction", async interaction =>{
    if(!interaction.isCommand()) return

    const command = client.commands.get(interaction.commandName)

    if(!command) return
    try{
         await command.execute(interaction);
    }catch(err){
        console.log(err)
        await interaction.reply({
            content: "ERROR" + err,
            ephemeral:true
        })
    }
})
client.login("MyToken")

As said in my comment, this is a ASI (automatic semicolon insertion) related error. I checked it, and after putting a semicolon after }).setToken("MyToken"); it worked fine.

Always use semicolons to avoid these mistakes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM