Files
play-dl-test/examples/YouTube/play - search.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-01-24 18:31:00 -06:00
const { Intents, Client } = require('discord.js')
2021-08-22 12:20:21 +05:30
const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice')
2021-09-05 18:50:57 +05:30
const play = require('play-dl')
2022-01-24 18:31:00 -06:00
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES],
partials: ['CHANNEL', 'MESSAGE']
})
2021-08-22 12:20:21 +05:30
const token = '< YOUR BOT TOKEN >'
client.on('messageCreate', async message => {
2022-01-24 18:31:00 -06:00
if (message.content.startsWith('!play')) {
if (!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel')
2021-08-22 12:20:21 +05:30
const connection = joinVoiceChannel({
2022-01-24 18:31:00 -06:00
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
2021-08-22 12:20:21 +05:30
adapterCreator: message.guild.voiceAdapterCreator
})
2022-01-24 18:31:00 -06:00
let args = message.content.split('play')[1]
let yt_info = await play.search(args, {
limit: 1
})
let stream = await play.stream(yt_info[0].url)
2021-09-21 22:04:45 +05:30
let resource = createAudioResource(stream.stream, {
2022-01-24 18:31:00 -06:00
inputType: stream.type
2021-08-22 12:20:21 +05:30
})
2022-01-24 18:31:00 -06:00
2021-08-22 12:20:21 +05:30
let player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play
}
})
2022-01-24 18:31:00 -06:00
2021-08-22 12:20:21 +05:30
player.play(resource)
connection.subscribe(player)
2022-01-24 18:31:00 -06:00
}
2021-08-22 12:20:21 +05:30
})
client.on('ready', () => {
console.log(`We have logged in as ${client.user.tag}!`)
})
client.login(token);