有没有办法使这种情况下的3种情况?

时间:2020-08-04 21:08:44

标签: javascript discord.js

我制作了一个Discord机器人,并要求在Discord服务器上的此代码上寻求帮助。他们建议在这里问。

这是原始代码

    if(msg.content.toLowerCase() === "yandev, buy something"){
    var Patreon = Math.random();
        if (Patreon > 0.5)
       {
             msg.channel.send('image link');
             msg.channel.send('Just bought this with my patreon money.');

       }
        else
        {
             msg.channel.send('image link');
             msg.channel.send('Just bought 3 of these with my patreon money.')
        }

})

这是我在不和谐的人的帮助下编写的代码

    if(msg.content.toLowerCase() === "yandev, buy something"){
    var Patreon = Math.random();
        if (Patreon > 0.5)
       {
             msg.channel.send('image link');
             msg.channel.send('Just bought this with my patreon money.');

       }
        else
        {
             msg.channel.send('image link');
             msg.channel.send('Just bought 3 of these with my patreon money.')
        }
        else if
        {
             msg.channel.send('image link')
             msg.channel.send('Just bought this with my patreon money.')
        }
})  

这两个都给出相同的错误:

                else if
                ^^^^

SyntaxError: Unexpected token 'else'
                else
                ^^^^

SyntaxError: Unexpected token 'else'

我尝试自己修复它,但找不到解决方法。 所以现在我在这里。谢谢。

2 个答案:

答案 0 :(得分:0)

您需要切换elseelse if。订单必须始终为if -> else if -> else。您还缺少else if中的条件。

这很有意义,因为最后一个else将涵盖所有您未明确检查的条件。

答案 1 :(得分:0)

底部的右括号是什么? 从第一块中提供的代码来看,应该是这样。

   if(msg.content.toLowerCase() === "yandev, buy something"){
    var Patreon = Math.random();
        if (Patreon > 0.5)
       {
             msg.channel.send('image link');
             msg.channel.send('Just bought this with my patreon money.');

       }
        else
        {
             msg.channel.send('image link');
             msg.channel.send('Just bought 3 of these with my patreon money.')
        }

}

第二个代码块实际上没有任何意义,因为其他代码块没有条件。

尝试删除第一个块的最后一行上的')',看看是否可行。