Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

javascript - Don't know how to change to lowercase in this scenario

I am trying to use the discord.js awaitMessages method but it doesn't seem to work with uppercase letters because my possible answers (triviacharacterslist.js) are all lowercase. I tried to convert this (in the code) but it doesn't seem to work.

triviachar.js:

const iKF = Characters.KiritoVariants;
        const possibleAnswers = [iKF.kirito0, iKF.kirito1, iKF.kirito2, iKF.kirito3, iKF.kirito4, iKF.kirito5, iKF.kirito6, iKF.kirito7, iKF.kirito8];
        const triviaKiritoEmbed = new MessageEmbed()
            .setColor('#008fff')
            .setTitle(guessCharacterString)
            .setImage(Characters.CharacterImgs.kiritoimg)
    
        msg.channel.send(triviaKiritoEmbed)

        const filterK = m => possibleAnswers.some(answer => m.content.includes(answer));
        msg.channel.awaitMessages(filterK, { max: 1, time: 15000, errors: ['time'] })
        .then(collected => {switch(collected.first().content.toLowerCase()){
            case Characters.KiritoVariants.kirito0:
                msg.channel.send(fivePoint);
                break;
            case Characters.KiritoVariants.kirito3:
                msg.channel.send(fivePoint);
                break;
            case Characters.KiritoVariants.kirito6:
                msg.channel.send(fivePoint);
                break;
            case Characters.KiritoVariants.kirito1:
                msg.channel.send(tenPoint);
                break;
            case Characters.KiritoVariants.kirito2:
                msg.channel.send(tenPoint);
                break;
            case Characters.KiritoVariants.kirito4:
                msg.channel.send(tenPoint);
                break;
            case Characters.KiritoVariants.kirito5:
                msg.channel.send(tenPoint);
                break;
            case Characters.KiritoVariants.kirito7:
                msg.channel.send(tenPoint);
                break;
            case Characters.KiritoVariants.kirito8:
                msg.channel.send(tenPoint);
                break;
        }})
        .catch(collected => msg.channel.send('too bad u took too long ya 3am'));
    }

triviacharacterslist.js:

//character names ---------------------------------------------------------------------------------------------------------------

const KiritoVariants = {
    kirito0: 'kirito',
    kirito1: 'kirito sao',
    kirito2: 'kirito sword art online',
    kirito3: 'kazuto',
    kirito4: 'kazuto sao',
    kirito5: 'kazuto sword art online',
    kirito6: 'kazuto kirigaya',
    kirito7: 'kazuto kirigaya sao',
    kirito8: 'kazuto kirigaya sword art online'
};
question from:https://stackoverflow.com/questions/65933897/dont-know-how-to-change-to-lowercase-in-this-scenario

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think @Zsolt had the right idea but the execution is a bit wrong. Since all your answers are already in lowercase, calling .toLowerCase() on an answer string won't do much. Instead, you need to call .toLowerCase() on the string content. See the example below:

const filterK = m => possibleAnswers.some(answer => m.content.toLowerCase().includes(answer));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...