Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - Sme answer variants not working discord.js

I tried to make a character trivia bot where when I write .tc it shows a random character and you have to answer what is the name of the character in 15 seconds, the code for it is in a file called triviachar.js and all the answer variants are put as variables in a js file called triviacharacterlist.js, but I am having a problem where some of the answer variants would work but the others won't. It's the strangest bot behavior I have even encountered, so If you would help me out it would mean a lot, here is the code. (also I'm not that good at programming) (btw I'm using discord.js-commando):

(there is still more code in these files but its kinda the same as this)

triviachar.js:

run(msg) {
    const fivePoint = "Be3 bo6 u have asnwer correct, but u have answer without anime/game name so u get 5 points only lel";
    const tenPoint = "Bo3 bo6 u have answer correct with anime/game name so u get 10 points instead pog"
    const guessCharacterString = "Guess character bo3"
    var characterRandom = Math.floor(Math.random() * 5);

    if (characterRandom == 0) {
        const iKF = Characters.KiritoVariants;
        const triviaKiritoEmbed = new MessageEmbed()
            .setColor('#008fff')
            .setTitle(guessCharacterString)
            .setImage(Characters.CharacterImgs.kiritoimg)
    
        msg.channel.send(triviaKiritoEmbed)

        const filterK = m => m.content.includes(iKF.kirito0 || iKF.kirito1 || iKF.kirito2 || iKF.kirito3 || iKF.kirito4 || iKF.kirito5 || iKF.kirito6 || iKF.kirito7 || iKF.kirito8);
        msg.channel.awaitMessages(filterK, { max: 1, time: 15000, errors: ['time'] })
        .then(collected => {switch(collected.first().content){
            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'));
    }

triviacharacterlist.js:

//character images -------------------------------------------------------------------------------------------------------------
const CharacterImgs = {
    kiritoimg: "https://i.pinimg.com/originals/05/b8/d9/05b8d9a934b8d831fef385e7f60b5625.jpg",
    asunaimg: "https://pm1.narvii.com/6210/87c8712f955134bdbfe4135fbded8366ea21f917_00.jpg",
    jojoimg: "https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/f6ec6f25-09b6-414d-aeac-9a7cb7527ee7/dcdtfn4-618039ef-f3ee-4333-90a0-208fb4d0db2d.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOiIsImlzcyI6InVybjphcHA6Iiwib2JqIjpbW3sicGF0aCI6IlwvZlwvZjZlYzZmMjUtMDliNi00MTRkLWFlYWMtOWE3Y2I3NTI3ZWU3XC9kY2R0Zm40LTYxODAzOWVmLWYzZWUtNDMzMy05MGEwLTIwOGZiNGQwZGIyZC5wbmcifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6ZmlsZS5kb3dubG9hZCJdfQ.QpJ2Po7ZB8AlZsmtuMH1O6zg7GpGWMo_RW-126L8ftI",
    dekuimg: "https://media.discordapp.net/attachments/472313197836107780/606995677737779211/XokvvZc.png",
    senkuimg: "https://pm1.narvii.com/7346/a2bb65cfca86e3d2bf85f1b12e2132af3d43f5f6r1-736-1041v2_uhq.jpg"
};

//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'
};

these are images form discord: https://i.stack.imgur.com/RkAf6.png https://i.stack.imgur.com/gfBAh.png

question from:https://stackoverflow.com/questions/65921040/sme-answer-variants-not-working-discord-js

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

1 Answer

0 votes
by (71.8m points)

You have your message collector filter setup wrong. Chaining variables with a || won't do what you intend to use it for. For more information, checkout Logical OR operator.

In short, your message collector filter only collects messages whose content include the value of iKF.kirito0.

The fix for this is to store all the values in an array and check if the message includes at least one of the values of this array. See the code example below.

const possibleAnswers = [iKF.kirito0, iKF.kirito1, iKF.kirito2, etc...];

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...