簡體   English   中英

discord 機器人不播放音樂

[英]discord bot doesnt play music

我有一個 discord 機器人,我正在嘗試制作播放音樂。 它將加入語音頻道,但不會播放任何內容。 我已經嘗試了很多東西,但似乎沒有任何效果。 這是代碼:

const ytdl = require("ytdl-core")
const ytSearch = require("yt-search")
const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource
} = require('@discordjs/voice')

module.exports = {
    name: "play",
    description: "Plays a song of your choice.",
    async execute(messageCreate, args) {
        const voiceChannel = messageCreate.member.voice.channel

        if (!voiceChannel) return messageCreate.channel.send("You must be in a voice channel to run this command.")
        const permissions = voiceChannel.permissionsFor(messageCreate.client.user)
        if (!permissions.has("CONNECT")) return messageCreate.channel.send("You do not have the sufficient permissions to do this.")
        if (!permissions.has("SPEAK")) return messageCreate.channel.send("You do not have the sufficient permissions to do this.")
        if (!args.length) return messageCreate.channel.send("You must specify some keywords to identify the song you are looking for.")

        joinVoiceChannel({
            channelId: messageCreate.member.voice.channel.id,
            guildId: messageCreate.guild.id,
            adapterCreator: messageCreate.guild.voiceAdapterCreator
        })
        
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query)

            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null

        }
        
        const video = await videoFinder(args.join(" "))

        if (video) {
            const stream = ytdl(video.url, {filter: "audioonly"})
            const player = createAudioPlayer()
            const resource = createAudioResource(stream)

            async function play() {
                await player.play(resource)
                connection.subscribe(player)
            connection.play(stream, {seek: 0, volume: 1})
            .on("finish", () => {
                voiceChannel.leave()
            })
            }

            await messageCreate.reply(`:thumbsup: Now playing: ***${video.title}***.`)

        } else {
            messageCreate.channel.send("No video results found.")
        }
    }

}

我正在使用 VS 代碼 node.js 18.7.0 和 discord.js v13。 我真的需要這方面的幫助,所以任何事情都會受到贊賞。

據我所知,你從來沒有真正播放過視頻。 你定義了這個 function,'play',但從不調用它。

async function play() {
    await player.play(resource)
    connection.subscribe(player)
    connection.play(stream, {seek: 0, volume: 1})
        .on("finish", () => {
            voiceChannel.leave()
        })
}

據我所知,您正在嘗試制作異步 function 以便您可以使用await關鍵字,但這不是必需的。 只需將play器function的主體正常放入代碼,stream應該播放到頻道。

const ytdl = require("ytdl-core")
const ytSearch = require("yt-search")
const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource
} = require('@discordjs/voice')

module.exports = {
    name: "play",
    description: "Plays a song of your choice.",
    async execute(messageCreate, args) {
        const voiceChannel = messageCreate.member.voice.channel

        if (!voiceChannel) return messageCreate.channel.send("You must be in a voice channel to run this command.")
        const permissions = voiceChannel.permissionsFor(messageCreate.client.user)
        if (!permissions.has("CONNECT")) return messageCreate.channel.send("You do not have the sufficient permissions to do this.")
        if (!permissions.has("SPEAK")) return messageCreate.channel.send("You do not have the sufficient permissions to do this.")
        if (!args.length) return messageCreate.channel.send("You must specify some keywords to identify the song you are looking for.")

        joinVoiceChannel({
            channelId: messageCreate.member.voice.channel.id,
            guildId: messageCreate.guild.id,
            adapterCreator: messageCreate.guild.voiceAdapterCreator
        })
        
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query)

            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null

        }
        
        const video = await videoFinder(args.join(" "))

        if (video) {
            const stream = ytdl(video.url, {filter: "audioonly"})
            const player = createAudioPlayer()
            const resource = createAudioResource(stream)

            async function play() {
                await player.play(resource)
                connection.subscribe(player)
            connection.play(stream, {seek: 0, volume: 1})
            .on("finish", () => {
                voiceChannel.leave()
            })
            }

            await messageCreate.reply(`:thumbsup: Now playing: ***${video.title}***.`)

        } else {
            messageCreate.channel.send("No video results found.")
        }
    }

}

如果你真的想保留play function,你可以通過在定義它之后立即調用 function 來修復它。

async function play() {
    /*...*/
}
play() // Just add this line here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM