fix: changed the defer method to hopefully fix the "app did not respond" issue

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-04-01 16:57:51 +02:00
parent 3395d53bf6
commit fc922206bb
10 changed files with 78 additions and 63 deletions

View file

@ -35,6 +35,8 @@ import dev.kord.rest.builder.interaction.integer
import dev.kord.rest.builder.interaction.string
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.kord.lavakord
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
object Bot {
//The kord object gets set at app launch
@ -60,7 +62,10 @@ object Bot {
//Register Command Listener
kord.on<GuildChatInputCommandInteractionCreateEvent> {
val response = interaction.deferPublicResponse()
//val response = interaction.deferPublicResponse()
coroutineScope {
val deferred = async { interaction.deferPublicResponse() }
val command = interaction.command
Logger.out("Command /${command.rootName} with ${command.options.size} Option${if (command.options.size == 1) "" else "s"}")
for (c in SlashCommandManager.commands) {
@ -84,14 +89,15 @@ object Bot {
}
}
}
c.onSlashCommand(interaction, response, opt)
return@on
c.onSlashCommand(interaction, deferred, opt)
return@coroutineScope
}
MessageUtil.sendEmbedForInteraction(
interaction, response, "Command not found", "Could not find the command, which you just ran.\n" +
interaction, deferred, "Command not found", "Could not find the command, which you just ran.\n" +
"This is 110%ly an error. Please report it to limited_dev#7441."
)
}
}
//Register Slash Commands at Discord
kord.createGlobalApplicationCommands {

View file

@ -22,12 +22,13 @@ package de.limited_dev.botendo.commands.slash.component
import de.limited_dev.botendo.commands.slash.component.options.CommandOption
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import kotlinx.coroutines.Deferred
abstract class SlashCommand(val name: String, val description: String, val options: List<CommandOption>? = null) {
open suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
}

View file

@ -28,11 +28,12 @@ import de.limited_dev.botendo.util.UrlUtil
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import dev.schlaubi.lavakord.audio.Link
import kotlinx.coroutines.Deferred
class NowPlayingCommand : SlashCommand("nowplaying", "Show what's currently playing", null) {
override suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
val guildId = interaction.guildId
@ -41,7 +42,7 @@ class NowPlayingCommand : SlashCommand("nowplaying", "Show what's currently play
if (link.state != Link.State.CONNECTED) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Not connected",
"I'm not in a VC and therefore, I am not playing anything."
)
@ -51,7 +52,7 @@ class NowPlayingCommand : SlashCommand("nowplaying", "Show what's currently play
if (track == null) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Not Playing",
"I'm not playing anything currently"
)
@ -59,7 +60,7 @@ class NowPlayingCommand : SlashCommand("nowplaying", "Show what's currently play
}
MessageUtil.sendEmbedForInteractionWithImage(
interaction,
response,
deferred,
"Currently playing",
"**${track.title}**\n*Now Playing*\nby ${track.author} :: ${
TimeUtil.getTimeFormatedRaw(

View file

@ -28,6 +28,7 @@ import de.limited_dev.botendo.util.MessageUtil
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import dev.schlaubi.lavakord.audio.Link
import kotlinx.coroutines.Deferred
class PlayCommand : SlashCommand(
"play",
@ -36,7 +37,7 @@ class PlayCommand : SlashCommand(
) {
override suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
val guildId = interaction.guildId
@ -46,7 +47,7 @@ class PlayCommand : SlashCommand(
if (voiceState == null) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"You are not connected to a VC",
"Please connect to a VC"
)
@ -60,7 +61,7 @@ class PlayCommand : SlashCommand(
} else if (link.state == Link.State.CONNECTED && link.lastChannelId != channelId!!.value) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"You are not in my VC",
"We are not in the same VC and therefore, you cannot play any music"
)
@ -74,6 +75,6 @@ class PlayCommand : SlashCommand(
"ytsearch:$query"
}
MusicManager.addToQueue(interaction, response, link, search)
MusicManager.addToQueue(interaction, deferred, link, search)
}
}

View file

@ -28,11 +28,12 @@ import de.limited_dev.botendo.util.TimeUtil
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import dev.schlaubi.lavakord.audio.Link
import kotlinx.coroutines.Deferred
class QueueCommand : SlashCommand("queue", "Show whats up next", null) {
override suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
val guildId = interaction.guildId
@ -41,7 +42,7 @@ class QueueCommand : SlashCommand("queue", "Show whats up next", null) {
if (link.state != Link.State.CONNECTED) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Not connected",
"I'm not in a VC and therefore, I am not playing anything."
)
@ -51,7 +52,7 @@ class QueueCommand : SlashCommand("queue", "Show whats up next", null) {
if (track == null) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Queue empty",
"I'm not playing anything currently"
)
@ -66,6 +67,6 @@ class QueueCommand : SlashCommand("queue", "Show whats up next", null) {
continue
desc += tr.info.title + " - " + TimeUtil.getTimeFormatedShortend(tr.info.length) + " (" + tr.info.author + ")\n"
}
MessageUtil.sendEmbedForInteraction(interaction, response, "Queue", desc)
MessageUtil.sendEmbedForInteraction(interaction, deferred, "Queue", desc)
}
}

View file

@ -29,11 +29,12 @@ import de.limited_dev.botendo.util.UrlUtil
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import dev.schlaubi.lavakord.audio.Link
import kotlinx.coroutines.Deferred
class SkipCommand : SlashCommand("skip", "Skip to the next song in queue", null) {
override suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
val guildId = interaction.guildId
@ -43,7 +44,7 @@ class SkipCommand : SlashCommand("skip", "Skip to the next song in queue", null)
if (voiceState == null) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"You are not connected to a VC",
"Please connect to a VC"
)
@ -54,7 +55,7 @@ class SkipCommand : SlashCommand("skip", "Skip to the next song in queue", null)
if (link.state != Link.State.CONNECTED) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Not connected",
"I'm not in a VC and therefore, I am not playing anything."
)
@ -62,7 +63,7 @@ class SkipCommand : SlashCommand("skip", "Skip to the next song in queue", null)
} else if (link.state == Link.State.CONNECTED && link.lastChannelId != channelId!!.value) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"You are not in my VC",
"We are not in the same VC and therefore, you cannot control the music"
)
@ -72,7 +73,7 @@ class SkipCommand : SlashCommand("skip", "Skip to the next song in queue", null)
if (track == null) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Not playing",
"I'm not playing anything currently"
)
@ -83,7 +84,7 @@ class SkipCommand : SlashCommand("skip", "Skip to the next song in queue", null)
gts.playNext()
MessageUtil.sendEmbedForInteractionWithImage(
interaction,
response,
deferred,
"Skipped song; now playing",
"**${track.title}**\n*Now Playing*\nby ${track.author} :: ${
TimeUtil.getTimeFormatedRaw(

View file

@ -27,11 +27,12 @@ import de.limited_dev.botendo.util.MessageUtil
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import dev.schlaubi.lavakord.audio.Link
import kotlinx.coroutines.Deferred
class StopCommand : SlashCommand("stop", "Stop playing and start leavin'", null) {
override suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
val guildId = interaction.guildId
@ -42,7 +43,7 @@ class StopCommand : SlashCommand("stop", "Stop playing and start leavin'", null)
if (voiceState == null) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"You are not connected to a VC",
"Please connect to my VC"
)
@ -53,7 +54,7 @@ class StopCommand : SlashCommand("stop", "Stop playing and start leavin'", null)
if (link.state != Link.State.CONNECTED) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Not connected",
"I'm not in a VC and therefore, I am not playing anything."
)
@ -61,7 +62,7 @@ class StopCommand : SlashCommand("stop", "Stop playing and start leavin'", null)
} else if (link.state == Link.State.CONNECTED && link.lastChannelId != channelId!!.value) {
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"You are not in my VC",
"We are not in the same VC"
)
@ -70,6 +71,6 @@ class StopCommand : SlashCommand("stop", "Stop playing and start leavin'", null)
player.stopTrack()
link.destroy()
MusicManager.getGuildTrackScheduler(interaction.getGuild(), player).clear()
MessageUtil.sendEmbedForInteraction(interaction, response, "I stopped and left", "just like your girlfriend")
MessageUtil.sendEmbedForInteraction(interaction, deferred, "I stopped and left", "just like your girlfriend")
}
}

