Skip to content

Commit

Permalink
Start on slash commands for music
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Jul 17, 2023
1 parent c095382 commit d9d861a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
10 changes: 8 additions & 2 deletions bot/src/main/java/ml/duncte123/skybot/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,17 @@ public List<SlashCommandData> getAllSlashCommands() {
})
.toList();*/

return this.commands.values().stream()
final List<SlashCommandData> commands = this.commands.values()
.stream()
.filter((cmd) -> cmd instanceof SlashSupport)
.map((cmd) -> (SlashSupport) cmd)
.map(SlashSupport::getSlashData)
.toList();
.collect(Collectors.toList());

// Manually register some commands
commands.add(MusicCommand.getMusicCommandData(this));

return commands;
}

public void executeSlashCommand(SlashCommandInteractionEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ private record SettingData(String help, SettingsConsumer handler, boolean hasSet
}
}

// TODO: make this a slash command
public SettingsCommand() {
this.displayAliasesInHelp = true;
// Override category here to make sure that we can hide all the other settings commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
package ml.duncte123.skybot.objects.command;

import fredboat.audio.player.LavalinkManager;
import ml.duncte123.skybot.CommandManager;
import ml.duncte123.skybot.Variables;
import ml.duncte123.skybot.objects.CooldownScope;
import ml.duncte123.skybot.utils.AudioUtils;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;

import javax.annotation.Nonnull;
import java.util.function.Function;
Expand Down Expand Up @@ -116,4 +122,24 @@ private boolean isAbleToJoinChannel(CommandContext ctx) {
protected static LavalinkManager getLavalinkManager() {
return LavalinkManager.INS;
}

@Nonnull
protected SubcommandData getSubData() {
return new SubcommandData(getName(), getHelp(getName(), "/"));
}

public void handleEvent(@Nonnull SlashCommandInteractionEvent event, @Nonnull Variables variables) {
//
}

public static SlashCommandData getMusicCommandData(CommandManager mngr) {
final var base = Commands.slash("music", "base command for music commands")
.setGuildOnly(true);

mngr.getCommands(CommandCategory.MUSIC).forEach((cmd) -> base.addSubcommands(
((MusicCommand) cmd).getSubData()
));

return base;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ package ml.duncte123.skybot.commands.music
import com.dunctebot.sourcemanagers.pornhub.PornHubAudioSourceManager
import me.duncte123.botcommons.messaging.MessageUtils
import me.duncte123.botcommons.messaging.MessageUtils.sendMsg
import ml.duncte123.skybot.Variables
import ml.duncte123.skybot.extensions.isNSFW
import ml.duncte123.skybot.objects.command.CommandContext
import ml.duncte123.skybot.objects.command.MusicCommand
import ml.duncte123.skybot.utils.AirUtils
import ml.duncte123.skybot.utils.CommandUtils
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.OptionType
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData

open class PlayCommand(private val skipParsing: Boolean = false) : MusicCommand() {
private val acceptedExtensions = listOf("wav", "mkv", "mp4", "flac", "ogg", "mp3", "aac", "ts")
Expand Down Expand Up @@ -79,7 +84,7 @@ open class PlayCommand(private val skipParsing: Boolean = false) : MusicCommand(
}

if (!AirUtils.isURL(toPlay) && !toPlay.startsWith("OCR", true)) {
val vidId = searchYt(toPlay, ctx)
val vidId = searchYt(toPlay, ctx.variables)

if (vidId == null) {
MessageUtils.sendError(ctx.message)
Expand Down Expand Up @@ -112,8 +117,8 @@ open class PlayCommand(private val skipParsing: Boolean = false) : MusicCommand(
return true
}

private fun searchYt(search: String, ctx: CommandContext): String? {
val playlist = ctx.audioUtils.searchYoutube(search)
private fun searchYt(search: String, variables: Variables): String? {
val playlist = variables.audioUtils.searchYoutube(search)

if (playlist == null || playlist.tracks.isEmpty()) {
return null
Expand All @@ -131,4 +136,53 @@ open class PlayCommand(private val skipParsing: Boolean = false) : MusicCommand(

ctx.audioUtils.loadAndPlay(ctx, toPlay, true)
}

private fun handlePlay(toPlay: String, variables: Variables, event: SlashCommandInteractionEvent) {
if (toPlay.length > 1024) {
event.reply("Input cannot be longer than 1024 characters.").queue()
return
}

event.deferReply().queue()

// TODO: get rid of CTX
// variables.audioUtils.loadAndPlay(ctx, toPlay, true)
}

override fun getSubData(): SubcommandData {
return super.getSubData()
.addOption(
OptionType.STRING,
"item",
"A url or a search term to play.",
true
)
}

override fun handleEvent(event: SlashCommandInteractionEvent, variables: Variables) {
var toPlay = event.getOption("item")!!.asString

if (toPlay.contains(PornHubAudioSourceManager.DOMAIN_REGEX.toRegex()) && !event.channel.isNSFW) {
event.reply("Because of thumbnails being loaded you can only use PornHub links in channels that are marked as NSFW").queue()
return
}

if (skipParsing) {
handlePlay(toPlay, variables, event) // TODO
return
}

if (!AirUtils.isURL(toPlay) && !toPlay.startsWith("OCR", true)) {
val vidId = searchYt(toPlay, variables)

if (vidId == null) {
event.reply("No tracks were found").queue()
return
}

toPlay = "https://www.youtube.com/watch?v=$vidId"
}

handlePlay(toPlay, variables, event)
}
}
31 changes: 31 additions & 0 deletions bot/src/main/kotlin/ml/duncte123/skybot/extensions/channel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Skybot, a multipurpose discord bot
* Copyright (C) 2017 Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package ml.duncte123.skybot.extensions

import net.dv8tion.jda.api.entities.channel.attribute.IAgeRestrictedChannel
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion

val MessageChannelUnion.isNSFW: Boolean
get() {
if (this is IAgeRestrictedChannel) {
return this.isNSFW
}

return false
}

0 comments on commit d9d861a

Please sign in to comment.