Discordbot doesn't play music with ytdl-core

一个人想着一个人 提交于 2020-04-16 03:06:04

问题


I typed in the command in Discord to play music and the bot joins but disconnects a few seconds afterwards from the Voice Channel without playing the music. A few days ago it worked just fine but now it doesn't and I can't find the mistake in the code. I have ffmpeg and opusscript installed so that can't be the mistake. I hope someone can help me:

const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const bot = new Discord.Client();
const prefix = "$";
const token = "";

function play(connection, message){
    var server = servers[message.guild.id];

    server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audioonly"}));

    server.queue.shift();

    server.dispatcher.on("end", function() {
        if(server.queue[0]) play(connection, message);
        else connection.disconnect();
    });
}

var servers = {};

bot.on("ready", function(){
    console.log("Ready");
});

bot.on("message", function(message) {
    if(message.author.equals(bot.user)) return;
    if(!message.content.startsWith(prefix)) return;

    var args = message.content.substring(prefix.length).split(" ");

switch(args[0].toLowerCase()){
    case "play":
        if(!args[1]){
            message.channel.send("Please add a link");
            return;
        }
        if(!message.member.voiceChannel){
            message.channel.send("You have to be in a Voice Channel");
            return;
        }
        if(!servers[message.guild.id]) servers[message.guild.id] = {
            queue: []
        };
        var server = servers[message.guild.id];

        server.queue.push(args[1]);

        if(!message.guild.voiceConnection) message.member.voiceChannel.join().then(function(connection){
            play(connection, message);
        });
        break;
    default:
        message.channel.send("Unknown Command");
        break;
}

});

bot.login(token);

回答1:


Re-watch the video and make sure you copied his code correctly, if you have then go into the terminal in your project folder, type in the following:

npm install

That will installed all your missing dependencies. If you don't have node installed do that (just a quick google search it's easy)

If it still isn't working, I don't know about how to fix it, but considering you're already using someone elses code, here is another option it is far superior to Aeirety music bot. It has the following functionalitys:

  • play |: Play audio from YouTube.
  • skip [number]: Skip a song or multi songs with skip [some number],
  • queue: Display the current queue.
  • pause: Pause music playback.
  • resume: Resume music playback.
  • volume: Adjust the playback volume between 1 and 200.
  • leave: Clears the song queue and leaves the channel.
  • clearqueue: Clears the song queue.

It's also simple to install take a look at it and consider it.




回答2:


I know this is an old issue but for whomever meets with this upsetting misfortune in the future, all you have to do is reinstall the ytdl-core module with npm install --save ytdl-core. Yeah it's as simple and dumb as that...

Good luck!



来源:https://stackoverflow.com/questions/48836812/discordbot-doesnt-play-music-with-ytdl-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!