From 972a407fd21364a3b80c598a1d32405d54e67412 Mon Sep 17 00:00:00 2001 From: duncte123 Date: Mon, 2 Sep 2024 19:55:38 +0200 Subject: [PATCH] Update some music command executions --- .../me/duncte123/skybot/CommandManager.java | 2 +- .../skybot/objects/command/MusicCommand.java | 57 +++++++++++++++++-- .../skybot/commands/music/ForceSkip.kt | 43 +++++++++++++- .../skybot/commands/music/LeaveCommand.kt | 2 - .../skybot/commands/music/PPlayCommand.kt | 26 +-------- 5 files changed, 97 insertions(+), 33 deletions(-) diff --git a/bot/src/main/java/me/duncte123/skybot/CommandManager.java b/bot/src/main/java/me/duncte123/skybot/CommandManager.java index 4b0201d64..7f4b68f0d 100644 --- a/bot/src/main/java/me/duncte123/skybot/CommandManager.java +++ b/bot/src/main/java/me/duncte123/skybot/CommandManager.java @@ -764,7 +764,7 @@ public void executeSlashCommand(SlashCommandInteractionEvent event) { final MusicCommand command = (MusicCommand) this.getCommand(musicName); if (command != null) { - command.handleEvent(event, guild, variables); + command.handleSlashWithAutoJoin(event, guild, variables); } return; diff --git a/bot/src/main/java/me/duncte123/skybot/objects/command/MusicCommand.java b/bot/src/main/java/me/duncte123/skybot/objects/command/MusicCommand.java index 2ca26a6e9..8d55454f7 100644 --- a/bot/src/main/java/me/duncte123/skybot/objects/command/MusicCommand.java +++ b/bot/src/main/java/me/duncte123/skybot/objects/command/MusicCommand.java @@ -111,13 +111,45 @@ private boolean channelChecks(CommandContext ctx, AudioUtils audioUtils, String return true; } - private boolean isAbleToJoinChannel(CommandContext ctx) { - if (isUserOrGuildPatron(ctx, false)) { - return ctx.getMember().getVoiceState().inAudioChannel() && - !getLavalinkManager().isConnected(ctx.getGuild()); + private boolean canRunSlashCommand(SlashCommandInteractionEvent event, DunctebotGuild guild, AudioUtils audioUtils) { + if (!event.getMember().getVoiceState().inAudioChannel()) { + event.reply("Please join a voice channel first").setEphemeral(true).queue(); + return false; + } + + final LavalinkManager lavalinkManager = getLavalinkManager(); + + if (!lavalinkManager.isConnected(guild)) { + event.reply("I'm not in a voice channel, use `/join` to make me join a channel") + .setEphemeral(true) + .queue(); + + return false; + } + + final AudioChannelUnion connectedChannel = lavalinkManager.getConnectedChannel(guild); + + if (connectedChannel != null && !connectedChannel.getMembers().contains(event.getMember())) { + event.reply("I'm sorry, but you have to be in the same channel as me to use any music related commands") + .setEphemeral(true) + .queue(); + + return false; } - return false; + audioUtils.getMusicManager(guild.getIdLong()).setLatestChannelId(event.getChannel().getIdLong()); + return true; + } + + private boolean isAbleToJoinChannel(CommandContext ctx) { + // if (isUserOrGuildPatron(ctx, false)) { + // return ctx.getMember().getVoiceState().inAudioChannel() && + // !getLavalinkManager().isConnected(ctx.getGuild()); + // } + + // return false + return ctx.getMember().getVoiceState().inAudioChannel() && + !getLavalinkManager().isConnected(ctx.getGuild()); } protected static LavalinkManager getLavalinkManager() { @@ -129,6 +161,21 @@ protected SubcommandData getSubData() { return new SubcommandData(getName(), getHelp(getName(), "/")); } + public void handleSlashWithAutoJoin(@Nonnull SlashCommandInteractionEvent event, DunctebotGuild guild, @Nonnull Variables variables) { + if (this.mayAutoJoin) { + // TODO: this will cause issues with the event not working properly, need to find a way to resolve this + ((MusicCommand) variables.getCommandManager() + .getCommand("join")) + .handleEvent(event, guild, variables); + this.handleEvent(event, guild, variables); + return; + } + + if (this.justRunLmao || canRunSlashCommand(event, guild, variables.getAudioUtils())) { + this.handleEvent(event, guild, variables); + } + } + public abstract void handleEvent(@Nonnull SlashCommandInteractionEvent event, DunctebotGuild guild, @Nonnull Variables variables); public static SlashCommandData getMusicCommandData(CommandManager mngr) { diff --git a/bot/src/main/kotlin/me/duncte123/skybot/commands/music/ForceSkip.kt b/bot/src/main/kotlin/me/duncte123/skybot/commands/music/ForceSkip.kt index b9de899a2..18e878b5d 100644 --- a/bot/src/main/kotlin/me/duncte123/skybot/commands/music/ForceSkip.kt +++ b/bot/src/main/kotlin/me/duncte123/skybot/commands/music/ForceSkip.kt @@ -30,6 +30,7 @@ import net.dv8tion.jda.api.Permission 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 +import java.util.Optional import kotlin.jvm.optionals.getOrNull class ForceSkip : MusicCommand() { @@ -110,6 +111,46 @@ class ForceSkip : MusicCommand() { guild: DunctebotGuild, variables: Variables, ) { - // + val mng = variables.audioUtils.getMusicManager(guild.idLong) + + val player = mng.player.getOrNull() + val currTrack = player?.track + + if (currTrack == null) { + event.reply("The player is not playing.").queue() + return + } + + val skipCount = Optional.ofNullable(event.getOption("skip_count")) + .map { it.asInt } + .orElse(1) + val scheduler = mng.scheduler + + val trackData = scheduler.getUserData(currTrack) + + scheduler.skipTracks(skipCount, false) + + // Return the console user if the requester is null + val user = event.jda.getUserById(trackData.requester) ?: UnknownUser() + + val newTrack = player.track + + if (newTrack == null) { + event.reply( + "Successfully skipped $skipCount tracks.\n" + + "Queue is now empty." + ).queue() + return + } + + event.replyEmbeds( + EmbedUtils.embedMessage( + "Successfully skipped $skipCount tracks.\n" + + "Now playing: ${newTrack.info.title}\n" + + "Requester: ${user.asTag}" + ) + .setThumbnail(newTrack.info.artworkUrl) + .build() + ).queue() } } diff --git a/bot/src/main/kotlin/me/duncte123/skybot/commands/music/LeaveCommand.kt b/bot/src/main/kotlin/me/duncte123/skybot/commands/music/LeaveCommand.kt index 60cfc9b68..e36219783 100644 --- a/bot/src/main/kotlin/me/duncte123/skybot/commands/music/LeaveCommand.kt +++ b/bot/src/main/kotlin/me/duncte123/skybot/commands/music/LeaveCommand.kt @@ -55,8 +55,6 @@ class LeaveCommand : MusicCommand() { guild: DunctebotGuild, variables: Variables, ) { - val guild = event.guild!! - if (!getLavalinkManager().isConnected(guild)) { event.reply("I'm not connected to any channels.").queue() return diff --git a/bot/src/main/kotlin/me/duncte123/skybot/commands/music/PPlayCommand.kt b/bot/src/main/kotlin/me/duncte123/skybot/commands/music/PPlayCommand.kt index 22707286c..5caaa013f 100644 --- a/bot/src/main/kotlin/me/duncte123/skybot/commands/music/PPlayCommand.kt +++ b/bot/src/main/kotlin/me/duncte123/skybot/commands/music/PPlayCommand.kt @@ -18,7 +18,6 @@ package me.duncte123.skybot.commands.music -import me.duncte123.botcommons.messaging.MessageUtils.sendError import me.duncte123.botcommons.messaging.MessageUtils.sendMsg import me.duncte123.skybot.Variables import me.duncte123.skybot.entities.jda.DunctebotGuild @@ -35,28 +34,7 @@ class PPlayCommand : MusicCommand() { } override fun run(ctx: CommandContext) { - sendMsg(ctx, "Hey, this command will be going away soon. Please use the `/play` command instead.") - - if (ctx.args.isEmpty()) { - sendMsg(ctx, "To few arguments, use `${ctx.prefix}$name `") - return - } - - val toPlay = ctx.argsRaw - - if (toPlay.length > 1024) { - sendError(ctx.message) - sendMsg(ctx, "Input cannot be longer than 1024 characters.") - return - } - - sendMsg( - ctx, - "Loading playlist.......\n" + - "This may take a while depending on the size." - ) - - ctx.audioUtils.loadAndPlay(ctx.audioData, toPlay, true) + sendMsg(ctx, "This command has been removed. Please use the `/play` command instead.") } override fun handleEvent( @@ -64,6 +42,6 @@ class PPlayCommand : MusicCommand() { guild: DunctebotGuild, variables: Variables, ) { - event.reply("This command will be going away soon. Please use the `/play` command instead.").queue() + event.reply("This command has been removed. Please use the `/play` command instead.").queue() } }