为什么Discord.js API无法看到message.channel.id?

时间:2019-06-30 23:28:48

标签: javascript node.js discord.js

我刚刚用该代码创建了一个简单的机器人:

client.on('message', async message => {
    if (!message.author.bot) {
        if (!getClub(message) && (message.channel.id == channel_bot || message.channel.type != "dm")) { // 
            getRadio(message);
        }
    } else if (message.channel.id = channel_dev && message.author.bot) {
        getDevCommands(message);
    }
});

然后我用

检查bot命令
function getClub(msg) {
    const args = msg.content.slice(msg.content.includes(config.prefix) ? config.prefix.length : 0).trim().split(/ +/g);

    let isClub = false;
    club_commands.forEach(function (element) {
        if (element.id == "club" && element.commands.includes(args[0])) {
            isClub = true;
        }
    });
    if (!isClub) {
        return false;
    }
    club_commands.forEach(function (element) {
        // element is parsed object from JSON: {"id":"join", "commands":"join,attach,invite..."}
        if (element.commands.includes(args[1])) {
            switch (element.id) {
                case "stats":
                    clubStats(msg);
                    return true;
                case "join":
                    clubParticipation(msg, 1);
                    return true;
                case "leave":
                    clubParticipation(msg, 0);
                    return true;
                default:
                    // do nothing
                    break;
            }
        }
    });
    return false;
}

因此,在clubPartisipation()中,我正在获取msg.channel.id-实际的通道ID,但对于所有后续消息仅“ true”

function clubParticipation(msg, status) {
    const args = msg.content.trim().split(/ +/g).splice(2, 2).join("");
    if (args.length <= 3) {
        msg.channel.send("test0");
    } else {
        let member = guild.members.get(msg.author.id);
        if (status == "1") {
            msg.channel.send("test1").catch(console.log);
        } else {
            msg.channel.send("test3").catch(console.log);
        }
        getHTTPResponce(config.server_url + 'add/club/participation?channel_id=' + msg.channel.id + '&status=' + status + '&user_id=' + member.id + '&club_id=' + Base64.encode(Base64.encode(args)) + '&token=' + config.server_token, msg)
            .catch(console.log);
    }
}

错误代码为

{ DiscordAPIError: Invalid Form Body                                                                                       
channel_id: Value "true" is not snowflake.                                                                                 
    at item.request.gen.end (/root/curatortojps/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15
)                                                                                                                          
    at then (/root/curatortojps/node_modules/snekfetch/src/index.js:215:21)                                                
    at <anonymous>                                                                                                         
    at process._tickCallback (internal/process/next_tick.js:189:7)                                                         
  name: 'DiscordAPIError',                                                                                                 
  message: 'Invalid Form Body\nchannel_id: Value "true" is not snowflake.',                                                
  path: '/api/v7/channels/true/messages',                                                                                  
  code: 50035,                                                                                                             
  method: 'POST' }       

1 个答案:

答案 0 :(得分:1)

在您的第一段代码中,您具有:

  
(message.channel.id = channel_dev && message.author.bot)

=assignment operator。这意味着您正在 message.channel.id设置为channel_dev && message.author.bot的值,即布尔值(truefalse)。


您应该使用=====之类的equality operator来比较message.channel.id的值。要了解两者之间的区别,请查看this answer

(message.channel.id === channel_dev && message.author.bot)