Skip to content

Commit

Permalink
Update some music command executions
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Sep 2, 2024
1 parent 683de21 commit 972a407
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bot/src/main/java/me/duncte123/skybot/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,35 +34,14 @@ 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 <media link>`")
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(
event: SlashCommandInteractionEvent,
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()
}
}

0 comments on commit 972a407

Please sign in to comment.