Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

add a new command (playall) #597

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* MIT License
*
* Copyright (c) 2017 Frederik Ar. Mikkelsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package fredboat.command.music.control

import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import fredboat.audio.player.PlayerLimiter
import fredboat.audio.player.VideoSelectionCache
import fredboat.audio.queue.AudioTrackContext
import fredboat.commandmeta.abs.Command
import fredboat.commandmeta.abs.CommandContext
import fredboat.commandmeta.abs.ICommandRestricted
import fredboat.commandmeta.abs.IMusicCommand
import fredboat.definitions.PermissionLevel
import fredboat.definitions.SearchProvider
import fredboat.main.Launcher
import fredboat.messaging.internal.Context
import fredboat.shared.constant.BotConstants
import fredboat.util.TextUtils
import fredboat.util.extension.edit
import fredboat.util.rest.TrackSearcher
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory

class PlayAllCommand(private val playerLimiter: PlayerLimiter, private val trackSearcher: TrackSearcher,
private val videoSelectionCache: VideoSelectionCache, private val searchProviders: List<SearchProvider>,
name: String, vararg aliases: String, private val isPriority: Boolean = false
) : Command(name, *aliases), IMusicCommand, ICommandRestricted {

override val minimumPerms: PermissionLevel
get() = if (isPriority) PermissionLevel.DJ else PermissionLevel.USER

override suspend fun invoke(context: CommandContext) {
if (context.member.voiceChannel == null) {
context.reply(context.i18n("playerUserNotInChannel"))
return
}

if (!playerLimiter.checkLimitResponsive(context, Launcher.botController.playerRegistry)) return

if (!context.hasArguments()) {
context.reply(context.i18n("playAllSearchNotGiven"))
return
}

var url = StringUtils.strip(context.args[0], "<>")
//Search youtube for videos and play them directly
if (!url.startsWith("http") && !url.startsWith(FILE_PREFIX)) {
searchAndPlayForVideos(context)
return
}
}

private fun searchAndPlayForVideos(context: CommandContext) {
//Now remove all punctuation
val query = context.rawArgs.replace(TrackSearcher.PUNCTUATION_REGEX.toRegex(), "")

context.replyMono(context.i18n("playSearching").replace("{q}", query))
.subscribe{ outMsg ->
val list: AudioPlaylist?
try {
list = trackSearcher.searchForTracks(query, searchProviders)
} catch (e: TrackSearcher.SearchingException) {
context.reply(context.i18n("playYoutubeSearchError"))
log.error("YouTube search exception", e)
return@subscribe
}

if (list == null || list.tracks.isEmpty()) {
outMsg.edit(
context.textChannel,
context.i18n("playSearchNoResults").replace("{q}", query)
).subscribe()

} else {
//Get at most 5 tracks
val selectable = list.tracks.subList(0, Math.min(TrackSearcher.MAX_RESULTS, list.tracks.size))

val oldSelection = videoSelectionCache.remove(context.member)
oldSelection?.deleteMessage()

videoSelectionCache.put(outMsg.messageId, context, selectable, isPriority)

//Add musics in the queue
val player = Launcher.botController.playerRegistry.getOrCreate(context.guild)
val invoker = context.member
val selection = videoSelectionCache[invoker]
val selectedTracks = arrayOfNulls<AudioTrack>(TrackSearcher.MAX_RESULTS)
val outputMsgBuilder = StringBuilder()

if (selection == null) {
outMsg.edit(
context.textChannel,
context.i18n("playSearchNoResults").replace("{q}", query)
).subscribe()
} else {

for (i in 0 until TrackSearcher.MAX_RESULTS) {
selectedTracks[i] = selection.choices[i]

val msg = context.i18nFormat("selectSuccess", (i + 1),
TextUtils.escapeAndDefuse(selectedTracks[i]!!.info.title),
TextUtils.formatTime(selectedTracks[i]!!.info.length))

if (i < TrackSearcher.MAX_RESULTS) {
outputMsgBuilder.append("\n")
}
outputMsgBuilder.append(msg)

player.queue(AudioTrackContext(selectedTracks[i]!!, invoker, selection.isPriority), selection.isPriority)
}
videoSelectionCache.remove(invoker)

outMsg.edit(context.textChannel, outputMsgBuilder.toString()).subscribe()

player.setPause(false)
context.deleteMessage()
}
}
}
}

override fun help(context: Context): String {
val usage = "{0}{1} <search-term>\n#"
return usage + context.i18nFormat(if (!isPriority) "helpPlayAllCommand" else "helpPlayAllTopCommand", BotConstants.DOCS_URL)
}

companion object {

private val log = LoggerFactory.getLogger(PlayAllCommand::class.java)
private val JOIN_COMMAND = JoinCommand("")
private const val FILE_PREFIX = "file://"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class CommandInitializer(cacheMetrics: CacheMetricsCollector, weather: Weather,
const val SOUNDCLOUD_COMM_NAME = "soundcloud"
const val PREFIX_COMM_NAME = "prefix"
const val PLAY_COMM_NAME = "play"
const val PLAYALL_COMM_NAME = "playall"
const val CONFIG_COMM_NAME = "config"
const val LANGUAGE_COMM_NAME = "language"
}
Expand Down Expand Up @@ -240,6 +241,12 @@ class CommandInitializer(cacheMetrics: CacheMetricsCollector, weather: Weather,
musicModule.registerCommand(PlayCommand(playerLimiter, trackSearcher, videoSelectionCache,
listOf(SearchProvider.YOUTUBE, SearchProvider.SOUNDCLOUD),
"playnext", "playtop", "pn", isPriority = true))
musicModule.registerCommand(PlayAllCommand(playerLimiter, trackSearcher, videoSelectionCache,
Arrays.asList(SearchProvider.YOUTUBE, SearchProvider.SOUNDCLOUD),
PLAYALL_COMM_NAME, "pa"))
musicModule.registerCommand(PlayAllCommand(playerLimiter, trackSearcher, videoSelectionCache,
Arrays.asList(SearchProvider.YOUTUBE, SearchProvider.SOUNDCLOUD),
"playalltop", "patop", isPriority = true))
musicModule.registerCommand(PlaySplitCommand(playerLimiter, "split"))
musicModule.registerCommand(RepeatCommand("repeat", "rep", "loop"))
musicModule.registerCommand(ReshuffleCommand("reshuffle", "resh"))
Expand Down
3 changes: 3 additions & 0 deletions FredBoat/src/main/resources/lang/en_US.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#X-Generator: crowdin.com
playQueueEmpty=The player is not currently playing anything. Use the following syntax to add a song\:\n;;play <url-or-search-terms>
playAlreadyPlaying=The player is already playing.
playAllSearchNotGiven=Don't forget to give a search track.
playVCEmpty=There are no users in the voice chat.
playWillNowPlay=The player will now play.
playSearching=Searching YouTube for `{q}`...
Expand Down Expand Up @@ -242,6 +243,8 @@ helpPauseCommand=Pause the player.
helpPlayCommand=Play music from the given URL or search for a track. For a full list of sources please visit {0}
helpPlayNextCommand=Play music from the given URL or search for a track. Tracks are added to the top of the queue. For a full list of sources please visit {0}
helpPlaySplitCommand=Split a YouTube video into a tracklist provided in it\'s description.
helpPlayAllCommand=Play all musics from your tracks search. For a full list of sources please visit {0}
helpPlayAllTopCommand=Play all musics from your tracks search. Tracks are added to the top of the queue. For a full list of sources please visit {0}
helpRepeatCommand=Toggle between repeat modes.
helpReshuffleCommand=Reshuffle the current queue.
helpSelectCommand=Select one of the offered tracks after a search to play.
Expand Down