提问者:小点点

Javascript中的不和音乐机器人


运行此脚本后没有错误,但不知何故,当我尝试使用任何参数时,如play,什么都没有发生。我没有得到任何错误。脚本只是登录到Disord bot,除了脚本给出正确登录反馈的最后一行之外,什么都没有发生。

const { Client, Intents, Attachment} = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const PREFIX = '!';
const ytdl = require("ytdl-core");

var servers = {};

client.on('message', message => {
    let args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0]) {
        case 'play':

            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();
                    }
                });
            }

            if(!args[1]){
                message.channel.send("give a link");
                return;
            }

            if(!message.member.voiceChannel){
                message.channel.send("you must be on voice chat to play music");
                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;

        case "ping":
            message.channel.send("pong");
            console.log("pong");
            break;
    }
});

client.login(token);

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
  });

共1个答案

匿名用户

尝试使用“play-dl”模块而不是ytdl-core。

所以演奏音乐就是现在

const ytdl = require('play-dl');

// Play a music
await ytdl.stream(song_url, { filter: "audioonly" })

// Search something on YouTube
await ytdl.search(arguments, { source: { youtube: "video" } })

// Test if is a YouTube url
if (ytdl.yt_validate(url) === "video" && string.startsWith('https'))

该模块还可以从Deezer、Spotify和SoundCloud播放

希望这有助于:)