You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js报错TypeError: "listener"参数必须为函数,寻求解决方法

Fixing TypeError: "listener" argument must be a function in Your Discord Bot

Hey there, let's work through this frustrating error you're facing. That TypeError pops up when you try to register an event listener (like with Discord.js's client.on()) but pass something that's not a function as the listener argument—even if you removed the rich embed command, there's likely a leftover code issue or caching problem causing this.

Here's a step-by-step breakdown to track down and fix the problem:

1. Audit All Event Listener Registrations

This is the most common culprit. Double-check every place you use client.on() (or similar listener methods) in your code. The second argument must always be a function—if it's a variable that's undefined, a string, or an accidental value (like the result of a function call instead of the function itself), you'll get this error.

Example of Wrong Code:

// ❌ Bad: Passes undefined if myRichEmbedCommand isn't defined
client.on('messageCreate', myRichEmbedCommand);

// ❌ Worse: Immediately runs console.log, passes its return value (undefined)
client.on('messageCreate', console.log('Bot received a message'));

Example of Correct Code:

// ✅ Good: Uses an inline function
client.on('messageCreate', (message) => {
  // Your command logic here
});

// ✅ Good: Uses a pre-defined function
function handleMessage(message) {
  // Your command logic here
}
client.on('messageCreate', handleMessage);

2. Clear Nodemon's Cache (It Might Be Sticking to Old Code)

Nodemon is great for auto-reloading, but sometimes it doesn't fully refresh cached code—especially if you made quick edits to remove the rich embed command. Try these fixes:

  • Manually stop and restart Nodemon: Press Ctrl+C in your terminal, then re-run your start command (e.g., nodemon index.js).
  • Launch Nodemon with no cache: Use nodemon --no-cache index.js to bypass any stored cached code.
  • If all else fails, delete any .nodemon cache folders in your project directory (they're usually hidden, so you might need to show hidden files in Atom).

3. Hunt for Leftover Rich Embed Code

Even if you removed the main command, there might be fragments of code that are still causing issues:

  • Did you leave behind a reference to an embed-related function in an event listener?
  • Did you accidentally modify an existing listener when adding the embed command (e.g., overwriting a function with an undefined value)?
  • Check any utility functions you wrote for embeds—maybe one of them is being passed as a listener by mistake.

4. Use "Divide and Conquer" to Locate the Error

If you're still stuck, narrow down the problem by commenting out chunks of code:

  1. Start with a minimal working bot setup (only login and ready event):
    const { Client, GatewayIntentBits } = require('discord.js');
    const client = new Client({ 
      intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] 
    });
    
    client.on('ready', () => {
      console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.login('YOUR_BOT_TOKEN');
    
  2. If this runs without errors, gradually uncomment sections of your code (one at a time) and restart the bot after each change. The error will reappear when you uncomment the problematic code—this tells you exactly where the issue is.

5. Double-Check Function/Variable Spelling & Scope

Discord.js and JavaScript are case-sensitive, so a typo like messagecreate instead of messageCreate can break things. Also, make sure any functions you're using as listeners are defined in the correct scope (e.g., not inside another function where they can't be accessed by the listener).


内容的提问来源于stack exchange,提问作者Brage A. H.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:08:14