【问题标题】:Awaitmessage and ToLowerCase() not working togetherAwaitmessage 和 ToLowerCase() 不能一起工作
【发布时间】:2020-10-09 03:19:25
【问题描述】:

所以在我的代码中,我试图让消息在任何情况下都被接受。例如,真 = TRuE。然而,当我这样做时,我得到了一个我不期望的结果。

而不是给我一个错误,或者做我期望的事情。发送一条消息说“时间到了”。

const safari = function (message) {
            message.channel.send(embed).then(() => {
                message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })
                    .then(collected => {
                        if (collected.first().toLowerCase() == "look at reflection") {
                            const embed2 = new discord.RichEmbed()
                                .setTitle("__**Chat to a tourist!**__")
                                .setColor("#49499c")
                                .setDescription("You begin to look at yourself, and slowly become infatuated with yourself!\n**__Wait 24 hours before guessing again!__**")
                            message.channel.send(embed2);
                        }
                        else if (collected.first() == "Pond dip") {
                            const embed2 = new discord.RichEmbed()
                                .setTitle("__**Off road!**__")
                                .setColor("#49499c")
                                .setDescription("You begin to pond dip, you had such a blast!")
                            message.channel.send(embed2);
                        }
                        else if (collected.first() == "Chase a frog") {
                            const embed2 = new discord.RichEmbed()
                                .setTitle("__**Trip over a rock!**__")
                                .setColor("#49499c")
                                .setDescription("Oh you're very clumbsy luckily its only a bruise!")
                            message.channel.send(embed2);
                        }
                        else { message.channel.send("Could not find this answer"); return safari(message); }
                    })
                    .catch(collected => { message.channel.send('Time up'); });
            });

如果有人能向我解释为什么会发生这种情况,以及解决问题的可能方法,我会非常感激!

【问题讨论】:

  • 您没有在代码的任何地方使用 toLowerCase,只使用 toUpperCase 和小写字符串。
  • 对不起,在测试过程中改变了,应该是小写。但问题仍然存在。

标签: javascript node.js discord discord.js


【解决方案1】:

这里有问题。你不能使用collected.first().toLowerCase()。您必须使用collected.first().content.toLowerCase(),因为collected.first() 是一个Message 对象。

另外,过滤器变量定义了吗?

【讨论】:

  • 感谢这工作!是的,它被定义了。就是这样。 const filter = response => { return response.author.id === message.author.id; }
  • 另外,为了帮助我理解,.content 是否只取它的字符串值?
【解决方案2】:

它实际上是为 toLowerCase() 结果使用一个变量,并使用带有开关的逻辑或带有小写值的 ifs 来进行正确的比较。

const safari = function(message) {
    message.channel.send(embed).then(()=>{
        message.channel.awaitMessages(filter, {
            max: 1,
            time: 30000,
            errors: ['time']
        }).then(collected=>{
            let msg = collected.first().toLowerCase();

            /* switch version */
            switch (msg) {
            case "look at reflection":
                {
                    const embed2 = new discord.RichEmbed().setTitle("__**Chat to a tourist!**__").setColor("#49499c").setDescription("You begin to look at yourself, and slowly become infatuated with yourself!\n**__Wait 24 hours before guessing again!__**")
                    message.channel.send(embed2);
                    break;
                }
            case "pond dip":
                {
                    const embed2 = new discord.RichEmbed().setTitle("__**Off road!**__").setColor("#49499c").setDescription("You begin to pond dip, you had such a blast!")
                    message.channel.send(embed2);
                    break;
                }
            case "chase a frog":
                {
                    const embed2 = new discord.RichEmbed().setTitle("__**Trip over a rock!**__").setColor("#49499c").setDescription("Oh you're very clumbsy luckily its only a bruise!")
                    message.channel.send(embed2);
                    break;
                }
            default:
                {
                    message.channel.send("Could not find this answer");
                    return safari(message);
                }
            }

            /* if version */

            if (msg == "look at reflection") {
                const embed2 = new discord.RichEmbed().setTitle("__**Chat to a tourist!**__").setColor("#49499c").setDescription("You begin to look at yourself, and slowly become infatuated with yourself!\n**__Wait 24 hours before guessing again!__**")
                message.channel.send(embed2);
            } else if (msg == "pond dip") {
                const embed2 = new discord.RichEmbed().setTitle("__**Off road!**__").setColor("#49499c").setDescription("You begin to pond dip, you had such a blast!")
                message.channel.send(embed2);
            } else if (msg == "chase a frog") {
                const embed2 = new discord.RichEmbed().setTitle("__**Trip over a rock!**__").setColor("#49499c").setDescription("Oh you're very clumbsy luckily its only a bruise!")
                message.channel.send(embed2);
            } else {
                message.channel.send("Could not find this answer");
                return safari(message);
            }
        }
        ).catch(collected=>{
            message.channel.send('Time up');
        }
        );
    }
    );
}

也许你可以用 ES6 和 async/await 来升级你的 JS,而不是 Promises 风格 另外..如果使用存储的答案,它可以更灵活 试试这样的……

class Message extends String {
    channel = new Channel()
}
class Channel {
    async awaitMessages(filter, options) {
        let messages = new Collection();
        messages.push(new Message("pond dip"))
        return messages;
    }
    async send(msg) {

    }
}

class Collection extends Array {
    first() {
        return this[0];
    }
}
class Discord {
    RichEmbed() {
        return new Element()
    }
}
class Element {
    setTitle() {
        return this;
    }
    setColor() {
        return this;
    }
    setDescription() {
        return this;
    }
}

let discord = new Discord()
let answers =
{
    "look at reflection": { title: "__**Chat to a tourist!**__", color: "#49499c", description: "You begin to look at yourself, and slowly become infatuated with yourself!\n**__Wait 24 hours before guessing again!__**" },
    "pond dip": { title: "__**Off road!**__", color: "#49499c", description: "You begin to pond dip, you had such a blast!" },
    "chase a frog": { title: "__**Trip over a rock!**__", color: "#49499c", description: "Oh you're very clumbsy luckily its only a bruise!" },
}

async function safari(message) {
    try {


        let embed = "embed";
        let filter = undefined;
        await message.channel.send(embed);
        let collected = await message.channel.awaitMessages(filter, {
            max: 1,
            time: 30000,
            errors: ['time']
        });
        let msg = collected.first().toLowerCase();

        let answer = answers[msg];
        if (answer) {
            const embed2 = discord.RichEmbed().setTitle(answer.title).setColor(answer.color).setDescription(answer.description)
            return message.channel.send(embed2);
        } else {
            return message.channel.send("Could not find this answer");
        }

    } catch (error) {
        console.error(error);
        return message.channel.send('Time up');
    }
}
safari(new Message("test"))

【讨论】:

  • 感谢您的帮助,但同样的问题仍然存在。感谢您指出我的代码如何更改。我总是忘记开关。
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 2014-01-07
  • 2019-08-17
  • 2016-11-23
  • 2019-02-18
  • 2015-05-16
  • 2017-10-14
  • 2012-06-01
相关资源
最近更新 更多