#How to skip or stop - Music Bot
1 messages · Page 1 of 1 (latest)
Play.js
module.exports = {
name: "play",
description: "Plays music",
mod: false,
admin: true,
alias: ["skip", "stop"],
async execute(msg, args, discord) {
const ytdl = require('ytdl-core')
const ytsearch = require('yt-search')
const COLOURS = require('../terminal-colours.js')
msg.delete()
const voiceChannel = msg.member.voice.channel
if(!voiceChannel) {
console.log(`[ ${COLOURS.red}ERROR${COLOURS.reset} ] User is not in a voice channel.`)
msg.channel.send(`You must be in a voice channel to use this command.`)
return
}
const permissions = voiceChannel.permissionsFor(msg.client.user)
if(!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
console.log(`[ ${COLOURS.red}ERROR${COLOURS.reset} ] Bot does not have permissions for the voice channel.`)
msg.channel.send('I need the permissions to join and speak in your voice channel.')
return
}
if(args[0] === 'play') {
if(!args[1]) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] No song given.`)
msg.channel.send('Please specify a song to play.')
return
}
let song = {}
const validURL = (str) => {
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/
if(!regex.test(str)) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Invalid URL: ${str}`)
return false
} else {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Valid URL: ${str}`)
return true
}
}
const videoFinder = async (query) => {
const videoResult = await ytsearch(query)```
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null
}
const connection = await voiceChannel.join()
globalThis.connection = connection
const playSong = async (url) => {
const stream = ytdl(url, { filter: 'audioonly' })
const embed = new discord.MessageEmbed({
title: `${globalThis.songs[0].title}`,
description: `Requested by <@${globalThis.songs[0].requester}>`,
url: globalThis.songs[0].url,
author: {
name: globalThis.songs[0].author_name,
iconURL: globalThis.songs[0].author_image,
url: globalThis.songs[0].channel_url
},
image: {
url: globalThis.songs[0].thumbnail
}
})
msg.channel.send(embed)
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Playing "${globalThis.songs[0].title}" by "${globalThis.songs[0].author_name}"`)
globalThis.connection.play(stream, { seek: 0, volume: 1, quality: 'highestaudio', highWaterMark: 1 << 25 })
.on('finish', () => {
globalThis.playing = false
if(globalThis.songs.length > 0) globalThis.songs.shift()
if(globalThis.songs.length > 0) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Finished! Next song: "${globalThis.songs[0].title}" by "${globalThis.songs[0].author_name}"`)
playSong(globalThis.songs[0].url)
}
else {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Queue is empty.`)
msg.channel.send('Queue is empty.')
}
})```
.on('error', (err) => {
console.log(`[ ${COLOURS.red}ERROR${COLOURS.reset} ] Error occured playing the song: ${err}`)
msg.channel.send(`An error occured while playing the song.`)
globalThis.playing = false
voiceChannel.leave()
})
}
if(validURL(args[1])) {
let song_info
try {
song_info = await ytdl.getInfo(args[1])
} catch(err) {
console.log(`[ ${COLOURS.red}ERROR${COLOURS.reset} ] ${args[1]} is not a YouTube domain: ${err}`)
msg.channel.send(`Not a YouTube domain.`)
return
}
song = { title: song_info.videoDetails.title, url: song_info.videoDetails.video_url, requester: msg.author.id, author_name: song_info.videoDetails.author.name, author_image: song_info.videoDetails.author.thumbnails[0].url, channel_url: song_info.videoDetails.author.channel_url, thumbnail: song_info.videoDetails.thumbnails[0].url }
globalThis.songs.push(song)
if(globalThis.playing) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Song added to queue: ${song.title}`)
msg.channel.send(`Added **${song.title}** to the queue.`)
return
}
globalThis.playing = true
playSong(song_info.videoDetails.video_url)
}
else {
const video = await videoFinder(args.slice(1).join(' '))
const channel_info = await ytdl.getInfo(video.url)
if(video){
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Video found.`)```
song = { title: video.title, url: video.url, requester: msg.author.id, author_name: video.author.name, author_image: channel_info.videoDetails.author.thumbnails[0].url, channel_url: video.author.url, thumbnail: video.thumbnail }
globalThis.songs.push(song)
if(globalThis.playing) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Song added to queue: ${song.title}`)
msg.channel.send(`Added **${song.title}** to the queue.`)
return
}
globalThis.playing = true
playSong(video.url)
}
else {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Video not found.`)
msg.reply('I was unable to find that video.')
}
}
}
if(args[0] === 'skip') {
if(msg.author.id === globalThis.songs[0].requester) {
if(!globalThis.playing) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] No song playing.`)
msg.channel.send('There is no song playing.')
return
}
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Song skipped.`)
globalThis.connection.dispatcher.end()
msg.channel.send('Skipped the current song.')
}
else {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] User is not the requester.`)
msg.channel.send('You are not the requester of the current song.')
}
}
if(args[0] === 'stop') {
if(!globalThis.playing) {
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] No song playing.`)
msg.channel.send('There is no song playing.')```
return
}
globalThis.songs = []
globalThis.connection.dispatcher.end()
console.log(`[ ${COLOURS.cyan}Music${COLOURS.reset} ] Stopped the song.`)
msg.channel.send('Stopped the current song.')
}
}
}
Terminal:
[ Init ] Starting . . .
[ Init ] Loading events . . .
[ Init ] Loaded event: guildMemberAdd
[ Init ] Loaded event: message
[ Init ] Loaded event: ready
[ Init ] Loading counters . . .
[ Init ] Loaded counter: total-members
[ READY ] NAME=DamianBossUtilities#7452 | ID=783249439137333259
[ COMMAND ] DamianBossPL#7317 | play https://www.youtube.com/watch?v=7FEuZYRx41M
[ Music ] Valid URL: https://www.youtube.com/watch?v=7FEuZYRx41M
[ Music ] Playing "Google Translate becomes human (Fan Voiced Special)" by "Random Týpek"
[ COMMAND ] DamianBossPL#7317 | play scatman
[ Music ] Invalid URL: scatman
[ Music ] Video found.
[ Music ] Song added to queue: Scatman (ski-ba-bop-ba-dop-bop) Official Video HD - Scatman John
[ COMMAND ] DamianBossPL#7317 | skip
[ Music ] Song skipped.
[ COMMAND ] DamianBossPL#7317 | stop
[ Music ] Stopped the song.
#archive-voice is probably better
Also the actual issue would be useful, too much code there.