【问题标题】:Discord.js - TypeError: "listener" argument must be a functionDiscord.js - TypeError:“listener”参数必须是一个函数
【发布时间】:2018-08-11 14:09:42
【问题描述】:

这是我的 Discord 机器人代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const token = require('./settings.json').token;

client.on('ready',() => {
    console.log('\n\nI\'m Online, mate.\nVery online, indeed.\n');
});
// Showing (in console) that the bot is online.



var prefix = "-"
client.on('message', message => { // Messaging stuff - Chat commands etc.

    if (!message.content.startsWith(prefix)) return;

    console.log('A command was found.')

    let args = message.content.split(' ').slice(1);
    var argresult = args.join(' ');

    if (message.author.bot) return;
    // If bot is the author (prevent selfbot.)



    if (message.content.startsWith(prefix + 'moffa')) { // Checks if bot is online and shows latency.
        message.channel.send(`**MOFFA!!** \`${Date.now() - message.createdTimestamp} ms delay.\``);
    } else



    if (message.content.startsWith(prefix + 'moos')) { // Cross-server messaging.
        client.channels.get('censored').send('Cross-channel typing is working, yes.');
        message.channel.send('Look in <#censored>.')
    } else



    if (message.content.startsWith(prefix + 'sg')) { // Set game.
        if (!argresult) argresult = null;
        client.user.setActivity(argresult)
        message.channel.send('Set `' + argresult + '` as activity.')
    } else



    if (message.content.startsWith(prefix + 'ss')) { // Set status.
        if(!argresult) argresult = 'online';
        client.user.setStatus(argresult);
        message.channel.send('Updated status.')
    } else



    if (message.content.startsWith(prefix + 'fnname')) { // Sends censored's name on Fortnite.
        message.channel.send('`censored` is <@!censored>\'s name on Fortnite');
    } else



    if (message.content.startsWith(prefix + 'commands')) { // Shows a message with all commands. Update manually.
        message.channel.send('All the current commands are: `moffa`, `fnname` & `commands`. Ex. use: `' + prefix + 'moffa`.');
    }
});


client.login(token);

我收到此错误:

PS C:\Users\censored\Documents\Code\nodejs\discord bot 2> nodemon
[nodemon] 1.17.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
events.js:239
    throw new TypeError('"listener" argument must be a function');
    ^

TypeError: "listener" argument must be a function
    at _addListener (events.js:239:11)
    at Client.addListener (events.js:297:10)
    at Object.<anonymous> (C:\Users\censored\Documents\Code\nodejs\discord bot 2\app.js:7:8)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
[nodemon] app crashed - waiting for file changes before starting...

它没有指定错误在哪里,或者如何修复它。

我正在使用 Atom(最新版本。) Nodemon 可以让我绕过每次刷新机器人时都必须打开和关闭终端。 它工作得很好,直到我尝试制作一个丰富的嵌入命令。 当我删除命令时,我得到了这个错误。 嗯,我也是在制作的时候得到的。

我们将不胜感激。谢谢!

【问题讨论】:

    标签: javascript node.js bots discord discord.js


    【解决方案1】:

    这部分错误清楚地表明错误发生在第 7 行。

        at Object.<anonymous> (C:\Users\censored\Documents\Code\nodejs\discord bot 2\app.js:7:8)
    

    第 7 行有 client.on(''),它需要第二个参数作为函数。

    此外,在该行中没有您正在收听的事件。

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 2017-07-30
      • 2019-01-24
      • 1970-01-01
      • 2020-09-16
      • 2017-05-08
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      相关资源
      最近更新 更多