简体   繁体   English

discord 机器人不播放音乐

[英]discord bot doesnt play music

I have a discord bot that im trying to make play music.我有一个 discord 机器人,我正在尝试制作播放音乐。 It will join the voice channel but it wont play anything.它将加入语音频道,但不会播放任何内容。 I've tried lots of things but nothing seems to be working.我已经尝试了很多东西,但似乎没有任何效果。 Here is the code:这是代码:

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.")
        }
    }

}

I am using VS Code node.js 18.7.0 and discord.js v13.我正在使用 VS 代码 node.js 18.7.0 和 discord.js v13。 I really need help with this so anything would be appreciated.我真的需要这方面的帮助,所以任何事情都会受到赞赏。

From what I can see, you're never actually playing the video.据我所知,你从来没有真正播放过视频。 You define this function, 'play,' but never call it.你定义了这个 function,'play',但从不调用它。

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

From what I can tell, you're trying to make an async function so you can use the await keyword, but this isn't necessary.据我所知,您正在尝试制作异步 function 以便您可以使用await关键字,但这不是必需的。 Just put the body of the play function into the code normally, and the stream should play to the channel.只需将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.")
        }
    }

}

If you really want to keep the play function, you can fix it by calling the function right after defining it.如果你真的想保留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