View file

@ -30,6 +30,7 @@ import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.player.Player
import dev.schlaubi.lavakord.rest.TrackResponse
import dev.schlaubi.lavakord.rest.loadItem
import kotlinx.coroutines.Deferred
object MusicManager {
private var musicManagerMap: MutableMap<Snowflake, GuildTrackScheduler> = mutableMapOf()
@ -43,16 +44,16 @@ object MusicManager {
suspend fun addToQueue(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
link: Link,
search: String
) {
addToQueue(interaction, response, link, search, false)
addToQueue(interaction, deferred, link, search, false)
}
suspend fun addToQueue(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
link: Link,
search: String,
silent: Boolean
@ -69,7 +70,7 @@ object MusicManager {
if (!silent)
MessageUtil.sendEmbedForInteractionWithImage(
interaction,
response,
deferred,
"Queuing track from link",
"**${item.track.info.title}**\n*Queue*\nby ${item.track.info.author} :: ${
TimeUtil.getTimeFormatedRaw(
@ -89,7 +90,7 @@ object MusicManager {
if (!silent)
MessageUtil.sendEmbedForInteractionWithImage(
interaction,
response,
deferred,
"Queuing playlist from link",
"**${item.tracks.first().info.title}**\n*${item.playlistInfo.name}*\nby ${item.tracks.first().info.author} :: ${
TimeUtil.getTimeFormatedRaw(
@ -106,7 +107,7 @@ object MusicManager {
if (!silent)
MessageUtil.sendEmbedForInteractionWithImage(
interaction,
response,
deferred,
"Queuing track from query",
"**${item.tracks.first().info.title}**\n*Queue*\nby ${item.tracks.first().info.author} :: ${
TimeUtil.getTimeFormatedRaw(
@ -120,14 +121,14 @@ object MusicManager {
TrackResponse.LoadType.NO_MATCHES -> {
if (!silent)
MessageUtil.sendEmbedForInteraction(interaction, response, "Not found", "There were no matches.")
MessageUtil.sendEmbedForInteraction(interaction, deferred, "Not found", "There were no matches.")
}
TrackResponse.LoadType.LOAD_FAILED -> {
if (!silent)
MessageUtil.sendEmbedForInteraction(
interaction,
response,
deferred,
"Load failed",
"There was an error while loading."
)

View file

@ -25,13 +25,14 @@ import de.limited_dev.botendo.commands.slash.component.options.CommandOption
import de.limited_dev.botendo.util.MessageUtil
import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteractionResponseBehavior
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import kotlinx.coroutines.Deferred
class InfoCommand : SlashCommand("info", "Shows infos about the bot", null) {
override suspend fun onSlashCommand(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
args: Map<CommandOption, String>
) {
MessageUtil.sendEmbedForInteraction(interaction, response, "Botendo v5", "ver." + BuildConstants.version)
MessageUtil.sendEmbedForInteraction(interaction, deferred, "Botendo v5", "ver." + BuildConstants.version)
}
}

View file

@ -23,6 +23,7 @@ import dev.kord.core.behavior.interaction.response.DeferredPublicMessageInteract
import dev.kord.core.behavior.interaction.response.respond
import dev.kord.core.entity.interaction.GuildChatInputCommandInteraction
import dev.kord.rest.builder.message.EmbedBuilder
import kotlinx.coroutines.Deferred
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
@ -31,11 +32,11 @@ object MessageUtil {
suspend fun sendEmbedForInteraction(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
title: String,
description: String
) {
response.respond {
deferred.await().respond {
embeds = mutableListOf(
getEmbed(
title,
@ -48,12 +49,12 @@ object MessageUtil {
suspend fun sendEmbedForInteractionWithImage(
interaction: GuildChatInputCommandInteraction,
response: DeferredPublicMessageInteractionResponseBehavior,
deferred: Deferred<DeferredPublicMessageInteractionResponseBehavior>,
title: String,
description: String,
thumbnailUrl: String
) {
response.respond {
deferred.await().respond {
embeds = mutableListOf(
getEmbedWithImage(
title,