【问题标题】:TypeError: Cannot read properties of null (reading 'play')TypeError:无法读取 null 的属性(读取“播放”)
【发布时间】:2022-01-10 00:01:36
【问题描述】:

我人生中第一次尝试创建一个不和谐的机器人,所以我想说我的代码远非完美和优化^^'

我有问题,TypeError: Cannot read properties of null (reading 'play').

当我尝试“跳过”命令时出现问题。一切正常,但是当我尝试该命令时,我的调试控制台中完全没有任何内容,只有机器人返回的不和谐错误。

播放命令:

const { VoiceConnection } = require("discord.js");
const { Command, CommandoMessage } = require("discord.js-commando");
const ytdl = require('ytdl-core-discord');
const {UserNotInVoiceChannel } = require(`../../strings.json`);

module.exports = class PlayCommand extends Command {
    constructor(client) {
        super(client, {
            name: `play`,
            aliases: [`p`],
            group: `music`,
            memberName: `play`,
            description: `Lit une video youtube`,
            args: [
                {
                    key: `query`,
                    prompt: `Quelle musique veux tu lire?`,
                    type: `string`
                }
            ]
        });
    }

    /**
     * 
     * @param {CommandoMessage} message 
     * @param {String} query 
     */
    async run(message, { query }) {
        const server = message.client.server;

        if(!message.member.voice.channel){
            return message.say(UserNotInVoiceChannel);
        }
        
        await message.member.voice.channel.join().then((connection) => {
            if (server.currentVideo.url != "") {
                server.queue.push({ title: "", url: query });
                return message.say("Ajouté a la file d'attente")
            }

            server.currentVideo = { title: "", url: query };
            this.runVideo(message, connection, query);
        });
    }

    /**
     * 
     * @param {*} message 
     * @param {VoiceConnection} connection 
     * @param {*} video 
     */
    async runVideo(message, connection, videoUrl) {
        const server = message.client.server;
        const dispatcher = connection.play(await ytdl(videoUrl, {filter: 'audioonly'}), { type: 'opus' });
        
        server.queue.shift();
        server.dispatcher = dispatcher;
        server.connection = null

        dispatcher.on('finish', () => {
            if (server.queue[0]) {
                server.currentVideo = server.queue[0]; 
                return this.runVideo(message, connection, server.currentVideo.url);
            }
        });

        return message.say("En train de jouer :notes:");
    }
}

跳过命令:

const { Command, CommandoMessage } = require("discord.js-commando");
const {UserNotInVoiceChannel, BotNotInVoiceChannel } = require(`../../strings.json`);


module.exports = class skipCommand extends Command {
    constructor(client) {
        super(client, {
            name: `skip`,
            group: `music`,
            memberName: `skip`,
            description: `Skip la video.`
        });
    }

    /**
     * 
     * @param {CommandoMessage} message 
     * @param {String} query 
     */
    async run(message) {
        const voiceChannel = message.member.voice.channel;
        const server = message.client.server;

        if(!voiceChannel){
            return message.say(UserNotInVoiceChannel)
        }

        if (!message.client.voice.connections.first()) {
            return message.say(BotNotInVoiceChannel);
        }
        
        server.queue.shift();

        if (!server.queue[0]) {
            server.currentVideo = {url: "", title: "Rien pour le moment"}

        }

        server.currentVideo = server.queue[0];
        server.connection.play(await ytdl(server.currentVideo.url, {filter: 'audioonly'}), { type: 'opus' } );

        return message.say(":fast_foward: Ignoré :thumbsup:");
    }
}

【问题讨论】:

  • 你能发布你的跳过命令吗?
  • 我已经编辑了原帖

标签: javascript node.js discord discord.js audio-player


【解决方案1】:

错误表示它无法在您的跳过命令中读取null 的属性(读取'play')。这意味着server.connectionnull

如果你检查你的播放命令,你可以看到在runVideo里面你设置了server.connection = null

您的意思是将connection 传递给runVideo 吗?

async runVideo(message, connection, videoUrl) {
  const server = message.client.server;
  const dispatcher = connection.play(
    await ytdl(videoUrl, { filter: 'audioonly' }),
    { type: 'opus' },
  );

  server.queue.shift();
  server.dispatcher = dispatcher;
  server.connection = connection;

  dispatcher.on('finish', () => {
    if (server.queue[0]) {
      server.currentVideo = server.queue[0];
      return this.runVideo(message, connection, server.currentVideo.url);
    }
  });

  return message.say('En train de jouer :notes:');
}

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2021-12-04
    • 2021-12-17
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2022-11-17
    相关资源
    最近更新 更多