Files
play-dl-test/examples/SoundCloud/play.js

54 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-01-24 18:31:00 -06:00
const { Intents, Client } = require('discord.js')
2021-09-21 13:00:38 +05:30
const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice')
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-09-21 13:00:38 +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-09-21 13:00:38 +05:30
const connection = joinVoiceChannel({
2022-01-24 18:31:00 -06:00
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
2021-09-21 13:00:38 +05:30
adapterCreator: message.guild.voiceAdapterCreator
})
2022-01-24 18:31:00 -06:00
let args = message.content.split('play ')[1].split(' ')[0]
2021-09-21 13:00:38 +05:30
let stream = await play.stream(args)
2022-01-24 18:31:00 -06:00
2021-09-21 13:00:38 +05:30
/*
OR if you want to get info about soundcloud link and then stream it
let so_info = await play.soundcloud(args) // Make sure that url is track url only. For playlist, make some logic.
console.log(so_info.name)
let stream = await play.stream_from_info(so_info)
*/
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-09-21 13:00:38 +05:30
})
2022-01-24 18:31:00 -06:00
2021-09-21 13:00:38 +05:30
let player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Play
}
})
2022-01-24 18:31:00 -06:00
2021-09-21 13:00:38 +05:30
player.play(resource)
connection.subscribe(player)
2022-01-24 18:31:00 -06:00
}
2021-09-21 13:00:38 +05:30
})
client.on('ready', () => {
console.log(`We have logged in as ${client.user.tag}!`)
})
client.login(token);