Home > Blockchain >  TypeError: async is not a function
TypeError: async is not a function

Time:03-01

I'm doing a slash command handler using discord.js, and I'm getting the "not a function" error. I saw other questions here and some videos on YouTube but I still couldn't solve the problem

This is the code

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

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

    if (!command) return
}))

And this is the error

client.on('interactionCreate', async (interaction => {
                               ^

TypeError: async is not a function
    at Object.<anonymous> (C:\Users\...\Desktop\...\...\...\index.js:60:32)
    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)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

I have tried: npm i async and var async = require('async')

Before I created the variable the error was that async was not defined but now it shows this new error

CodePudding user response:

You missed a ")"

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

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

  if (!command) return;
});

CodePudding user response:

You are calling client.on('interactionCreate', f ) where f is represented as:

async (interaction => {
    if (!interaction.isCommand()) return

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

    if (!command) return
})

JavaScript is interpreting this as async( ... ).

You can either remove the parentheses surrounding your function, or move the closing parentheses to the end of the arguments of your arrow function.

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

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

    if (!command) return
})

OR

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

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

    if (!command) return
})
  • Related