首页 > 解决方案 > 无法读取未定义错误 aki-api 的属性“toLowerCase”

问题描述

我正在尝试制作一个具有 Akinator 命令的 discord.js 机器人(我猜知道是什么

TypeError: Cannot read property 'toLowerCase' of undefined
    at regionURL (E:\Users\kazak\Documents\GitHub\Vixo\node_modules\aki-api\dist\functions\Request.js:90:63)
    at Akinator.start (E:\Users\kazak\Documents\GitHub\Vixo\node_modules\aki-api\dist\Akinator.js:35:56)
    at Object.execute (E:\Users\kazak\Documents\GitHub\Vixo\commands\fun\aki.js:36:16)
    at Object.execute (E:\Users\kazak\Documents\GitHub\Vixo\events\message.js:202:12)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

我去了错误位置,我发现了这一行await aki.start(),但我不明白为什么它会把我带到这里是其余的代码,所以你们都可以理解。我会在错误发送给我的地方添加注释

const { Aki } = require('aki-api');
const { MessageEmbed } = require('discord.js');
module.exports = {
    name: 'aki',
    description: 'Akinator',
    category: 'fun',
    aliases: [],
    cooldown: 4,
    // eslint-disable-next-line no-unused-vars
    async execute(message, args, client) {
        const regions = ['person', 'object', 'animal'];
        const { verify } = require('../../function');
        if (!args[0]) return message.channel.send('Please provide a category!\nEither `person or object or animal`');

        let region;
        const stringAki = args[0].toLowerCase();

        if (stringAki === 'person'.toLocaleLowerCase()) region = 'en';
        if (stringAki === 'object'.toLocaleLowerCase()) region = 'en_objects';
        if (stringAki === 'animal'.toLocaleLowerCase()) region = 'en_animals';
        if (!regions.includes(stringAki)) return message.channel.send('Please provide a valid category!');
        message.channel.send('Please wait...').then((m) => m.delete({ timeout: 4000 }));
        try {
            const aki = new Aki(region); // i have a suspission that the error has to do something with this
            let ans = null;
            let win = false;
            let timeguessed = 0;
            let guessResetNum = 0;
            let wentBack = false;
            let forceGuess = false;
            const GuessBlackList = [];
            while (timeguessed < 3) {
                if (guessResetNum > 0) guessResetNum--;
                if (ans === null) {
                    await aki.start(); // The error leads me here
                }
                else if (wentBack) {
                    wentBack = false;
                }
                else {
                    try {
                        await aki.step(ans);
                    }
                    catch(err) {
                        console.log(err);
                        await aki.step(ans);
                    }
                }
                if (!aki.answers || aki.currentStep >= 79) forceGuess = true;
                const answers = aki.answers.map((answer) => answer.toLowerCase());
                answers.push('end');
                if (aki.currentStep > 0) answers.push('back');
                const embed = new MessageEmbed()
                    .setAuthor(`Question Number ${aki.currentStep + 1}`, client.user.avatarURL())
                    .setDescription([
                        `${aki.question}`,
                        'Available answers:',
                        `${aki.answers.join(' | ')}${aki.currentStep > 0 ? ' | Back' : ''} | End **`,
                    ])
                    .setColor('RANDOM');
                await message.channel.send(embed);
                const filter = (res) => res.author.id === message.author.id && answers.includes(res.content.toLowerCase());
                const messages = await message.channel.awaitMessages(filter, {
                    max: 1,
                    time: 30000,
                });
                if (!messages.size) {
                    await message.channel.send('Times up!');
                    win = true;
                    break;
                }
                const choice = messages.first().content.toLowerCase();
                if (choice.toLowerCase() === 'end'.toLocaleLowerCase()) {
                    forceGuess = true;
                }
                else if (choice.toLowerCase() === 'back'.toLocaleLowerCase()) {
                    wentBack = true;
                    await aki.back();
                    continue;
                }
                else {
                    ans = answers.indexOf(choice);
                }
                if ((aki.progress >= 90 && !guessResetNum) || forceGuess) {
                    timeguessed++;
                    guessResetNum += 10;
                    await aki.win();
                    const guess = aki.answers.filter((g) => !GuessBlackList.includes(g.id))[0];
                    if (!guess) {
                        await message.channel.send('I can\'t think of anyone!');
                        win = true;
                        break;
                    }
                    GuessBlackList.push(guess.id);
                    const embedss = new MessageEmbed()
                        .setColor('RANDOM')
                        .setTitle(`I'm ${Math.round(guess.proba * 100)}% sure its`)
                        .setDescription(`${guess.name}${guess.description ? `\nProffesion - ${guess.description}` : ''}\nRanking : ${guess.ranking}
                        Type yes/no to confirm or deny!`)
                        .setImage(guess.absolute_picture_path || null);
                    await message.channel.send(embedss);
                    const verification = await verify(message.channel, message.author);
                    if (verification === 0) {
                        win = 'time';
                        break;
                    }
                    else if (verification) {
                        win = false;
                        break;
                    }
                    else {
                        const exmessage = timeguessed >= 3 || forceGuess ? 'I give up!' : 'I can keep going!';
                        const embeds = new MessageEmbed()
                            .setDescription([`Is that so? ${exmessage}`])
                            .setColor('RANDOM');
                        await message.channel.send(embeds);
                        if (timeguessed >= 3 || forceGuess) {
                            win = true;
                            break;
                        }
                    }
                }
            }
            if (win === 'time') return message.channel.send('I guess your silence means i won >:D');
            if (win) {
                const embedsss = new MessageEmbed()
                    .setDescription('You have defeated me this time!')
                    .setColor('RANDOM');
                return message.channel.send(embedsss);
            }
            else {
                return message.channel.send('Guessed it right once again!');
            }
        }
        catch(err) {
            console.log(err);
            return message.channel.send(`Oh no an error occured: \`${err.message}\` Try again`);
        }

    },
};

还有一个文件,但我不会显示它,因为它会在之后执行await aki.start()

标签: node.jsdiscord.js

解决方案


推荐阅读