简体   繁体   中英

discord bot doesnt play music

I have a discord bot that im trying to make play music. 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. 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.

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. Just put the body of the play function into the code normally, and the stream should play to the channel.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM