Skip to content

Commit

Permalink
Merge branch 'fixes/boot-time' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bombies committed Oct 7, 2023
2 parents f5ec685 + b86446e commit 677c9a6
Show file tree
Hide file tree
Showing 31 changed files with 292 additions and 281 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/main/audiohandlers/GuildMusicManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GuildMusicManager(val guild: Guild) {

scheduler.clearRequesters()
SkipCommand().clearVoteSkipInfo(guild)
RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()
}

suspend fun leave() {
Expand Down
13 changes: 12 additions & 1 deletion src/main/kotlin/main/audiohandlers/RobertifyAudioManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import main.utils.json.restrictedchannels.RestrictedChannelsConfig
import main.utils.json.toggles.TogglesConfig
import main.utils.locale.LocaleManager
import main.utils.locale.messages.GeneralMessages
import main.utils.locale.messages.JoinMessages
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.GuildVoiceState
import net.dv8tion.jda.api.entities.Message
Expand All @@ -31,6 +32,7 @@ import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException
import net.dv8tion.jda.api.interactions.InteractionHook
import java.lang.IllegalArgumentException
import java.util.*

object RobertifyAudioManager {
Expand Down Expand Up @@ -215,7 +217,7 @@ object RobertifyAudioManager {
hookMessage: InteractionHook? = null
): Boolean {
try {
require(!GuildConfig(musicManager.guild).getTwentyFourSevenMode() && channel.members.size > 0) { "I can't join a voice channel with no one in it!" }
require(channel.members.size > 0) { "I can't join a voice channel with no one in it!" }
when (musicManager.link.state) {
Link.State.DESTROYED, Link.State.NOT_CONNECTED -> {
val guild = musicManager.guild
Expand Down Expand Up @@ -279,6 +281,15 @@ object RobertifyAudioManager {
if (message != null)
message.editMessageEmbeds(embed).queue()
else hookMessage?.editEmbed { embed }?.queue()
} catch (e: IllegalArgumentException) {
val embed = RobertifyEmbedUtils.embedMessage(
channel.guild,
JoinMessages.CANT_JOIN,
Pair("{channel}", channel.asMention)
).build()
if (message != null)
message.editMessageEmbeds(embed).queue()
else hookMessage?.editEmbed { embed }?.queue()
}

return false
Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/main/audiohandlers/TrackScheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import dev.schlaubi.lavakord.audio.player.Player
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.MissingFieldException
import main.audiohandlers.models.Requester
import main.audiohandlers.utils.artworkUrl
import main.audiohandlers.utils.author
Expand Down Expand Up @@ -114,6 +116,7 @@ class TrackScheduler(private val guild: Guild, private val link: Link) {
.launchIn(player.coroutineScope)
}

@OptIn(ExperimentalSerializationApi::class)
private suspend fun onTrackStart(event: TrackStartEvent) {
val track = event.track

Expand All @@ -137,7 +140,7 @@ class TrackScheduler(private val guild: Guild, private val link: Link) {
}

val requestChannelConfig = RequestChannelConfig(guild)
requestChannelConfig.updateMessage()?.await()
requestChannelConfig.updateMessage()

disconnectManager.cancelDisconnect()
queueHandler.lastPlayedTrackBuffer = track
Expand Down Expand Up @@ -183,14 +186,18 @@ class TrackScheduler(private val guild: Guild, private val link: Link) {
img = NowPlayingImageBuilder(
artistName = track.author,
title = track.title,
albumImage = track.artworkUrl ?: defaultBackgroundImage,
albumImage = try {
track.artworkUrl ?: defaultBackgroundImage
} catch (e: MissingFieldException) {
defaultBackgroundImage
},
requesterName = requesterUser.name,
requesterAvatar = requesterUser.avatarUrl
).build() ?: throw NullPointerException("The generated image was null!")
} catch (ex: Exception) {
// Either building the image failed or the bot doesn't have enough
// permission to send images in a certain channel
if (ex is PermissionException || ex is ImageBuilderException) {
if (ex is PermissionException || ex is ImageBuilderException || ex is NullPointerException) {
try {
sendNowPlayingEmbed(trackInfo, requesterMention).await()
} catch (ex: PermissionException) {
Expand Down Expand Up @@ -244,7 +251,7 @@ class TrackScheduler(private val guild: Guild, private val link: Link) {
nextTrack(trackToUse)
} else {
if (queueHandler.isEmpty)
RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()
}
}

Expand Down Expand Up @@ -390,7 +397,6 @@ class TrackScheduler(private val guild: Guild, private val link: Link) {
if (nextTrack != null) {
logger.debug("Retrieved {} and attempting to play", nextTrack.info.title)
player.playTrack(nextTrack)

} else {
if (lastTrack != null
&& AutoPlayConfig(guild).getStatus()
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/main/audiohandlers/loaders/AutoPlayLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.sedmelluq.discord.lavaplayer.tools.FriendlyException
import dev.arbjerg.lavalink.protocol.v4.Playlist
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.minn.jda.ktx.util.SLF4J
import kotlinx.coroutines.runBlocking
import main.audiohandlers.GuildMusicManager
import main.audiohandlers.loaders.MainAudioLoader.Companion.queueThenDelete
import main.audiohandlers.utils.identifier
Expand Down Expand Up @@ -62,7 +61,7 @@ class AutoPlayLoader(
queueHandler.clearSavedQueue()
}

RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()
}

override suspend fun onSearchResultLoad(results: List<Track>) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/kotlin/main/audiohandlers/loaders/MainAudioLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import dev.arbjerg.lavalink.protocol.v4.Exception
import dev.arbjerg.lavalink.protocol.v4.Playlist
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.minn.jda.ktx.coroutines.await
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import dev.minn.jda.ktx.events.getDefaultScope
import kotlinx.coroutines.runBlocking
import main.audiohandlers.GuildMusicManager
import main.audiohandlers.models.Requester
import main.main.Robertify
import main.utils.RobertifyEmbedUtils
import main.utils.json.logs.LogType
import main.utils.json.logs.LogUtilsKt
Expand Down Expand Up @@ -45,7 +42,7 @@ class MainAudioLoader(
private val executorService = Executors.newSingleThreadScheduledExecutor()

suspend fun RestAction<Message>.queueThenDelete(
context: CoroutineContext = Robertify.coroutineEventManager.coroutineContext,
context: CoroutineContext = getDefaultScope().coroutineContext,
time: Long = 10,
unit: TimeUnit = TimeUnit.SECONDS,
deletePredicate: (suspend (message: Message) -> Boolean)? = null,
Expand Down Expand Up @@ -188,7 +185,7 @@ class MainAudioLoader(
if (queueHandler.queueRepeating)
queueHandler.setSavedQueue(queueHandler.contents)

requestChannelConfig.updateMessage()?.await()
requestChannelConfig.updateMessage()
}

override suspend fun onNoMatches() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main.audiohandlers.utils

import dev.arbjerg.lavalink.protocol.v4.Track
import dev.schlaubi.lavakord.plugins.lavasrc.lavaSrcInfo
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.MissingFieldException

val Track.identifier: String
get() = info.identifier
Expand All @@ -24,8 +26,9 @@ val Track.uri: String?
val Track.source: String
get() = info.sourceName

@OptIn(ExperimentalSerializationApi::class)
val Track.artworkUrl: String?
get() = info.artworkUrl ?: lavaSrcInfo.artistArtworkUrl
get() = try { info.artworkUrl ?: lavaSrcInfo.artistArtworkUrl } catch (e: MissingFieldException) { null }

val Track.isrc: String?
get() = info.isrc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class MoveCommand : AbstractSlashCommand(
).build()
}

RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()

val movedTrack = trackList[id - 1]
LogUtilsKt(guild).sendLog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main.commands.slashcommands.audio

import com.github.topisenpai.lavasrc.mirror.MirroringAudioTrack
import dev.minn.jda.ktx.util.SLF4J
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.MissingFieldException
import main.audiohandlers.RobertifyAudioManager
import main.audiohandlers.utils.*
import main.constants.Toggle
Expand Down Expand Up @@ -37,6 +39,7 @@ class NowPlayingCommand : AbstractSlashCommand(
val logger by SLF4J
}

@OptIn(ExperimentalSerializationApi::class)
override suspend fun handle(event: SlashCommandInteractionEvent) {
event.deferReply().queue()

Expand Down Expand Up @@ -88,8 +91,11 @@ class NowPlayingCommand : AbstractSlashCommand(
val builder = NowPlayingImageBuilder(
title = track!!.title,
artistName = track.author,
albumImage = track.artworkUrl
?: defaultImage
albumImage = try {
track.artworkUrl ?: defaultImage
} catch (e: MissingFieldException) {
defaultImage
}
)

val image = if (!track.isStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PreviousTrackCommand : AbstractSlashCommand(
player.stopTrack()
player.playTrack(queueHandler.popPreviousTrack()!!)

RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()

LogUtilsKt(guild).sendLog(
LogType.TRACK_PREVIOUS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class RemoveCommand : AbstractSlashCommand(
Pair("{author}", removedTrack.author)
)
if (id <= 10)
RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()
RobertifyEmbedUtils.embedMessage(
guild,
RemoveMessages.REMOVED,
Expand Down Expand Up @@ -167,7 +167,7 @@ class RemoveCommand : AbstractSlashCommand(
Pair("{author}", removedTrack.author)
)
if (id < 10)
RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()
RobertifyEmbedUtils.embedMessage(
guild,
RemoveMessages.REMOVED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ShuffleCommand : AbstractSlashCommand(
queueHandler.clear()
queueHandler.addAll(trackList)

RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()

LogUtilsKt(guild).sendLog(
LogType.QUEUE_SHUFFLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class SkipCommand : AbstractSlashCommand(
queueHandler.pushPastTrack(playingTrack)
scheduler.nextTrack(playingTrack, true, player.position)

RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()
SkipCommand().clearVoteSkipInfo(guild)
return RobertifyEmbedUtils.embedMessage(musicManager.guild, "Skipped to **track #$id**!").build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class VolumeCommand : AbstractSlashCommand(
this.volume = volume / 100F
}

RequestChannelConfig(guild).updateMessage()?.await()
RequestChannelConfig(guild).updateMessage()

LogUtilsKt(guild).sendLog(
LogType.VOLUME_CHANGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class ManageSuggestionsCommand : AbstractSlashCommand(
event.deferReply(true).queue()

if (config.suggestionSetup)
return event.replyEmbed("The suggestions channels have already been setup!")
return event.hook.sendEmbed(guild, "The suggestions channels have already been setup!")
.setEphemeral(true)
.queue()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class UpdateCommand : AbstractSlashCommand(
successMsg = "Successfully updated all request channel messages!",
errorMsg = "Could not update all request channel messages!"
) {
it.updateMessage()?.await()
it.updateMessage()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ThemeCommand : AbstractSlashCommand(
suspend fun updateTheme(guild: Guild, theme: RobertifyTheme, shardManager: ShardManager = Robertify.shardManager) {
ThemesConfig(guild).setTheme(theme)
GeneralUtils.setDefaultEmbed(guild)
RequestChannelConfig(guild, shardManager).updateMessage()?.await()
RequestChannelConfig(guild, shardManager).updateMessage()
}

private suspend fun getSelectMenu(guild: Guild, userId: Long): StringSelectMenu {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class RequestChannelEditCommand : AbstractSlashCommand(

try {
if (RobertifyAudioManager[guild].player.playingTrack != null)
config.updateMessage()?.await()
config.updateMessage()
} catch (_: UninitializedPropertyAccessException) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/main/events/AbstractEventController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import main.main.Robertify
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter

abstract class AbstractEventController : ListenerAdapter() {
abstract class AbstractEventController {

companion object {
protected val shardManager = Robertify.shardManager
Expand Down
Loading

0 comments on commit 677c9a6

Please sign in to comment.