繁体   English   中英

我正在尝试为我的 discord 机器人发出轮询命令,但它一直给我错误:“TypeError: Cannot read property 'push' of undefined”

[英]I'm trying to make a poll command for my discord bot, but it keeps giving me the error: “TypeError: Cannot read property 'push' of undefined”

标题几乎说明了一切,我想知道是否有人可以帮助我。 我正在使用 discord v11.5.1

这是我的代码:

exports.run = async (bot, message) => {
    const options = ["🇦", "🇧", "🇨", "🇩", "🇪", "🇫", "🇬", "🇭", "🇮", "🇯", "🇰", "🇱", "🇲", "🇳", "🇴", "🇵", "🇶", "🇷", "🇸", "🇹", "🇺", "🇻", "🇼", "🇽", "🇾", "🇿"];
    const regex = /(["'])((?:\\\1|\1\1|(?!\1).)*)\1/g;
    const args = message.content.trim().split(/ +/g);
    let question, choices, content = [];
    let match;

    message.delete();
    for(let i=1;i<args.length;i++) {
        if(args[i].startsWith('"')) break;
        else question.push(args[i]);
    };

    question = question.join(" ");

    while(match = regex.exec(args.join(" "))) choices.push(match[2]);
    for(let i=0;i<choices.length;i++) content.push(`${options[i]} ${choices[i]}`);
    content = content.join("\n");

    message.channel.send(`${message.author} **started a poll**`, new(require("discord.js")).RichEmbed().setColor(0xbc13fe).setTitle(`:bar_chart: **Poll |** ${question}`).setDescription(content).setTimestamp()).then(async m => {for(let i=0;i<choices.length;i++) await m.react(options[i])});
}

任何帮助将不胜感激: :)

当您编写let question, choices, content = []时,您会创建三个变量。 content将是一个空数组, questionchoices将是undefined

如果你想用空的 arrays 来初始化它们,你可以这样做:

let question = [];
let choices = [];
let content = [];

如果下面都是数组,那么你想用空数组初始化它,然后使用=而不是逗号,

 let question = choices = content = [];

代替

 let question, choices, content = [];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM