From 584dc1d7fa0e74d145130f32e9d3435285f70a45 Mon Sep 17 00:00:00 2001 From: DarkInk Date: Mon, 24 Jan 2022 18:31:00 -0600 Subject: [PATCH] Cleaning the damn examples up --- examples/SoundCloud/play.js | 32 +++++++++++++---------- examples/Spotify/play.js | 39 +++++++++++++++++----------- examples/YouTube/play - search.js | 42 +++++++++++++++++++------------ examples/YouTube/play - url.js | 33 ++++++++++++++---------- 4 files changed, 89 insertions(+), 57 deletions(-) diff --git a/examples/SoundCloud/play.js b/examples/SoundCloud/play.js index d732b2b..d1d24c7 100644 --- a/examples/SoundCloud/play.js +++ b/examples/SoundCloud/play.js @@ -1,22 +1,27 @@ -const discord = require('discord.js') -const { Intents } = require('discord.js') +const { Intents, Client } = require('discord.js') const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice') const play = require('play-dl') -const client = new discord.Client({ intents : [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES] , partials : ['CHANNEL', 'MESSAGE']}) +const client = new Client({ + intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES], + partials: ['CHANNEL', 'MESSAGE'] +}) const token = '< YOUR BOT TOKEN >' - client.on('messageCreate', async message => { - if(message.content.startsWith('!play')){ - if(!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + if (message.content.startsWith('!play')) { + + if (!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + const connection = joinVoiceChannel({ - channelId : message.member.voice.channel.id, - guildId : message.guild.id, + channelId: message.member.voice.channel.id, + guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator }) - - let args = message.content.split('play ')[1].split(' ')[0] + + let args = message.content.split('play ')[1].split(' ')[0] + let stream = await play.stream(args) + /* OR if you want to get info about soundcloud link and then stream it @@ -26,17 +31,19 @@ client.on('messageCreate', async message => { */ let resource = createAudioResource(stream.stream, { - inputType : stream.type + inputType: stream.type }) + let player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } }) + player.play(resource) connection.subscribe(player) - } + } }) client.on('ready', () => { @@ -44,4 +51,3 @@ client.on('ready', () => { }) client.login(token); - diff --git a/examples/Spotify/play.js b/examples/Spotify/play.js index 9af001b..823d9f9 100644 --- a/examples/Spotify/play.js +++ b/examples/Spotify/play.js @@ -1,29 +1,39 @@ -const discord = require('discord.js') -const { Intents } = require('discord.js') +const { Intents, Client } = require('discord.js') const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice') const play = require('play-dl') -const client = new discord.Client({ intents : [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES] , partials : ['CHANNEL', 'MESSAGE']}) +const client = new Client({ + intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES], + partials: ['CHANNEL', 'MESSAGE'] +}) const token = '< Your Bot Token >' - client.on('messageCreate', async message => { - if(message.content.startsWith('!play')){ - if(!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + if (message.content.startsWith('!play')) { + + if (!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + const connection = joinVoiceChannel({ - channelId : message.member.voice.channel.id, - guildId : message.guild.id, + channelId: message.member.voice.channel.id, + guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator }) - if(play.is_expired()){ + + if (play.is_expired()) { await play.refreshToken() // This will check if access token has expired or not. If yes, then refresh the token. } - let args = message.content.split('play ')[1].split(' ')[0] + + let args = message.content.split('play ')[1].split(' ')[0] + let sp_data = await play.spotify(args) // This will get spotify data from the url [ I used track url, make sure to make a logic for playlist, album ] - let searched = await play.search(`${sp_data.name}`, { limit : 1 }) // This will search the found track on youtube. + + let searched = await play.search(`${sp_data.name}`, { + limit: 1 + }) // This will search the found track on youtube. + let stream = await play.stream(searched[0].url) // This will create stream from the above search let resource = createAudioResource(stream.stream, { - inputType : stream.type + inputType: stream.type }) let player = createAudioPlayer({ behaviors: { @@ -33,12 +43,11 @@ client.on('messageCreate', async message => { player.play(resource) connection.subscribe(player) - } + } }) client.on('ready', () => { console.log(`We have logged in as ${client.user.tag}!`) }) -client.login(token); - +client.login(token); \ No newline at end of file diff --git a/examples/YouTube/play - search.js b/examples/YouTube/play - search.js index cfcf36c..941ed98 100644 --- a/examples/YouTube/play - search.js +++ b/examples/YouTube/play - search.js @@ -1,35 +1,46 @@ -const discord = require('discord.js') -const { Intents } = require('discord.js') +const { Intents, Client } = require('discord.js') const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice') const play = require('play-dl') -const client = new discord.Client({ intents : [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES] , partials : ['CHANNEL', 'MESSAGE']}) +const client = new Client({ + intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES], + partials: ['CHANNEL', 'MESSAGE'] +}) const token = '< YOUR BOT TOKEN >' - client.on('messageCreate', async message => { - if(message.content.startsWith('!play')){ - if(!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + + if (message.content.startsWith('!play')) { + + if (!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + const connection = joinVoiceChannel({ - channelId : message.member.voice.channel.id, - guildId : message.guild.id, + channelId: message.member.voice.channel.id, + guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator }) - - 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) - let resource = createAudioResource(stream.stream, { - inputType : stream.type + + + 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) + + let resource = createAudioResource(stream.stream, { + inputType: stream.type + }) + let player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } }) + player.play(resource) connection.subscribe(player) - } + } }) client.on('ready', () => { @@ -37,4 +48,3 @@ client.on('ready', () => { }) client.login(token); - diff --git a/examples/YouTube/play - url.js b/examples/YouTube/play - url.js index 730f311..93ae547 100644 --- a/examples/YouTube/play - url.js +++ b/examples/YouTube/play - url.js @@ -1,22 +1,28 @@ -const discord = require('discord.js') -const { Intents } = require('discord.js') +const { Intents, Client } = require('discord.js') const { createAudioPlayer, createAudioResource , StreamType, demuxProbe, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice') const play = require('play-dl') -const client = new discord.Client({ intents : [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES] , partials : ['CHANNEL', 'MESSAGE']}) +const client = new Client({ + intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.DIRECT_MESSAGES], + partials: ['CHANNEL', 'MESSAGE'] +}) const token = '< YOUR BOT TOKEN >' - client.on('messageCreate', async message => { - if(message.content.startsWith('!play')){ - if(!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + + if (message.content.startsWith('!play')) { + + if (!message.member.voice?.channel) return message.channel.send('Connect to a Voice Channel') + const connection = joinVoiceChannel({ - channelId : message.member.voice.channel.id, - guildId : message.guild.id, + channelId: message.member.voice.channel.id, + guildId: message.guild.id, adapterCreator: message.guild.voiceAdapterCreator }) - - let args = message.content.split('play ')[1].split(' ')[0] + + let args = message.content.split('play ')[1].split(' ')[0] + let stream = await play.stream(args) + /* OR if you want to get info about youtube link and then stream it @@ -26,17 +32,19 @@ client.on('messageCreate', async message => { */ let resource = createAudioResource(stream.stream, { - inputType : stream.type + inputType: stream.type }) + let player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } }) + player.play(resource) connection.subscribe(player) - } + } }) client.on('ready', () => { @@ -44,4 +52,3 @@ client.on('ready', () => { }) client.login(token); -