带括号的函数不返回任何内容

时间:2018-08-29 02:15:23

标签: javascript node.js discord discord.js

当我运行我的代码时,该代码应该打印出发件人的名字,“采用”,然后是第一个提到的用户

const { Client } = require('discord.js', 'async', 'discord-message-handler');
const bot = new Client();
const cfg = require('./config.json');
function adoptaUser() {
    var finalString = '';

    finalString += '**' + (user.username) + 'adopted' + (message.mentions.member.first) + '**' + ':heart:';
}

//Skips to command here!!

if(message.content.includes('hws:adopt')) {
    message.channel.send(adoptaUser(message.author.id));
}
    });

});

bot.login(cfg.token);
    //I only included the code which the function uses!!

虽然看不到语法错误,但仍然出现此错误:

Bot Ready on 2 servers
C:\Users\REMOVED\Desktop\Bot2\app.js:16
finalString += '**' + (user.username) + 'has adopted' + (message.mentions.member.first) + '**' + ':heart:';
                       ^

ReferenceError: user is not defined
at adoptaUser (C:\Users\Moneq\Desktop\AutoRole\app.js:16:28)
at Client.bot.on.message (C:\Users\Moneq\Desktop\AutoRole\app.js:38:30)
at emitOne (events.js:116:13)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle (C:\Users\Moneq\Desktop\AutoRole\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (C:\Users\Moneq\Desktop\AutoRole\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\Moneq\Desktop\AutoRole\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\Moneq\Desktop\AutoRole\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\Moneq\Desktop\AutoRole\node_modules\ws\lib\event-target.js:120:16)
at emitOne (events.js:116:13)
[nodemon] app crashed - waiting for file changes before starting...

尽管当我尝试在不带括号的情况下调用该函数时,尽管它发送的是实际函数而不是字符串,但我还是从机器人那里得到了响应。

对此有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

您正在将user.id传递给acceptaUser函数,似乎该函数需要消息对象。

更改为此:

message.channel.send(adoptaUser(message));

然后更改此:

(user.username)

对此

message.author.username

您还需要在returnaUserUser函数中添加return关键字。

在没有括号的情况下“调用”函数不是函数调用,它是对函数对象的引用,因此它确实会返回对象的内容。