【问题标题】:Music bot issue音乐机器人问题
【发布时间】:2020-11-01 04:03:01
【问题描述】:

我尝试编写自己的音乐机器人,我找到了几个视频和代码......但我现在使用的可能是最好的代码,但我的问题是它会播放歌曲,然后什么时候播放到最后 10-15 秒,音乐停止,没有错误。

const Discord = require('discord.js');
const client = new Discord.Client();
const {prefix, token} = require("./config.json")

var queue = new Map();

const ytdl = require('ytdl-core');

client.on('ready', () => console.log("A Zongorista a helyén!"));

client.on('message', async (message) => {
    if(message.author.bot) return;
    if(message.content.indexOf(prefix) !== 0) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if(command == "play") {
        if(!args[0]) return;
        let url = args.join(" ");
        if(!url.match(/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/)) return message.channel.send("Érvényes Youtube linket használj!");

        let serverQueue = queue.get(message.guild.id);
        let vc = message.member.voice;

        if(!vc) return message.channel.send("Nem vagy voice szobában!");

        if(!vc.channel.permissionsFor(client.user).has('CONNECT') || !vc.channel.permissionsFor(client.user).has('SPEAK')) return message.channel.send("Nincs hozzá jogom!");

        let songinfo = await ytdl.getInfo(url);
        let song = {
            title: songinfo.title,
            url: songinfo.video_url
        }

        if(!serverQueue) {
            let queueConst = {
                textChannel: message.channel,
                voiceChannel: vc.channel,
                connection: null,
                songs: [],
                volume: 5,
                playing: true
            };

            queue.set(message.guild.id, queueConst);
            queueConst.songs.push(song);

            try {
                let connection = await vc.channel.join();
                queueConst.connection = connection
                playSong(message.guild, queueConst.songs[0])
            } catch (error) {
                console.log(error);
                queue.delete(message.guild.id);
                return message.channel.send("Hiba történt a lejátszáskor Hiba: " + error);
            }
        } else {
            serverQueue.songs.push(song);
            return message.channel.send(`${song.title} hozzá lett adva a lejátszási listához!`)
        }
    }
})

/**
 * 
 * @param {Discord.Guild} guild 
 * @param {Object} song 
 */
async function playSong(guild, song) {
    let serverQueue = queue.get(guild.id);

    if(!song){
        serverQueue.voiceChannel.leave();
        queue.delete(guild.id);
        return;
    }

    const dispatcher = serverQueue.connection.play(ytdl(song.url)).on('end', () => {
        serverQueue.songs.shift();
        playSong(guild, serverQueue.songs[0]);
    })
    .on('error', () => {
        console.log(error)
    })

    dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}

client.login(token)

【问题讨论】:

  • 看起来音量是用setVolumeLogarithmic 减小而不是停止,所以尝试删除此行看看是否有帮助
  • 似乎与here 描述的错误相同,尽管我们从未设法修复它。尝试我在答案中发布的建议,并告诉我们这是否能解决您的问题。
  • 感谢 Stéphane 的提示! :D 现在它跳过最后 3-5 秒的问题......但它有很大帮助!谢谢!
  • 问题是我不能下载一些 eztensions :( 它写 Pythin 错误我重新安装它下载了 2.7 原因在错误中写到 2.7 正在工作但不是....我只是无法安装 opusscript 和discord.js 没有错误
  • 我从安装错误中得到的日志:link

标签: javascript discord discord.js


【解决方案1】:
if(command == "play") {
    if(!args[0]) return;
    let url = args.join(" ");
    if(!url.match(/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/)) return message.channel.send("Érvényes Youtube linket használj!");

    let serverQueue = queue.get(message.guild.id);
    let vc = message.member.voice;

    if(!vc) return message.channel.send("Nem vagy voice szobában!");

    if(!vc.channel.permissionsFor(client.user).has('CONNECT') || !vc.channel.permissionsFor(client.user).has('SPEAK')) return message.channel.send("Nincs hozzá jogom!");

    let songinfo = await ytdl.getInfo(url);
    let song = {
        title: songinfo.title,
        url: songinfo.video_url
    }

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 2020-08-04
    • 2020-05-15
    • 2019-02-26
    • 2019-08-03
    • 2021-07-25
    • 2021-05-05
    • 2022-01-11
    • 2019-04-04
    相关资源
    最近更新 更多