【问题标题】:"TypeError: Cannot read property 'push' of undefined" discord.js“TypeError:无法读取未定义的属性‘push’” discord.js
【发布时间】:2019-07-11 09:52:40
【问题描述】:

我有这个完整的错误:

(node:9352) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined
    at Object.exports.run (C:\users\Cocoloco2005\Desktop\peterbot\commands\play.js:24:16)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:9352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9352) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我在“播放”命令中使用 Discord.js 创建我的不和谐机器人时发生的问题

(此处为完整命令:

const ytdl = require('ytdl-core');
exports.run = async (client, message, args, ops) => {
    if (!message.member.voiceChannel) return message.channel.send('Connectes toi à un salon vocal');


    if (!args[0]) return message.channel.send('Entres une URL');
    let validate = await ytdl.validateURL(args[0]);
  
    

    if (!validate) {
      let commandFile = require('./search.js');
      return commandFile.run(client, message, args, ops);
    
    }

    let info = await  ytdl.getInfo(args[0]);

	let data = ops.active = (message.guild.id) || {};
    if (!data.connection) data.connection = await message.member.voiceChannel.join();
    if(!data.queue) data.queue = [];
    data.guildID = message.guild.id;

    data.queue.push[0]({
        songTitle: info.title,
        requester: message.author.tag,
        url: args[0],
        announceChannel: message.channel.id

    });

    if (!data.dispatcher) play(client, ops, data);
    else {
        message.channel.send(`Added to queue: ${info.title} | requested by: ${message.author.id}`)
    }
    ops.active.set(message.guild.id, data);


}
async function play(client, ops, data) {
    client.channels.get(data.queue[0].announceChannel).send(`Now Playing: ${data.queue[0].songTitle} | Requested by: ${data.queue[0].requester}`);

    data.dispatcher = await data.connection.playStream(ytdl(data.queue[0].url, {filter: 'audioonly'}));
    data.dispatcher.guildID = data.guildID;

    data.dispatcher.once('end', function() {
        end(client, ops, this);

    });

}
function end(client, ops, dispatcher){

    let fetched = ops.active.get(dispatcher.guildID);

    fetched.queue.shift();

    if (fetched.queue.length > 0) {
        ops.active.set(dispatcher.guildID, fetched);
        play(client, ops, fetched);
    } else {
        ops.active.delete(dispatcher.guildID);

        let vc = client.guilds.get(dispatcher.guildID).me.voiceChannel;  

        if (vc) vc.leave();

    }

}

我不知道该怎么做,我没有尝试任何“特别”的东西,因为我是 Javascript 新手。 我尝试搜索,但没有找到任何关于有人遇到我的错误的信息。

【问题讨论】:

    标签: javascript discord


    【解决方案1】:

    您的错误在此块中:

    data.queue.push[0]({
        songTitle: info.title,
        requester: message.author.tag,
        url: args[0],
        announceChannel: message.channel.id
    
    });
    

    除非pushqueue 的可索引属性,否则第一行可能是问题所在。在我看来,您可能不小心添加了[0],因为如果没有它,这将是一个有效的声明。我希望这会有所帮助!

    【讨论】:

    • 谢谢,但错误仍然存​​在,我记得我试图把[0] 来修复“推送错误”,还是谢谢你!
    • Ps/Pd:机器人连接到 VoiceChannel,但他什么也没玩,而且我还有 2 个错误(我猜)(节点:9732) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝 id:1)(节点:9732)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。
    • @Cocoloco 好的!然后我的下一个假设是 data 变量值没有被正确分配。 let data = ops.active = (message.guild.id) || {}; 行有什么问题吗?
    • 我认为这可能是 /* global Map*/ /* global client*/ const active = new Map(); 在 server.js/index.js 文件中,我需要更改原始代码,指定 ops active o 类似的东西,可能是?
    • 现在我记得我改变了什么,那是let ops = { ownerID: ownerID, active: active } 部分,active: active
    猜你喜欢
    • 1970-01-01
    • 2023-01-14
    • 2020-07-14
    • 2021-05-29
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多