Skip to content

Commit

Permalink
Rough migration to JDA:v5.0.0-beta.6
Browse files Browse the repository at this point in the history
Not sure I did everything exactly right. A migration guide is covered
here, but I did most of this in a hurry.

https://jda.wiki/introduction/migration-v4-v5/
  • Loading branch information
jocull committed Aug 17, 2023
1 parent 101c28d commit daa1d3b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
// https://github.com/DV8FromTheWorld/JDA/commit/39ba0c2682ad99dbec88240cb8ea9d1ff7162ae9
// The snapshot version published in jitpack has this, so it is utilized for now.
// Eventually, a full upgrade to JDA 5.x will be necessary.
implementation("com.github.DV8FromTheWorld:JDA:legacy~v4-SNAPSHOT")
implementation("com.github.DV8FromTheWorld:JDA:v5.0.0-beta.6")
implementation("dev.arbjerg:lavaplayer:1.5.0")
implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/dev/arbjerg/ukulele/audio/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import dev.arbjerg.ukulele.config.BotProps
import dev.arbjerg.ukulele.data.GuildProperties
import dev.arbjerg.ukulele.data.GuildPropertiesService
import net.dv8tion.jda.api.audio.AudioSendHandler
import net.dv8tion.jda.api.entities.TextChannel
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -125,7 +125,7 @@ class Player(val beans: Beans, guildProperties: GuildProperties) : AudioEventAda

override fun onTrackStart(player: AudioPlayer, track: AudioTrack) {
if (beans.botProps.announceTracks) {
lastChannel?.sendMessage(beans.nowPlayingCommand.buildEmbed(track))?.queue()
lastChannel?.sendMessageEmbeds(beans.nowPlayingCommand.buildEmbed(track))?.queue()
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/main/kotlin/dev/arbjerg/ukulele/command/HelpCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package dev.arbjerg.ukulele.command
import dev.arbjerg.ukulele.features.HelpContext
import dev.arbjerg.ukulele.jda.Command
import dev.arbjerg.ukulele.jda.CommandContext
import net.dv8tion.jda.api.MessageBuilder
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.utils.MarkdownUtil
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
import org.springframework.stereotype.Component

@Component
Expand All @@ -12,14 +14,15 @@ class HelpCommand : Command("help") {
if (argumentText.isNotBlank()) {
replyHelp(beans.commandManager[argumentText.trim()] ?: command)
} else {
val msg = MessageBuilder()
.append("Available commands:")
.appendCodeBlock(buildString {
val msg = MessageCreateBuilder()
.addContent("Available commands:")
.addContent(MarkdownUtil.codeblock(buildString {
beans.commandManager.getCommands().forEach {
appendLine((listOf(it.name) + it.aliases).joinToString())
}
}, "")
.append("\nUse \"${trigger} <command>\" to see more details.")
}))
.addContent("\nUse \"${trigger} <command>\" to see more details.")

replyMsg(msg.build())
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/dev/arbjerg/ukulele/features/HelpContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package dev.arbjerg.ukulele.features

import dev.arbjerg.ukulele.jda.Command
import dev.arbjerg.ukulele.jda.CommandContext
import net.dv8tion.jda.api.MessageBuilder
import net.dv8tion.jda.api.utils.MarkdownUtil
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder

class HelpContext(private val commandContext: CommandContext, private val command: Command) {
private val lines = mutableListOf<String>()
Expand All @@ -20,7 +21,7 @@ class HelpContext(private val commandContext: CommandContext, private val comman
lines.add("# " + text.trim())
}

fun buildMessage() = MessageBuilder()
.appendCodeBlock(lines.joinToString(separator = "\n"), "md")
.build()
fun buildMessage() = MessageCreateBuilder()
.addContent(MarkdownUtil.codeblock("md", lines.joinToString(separator = "\n")))
.build()
}
25 changes: 13 additions & 12 deletions src/main/kotlin/dev/arbjerg/ukulele/jda/CommandContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.entities.TextChannel
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel
import net.dv8tion.jda.api.utils.messages.MessageCreateData
import org.springframework.stereotype.Component

class CommandContext(
val beans: Beans,
val guildProperties: GuildProperties,
val guild: Guild,
val channel: TextChannel,
val invoker: Member,
val message: Message,
val command: Command,
val prefix: String,
/** Prefix + command name */
val beans: Beans,
val guildProperties: GuildProperties,
val guild: Guild,
val channel: TextChannel,
val invoker: Member,
val message: Message,
val command: Command,
val prefix: String,
/** Prefix + command name */
val trigger: String
) {
@Component
Expand All @@ -44,12 +45,12 @@ class CommandContext(
channel.sendMessage(msg).queue()
}

fun replyMsg(msg: Message) {
fun replyMsg(msg: MessageCreateData) {
channel.sendMessage(msg).queue()
}

fun replyEmbed(embed: MessageEmbed) {
channel.sendMessage(embed).queue()
channel.sendMessage(MessageCreateData.fromEmbeds(embed)).queue()
}

fun replyHelp(forCommand: Command = command) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/arbjerg/ukulele/jda/CommandManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.launch
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.TextChannel
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/dev/arbjerg/ukulele/jda/EventHandler.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.arbjerg.ukulele.jda

import net.dv8tion.jda.api.events.ReadyEvent
import net.dv8tion.jda.api.entities.channel.ChannelType
import net.dv8tion.jda.api.events.StatusChangeEvent
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -13,9 +13,11 @@ class EventHandler(private val commandManager: CommandManager) : ListenerAdapter

private val log: Logger = LoggerFactory.getLogger(EventHandler::class.java)

override fun onGuildMessageReceived(event: GuildMessageReceivedEvent) {
override fun onMessageReceived(event: MessageReceivedEvent) {
if (event.isWebhookMessage || event.author.isBot) return
commandManager.onMessage(event.guild, event.channel, event.member!!, event.message)
if (event.channelType != ChannelType.TEXT) return

commandManager.onMessage(event.guild, event.channel.asTextChannel(), event.member!!, event.message)
}

override fun onStatusChange(event: StatusChangeEvent) {
Expand Down
27 changes: 14 additions & 13 deletions src/main/kotlin/dev/arbjerg/ukulele/jda/JdaConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import net.dv8tion.jda.api.sharding.ShardManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import net.dv8tion.jda.api.requests.GatewayIntent.*
import net.dv8tion.jda.api.requests.restaction.MessageAction
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
import net.dv8tion.jda.api.utils.cache.CacheFlag
import net.dv8tion.jda.api.utils.messages.MessageRequest
import javax.security.auth.login.LoginException
import kotlin.concurrent.thread

@Configuration
class JdaConfig {

init {
MessageAction.setDefaultMentions(emptyList())
MessageRequest.setDefaultMentions(emptyList())
}

@Bean
Expand All @@ -26,20 +26,21 @@ class JdaConfig {


val intents = listOf(
GUILD_VOICE_STATES,
GUILD_MESSAGES,
GUILD_BANS,
DIRECT_MESSAGES
GUILD_VOICE_STATES,
GUILD_MESSAGES,
GUILD_BANS,
DIRECT_MESSAGES,
MESSAGE_CONTENT
)

val builder = DefaultShardManagerBuilder.create(botProps.token, intents)
.disableCache(CacheFlag.ACTIVITY, CacheFlag.EMOTE, CacheFlag.CLIENT_STATUS)
.setBulkDeleteSplittingEnabled(false)
.setEnableShutdownHook(false)
.setAutoReconnect(true)
.setShardsTotal(botProps.shards)
.addEventListeners(eventHandler)
.setActivity(activity)
.disableCache(CacheFlag.ACTIVITY, CacheFlag.EMOJI, CacheFlag.CLIENT_STATUS)
.setBulkDeleteSplittingEnabled(false)
.setEnableShutdownHook(false)
.setAutoReconnect(true)
.setShardsTotal(botProps.shards)
.addEventListeners(eventHandler)
.setActivity(activity)

val shardManager: ShardManager
try {
Expand Down

0 comments on commit daa1d3b

Please sign in to comment.