簡體   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