Home > OS >  ERR_INVALID_ARG_TYPE on Discord.js
ERR_INVALID_ARG_TYPE on Discord.js

Time:11-29

So I'm making a bot in Discord.js and my output is an invalid argument type. Here is my code.

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`)
})

client.on(() => {
  if (msg.content === "!test") {
    msg.reply("Hello world!");
  }
  if (msg.content === "!purgec") {
  message.guild.channels.forEach(channel => channel.delete())
  msg.reply("Deleting all channels...");
  }
})

client.login('token')

My output in the console ends up being:

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received undefined
←[90m    at checkListener (node:events:128:3)←[39m
←[90m    at _addListener (node:events:423:3)←[39m
←[90m    at Client.addListener (node:events:487:10)←[39m
    at Object.<anonymous> (C:\Users\zen\Desktop\bot\main.js:8:8)
←[90m    at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m    at node:internal/main/run_main_module:17:47←[39m {
  code: ←[32m'ERR_INVALID_ARG_TYPE'←[39m
}

Can anyone help me?

CodePudding user response:

You are not listening to the event properly. First, you put the event name, then the callback. Do this instead:

client.on("messageCreate", (msg) => {
  if (msg.content === "!test") {
    msg.reply("Hello world!");
  }
  if (msg.content === "!purgec") {
    message.guild.channels.forEach(channel => channel.delete())
    msg.reply("Deleting all channels...");
  }
})
  • Related