尝试制作不和谐的js kick命令,但它会根据用户ID进行踢

时间:2020-09-29 16:55:32

标签: javascript node.js discord discord.js

我正在尝试发出一个不和谐的踢命令,以踢我服务器上的特定人员。是的,它不是很实用,但这正是我的机器人所需要的。 这是我的代码:

if (command == "kickderek") {
    let member = message.guild.member("<@userid>");
    member.kick();
    kickd = Math.floor(Math.random() * 10000);
    if (kickd == 1) {
        member.kick();
        message.channel.send("God has accepted your wish and derek has been kicked");
        return;
    } else {
        message.channel.send("Try again, you failed kicking derek");
        return;
    }
}

每当我运行它时,都会出现错误:

/home/admin/discbot/disc.js:357
    member.kick();
           ^
TypeError: Cannot read property 'kick' of null
at Client.<anonymous> (/home/admin/discbot/disc.js:357:16)
at Client.emit (events.js:326:22)
at MessageCreateAction.handle (/home/admin/discbot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/admin/discbot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/admin/discbot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/admin/discbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/admin/discbot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/home/admin/discbot/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:314:20)
at Receiver.receiverOnMessage (/home/admin/discbot/node_modules/ws/lib/websocket.js:797:20)

有人已经帮助我解决了这个问题,我已经工作了几个小时了。

1 个答案:

答案 0 :(得分:0)

Cannot read property 'kick' of null表示member.kick的成员部分(如果未定义),意味着在定义成员时,let member = message.guild.member('<@userid>');返回空值。
只需传递不带括号符号的用户ID:let member = message.guild.member(USERID);

P.S。如果您仅尝试在RNG滚动之后踢球,则您的代码中有一个错字:您在rng滚动之前叫踢球

member.kick();
kickd = Math.floor(Math.random() * 10000);
if(kickd == 1){