2023-01-01 19:49:51 +00:00
import {
Bot ,
editOriginalInteractionResponse ,
Interaction ,
sendInteractionResponse ,
type ApplicationCommandOption ,
type CreateSlashApplicationCommand
} from "../deps.ts" ;
2023-03-23 04:30:56 +00:00
import { YouTube } from "../discordeno-audio-plugin/deps.ts" ;
2023-01-01 19:49:51 +00:00
2023-03-23 04:30:56 +00:00
import { ensureVoiceConnection , formatCallbackData , isPlaylist , waitingForResponse } from "../utils.ts" ;
async function addedPlaylistResponse ( interaction : Interaction , url : string ) {
const playlist = await YouTube . getPlaylist ( url ) ;
return formatCallbackData ( ` ${ interaction . user . username } added ${ playlist . videoCount } videos from [** ${ playlist . title } **]( ${ interaction ! . data ! . options ! [ 0 ] . value } ) to the queue. ` ,
2023-03-23 04:38:31 +00:00
"Added playlist" ) ;
2023-03-23 04:30:56 +00:00
}
function addedSongResponse ( interaction : Interaction , title : string ) {
2023-03-23 04:38:31 +00:00
return formatCallbackData ( ` ${ interaction . user . username } added [** ${ title } **]( ${ interaction ! . data ! . options ! [ 0 ] . value } ) to the queue. ` , "Added song" ) ;
2023-01-01 19:49:51 +00:00
}
function alreadyPlayingResponse ( bot : Bot , interaction : Interaction ) {
const player = bot . helpers . getPlayer ( interaction . guildId ) ;
return formatCallbackData ( ` The bot is already playing.
Currently playing : * * $ { player . nowPlaying . title } * * , added by $ { player . nowPlaying . added_by } ` );
}
2023-01-17 22:09:21 +00:00
const badUrlResponse = formatCallbackData ( ` Bad URL, please enter a URL that starts with https://youtube.com or https://youtu.be. ` ) ;
2023-01-01 19:49:51 +00:00
const emptyQueueResponse = formatCallbackData ( ` There's nothing in the queue to play right now. ` ) ;
function nowPlayingResponse ( bot : Bot , interaction : Interaction ) {
const player = bot . helpers . getPlayer ( interaction . guildId ) ;
return formatCallbackData ( ` The bot has started playing again.
Currently playing : * * $ { player . nowPlaying . title } * * , added by $ { player . nowPlaying . added_by } ` );
}
export const playCommand = < CreateSlashApplicationCommand > {
name : "play" ,
description : "Adds a song or playlist to the queue and starts the music if it's not already playing" ,
dmPermission : false ,
options : [
< ApplicationCommandOption > {
type : 3 , // string
name : "url" ,
description : "The URL or video ID of the song or playlist to play" ,
required : false
}
]
} ;
2023-03-19 04:55:36 +00:00
export async function play ( bot : Bot , interaction : Interaction ) {
2023-01-01 19:49:51 +00:00
if ( ! interaction . guildId ) return ;
await ensureVoiceConnection ( bot , interaction . guildId ) ;
const player = bot . helpers . getPlayer ( interaction . guildId ) ;
await sendInteractionResponse ( bot , interaction . id , interaction . token , waitingForResponse ) ;
2023-01-17 22:09:21 +00:00
let parsed_url ;
2023-03-19 04:55:36 +00:00
if ( ! interaction ) return ;
if ( ! interaction . data ) return ;
if ( interaction . data . options ) {
if ( ! interaction . data . options [ 0 ] . value ) return ;
2023-01-17 22:09:21 +00:00
try {
2023-03-19 04:55:36 +00:00
parsed_url = new URL ( interaction . data . options [ 0 ] . value . toString ( ) ) ;
2023-01-17 22:09:21 +00:00
} catch {
await editOriginalInteractionResponse ( bot , interaction . token , badUrlResponse ) ;
}
2023-03-19 04:55:36 +00:00
if ( ! parsed_url ) return ;
2023-01-01 19:49:51 +00:00
let href ;
2023-02-10 18:56:14 +00:00
// remove the timestamp from the query
2023-01-01 19:49:51 +00:00
if ( parsed_url . href . indexOf ( "?t=" ) !== - 1 ) {
href = parsed_url . href . substring ( 0 , parsed_url . href . indexOf ( "?" ) )
} else {
href = parsed_url . href ;
}
2023-01-17 22:09:21 +00:00
2023-02-10 18:56:14 +00:00
const result = await player . pushQuery ( interaction . guildId , interaction . user . username , href ) ;
2023-01-17 22:09:21 +00:00
if ( result && result [ 0 ] && parsed_url . href . indexOf ( "youtube.com" ) !== - 1 || parsed_url . href . indexOf ( "youtu.be" ) !== - 1 && result [ 0 ] . title ) {
2023-03-23 04:30:56 +00:00
if ( isPlaylist ( parsed_url . href ) )
{
await editOriginalInteractionResponse ( bot , interaction . token , await addedPlaylistResponse ( interaction , parsed_url . href ) ) ;
} else {
await editOriginalInteractionResponse ( bot , interaction . token , addedSongResponse ( interaction , result [ 0 ] . title ) ) ;
}
2023-01-01 19:49:51 +00:00
}
} else {
// restart the player if there's no url
if ( player . waiting || ! player . playing ) {
if ( player . nowPlaying ) {
await player . play ( ) ;
await editOriginalInteractionResponse ( bot , interaction . token , nowPlayingResponse ( bot , interaction ) ) ;
} else {
await editOriginalInteractionResponse ( bot , interaction . token , emptyQueueResponse ) ;
}
} else {
await editOriginalInteractionResponse ( bot , interaction . token , alreadyPlayingResponse ( bot , interaction ) ) ;
}
}
}