diff --git a/bg-graph/src/main/java/com/dongtronic/diabot/graph/BgGraph.kt b/bg-graph/src/main/java/com/dongtronic/diabot/graph/BgGraph.kt index c2b4feaa..8cf5e14a 100644 --- a/bg-graph/src/main/java/com/dongtronic/diabot/graph/BgGraph.kt +++ b/bg-graph/src/main/java/com/dongtronic/diabot/graph/BgGraph.kt @@ -182,7 +182,8 @@ class BgGraph( fun getBitmapBytes(bitmapFormat: BitmapEncoder.BitmapFormat): ByteArray { val theme = settings.theme.instance - val bufferedImage = BufferedImage(theme.getImageWidth(this), theme.getImageHeight(this), BufferedImage.TYPE_INT_ARGB) + val bufferedImage = + BufferedImage(theme.getImageWidth(this), theme.getImageHeight(this), BufferedImage.TYPE_INT_ARGB) val g = bufferedImage.createGraphics() if (theme.overridePaint) { diff --git a/bot/build.gradle b/bot/build.gradle index 728db23d..e3d90fde 100644 --- a/bot/build.gradle +++ b/bot/build.gradle @@ -1,6 +1,6 @@ dependencies { // JDA - implementation "com.github.MinnDevelopment:jda-ktx:${jdaKtxVersion}" + implementation "club.minnced:jda-ktx:${jdaKtxVersion}" implementation "pw.chew:jda-chewtils:${jdaChewtilsVersion}" implementation("net.dv8tion:JDA:${jdaVersion}") { exclude module: 'opus-java' diff --git a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/info/AboutCommand.kt b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/info/AboutCommand.kt index f24557b1..42d41f41 100644 --- a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/info/AboutCommand.kt +++ b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/info/AboutCommand.kt @@ -32,7 +32,9 @@ class AboutCommand( if (info.isBotPublic) { info.setRequiredScopes("applications.commands") info.getInviteUrl(*perms) - } else "" + } else { + "" + } } catch (e: Exception) { logger.warn("Encountered exception while generating bot invite link", e) "" @@ -55,7 +57,9 @@ class AboutCommand( "Join my server [`here`](${event.client.serverInvite})" } else if (invite) { "Please [`invite`]($oauthLink) me to your server" - } else "" + } else { + "" + } val author = if (event.jda.getUserById(event.client.ownerId) == null) { "<@" + event.client.ownerId + ">" diff --git a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/nightscout/NightscoutGraphApplicationCommand.kt b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/nightscout/NightscoutGraphApplicationCommand.kt index 59a366cc..de8b785c 100644 --- a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/nightscout/NightscoutGraphApplicationCommand.kt +++ b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/nightscout/NightscoutGraphApplicationCommand.kt @@ -67,7 +67,7 @@ class NightscoutGraphApplicationCommand : ApplicationCommand { val chart = getDataSet(event.user.id, hours).awaitSingle() val imageBytes = chart.getBitmapBytes(BitmapFormat.PNG) - event.hook.editOriginalAttachments(FileUpload.fromData(imageBytes, "graph.png")).submit().await() + event.hook.editOriginalAttachments(FileUpload.fromData(imageBytes, "graph.png")).await() applyCooldown(event.user.id) } catch (e: Exception) { logger.error("Error generating NS graph for ${event.user}") diff --git a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteImportCommand.kt b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteImportCommand.kt index 6c0a78cf..e140a1f0 100644 --- a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteImportCommand.kt +++ b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteImportCommand.kt @@ -158,7 +158,7 @@ class QuoteImportCommand(category: Category, parent: QuoteCommand) : DiscordComm importQuotes.subscribe({ // on each - logger.debug("Finished adding $it") + logger.debug("Finished adding {}", it) }, { // on error var errorMessage = "Import failed: ${it::class.simpleName} - ${it.message}" @@ -304,7 +304,9 @@ class QuoteImportCommand(category: Category, parent: QuoteCommand) : DiscordComm // the channel key can be missing, which is why we need a null check val channelId = if (channel != null) { guild?.channels?.firstOrNull { it.name == channel }?.id ?: "" - } else "" + } else { + "" + } return QuoteDTO( quoteId = id, diff --git a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteMineCommand.kt b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteMineCommand.kt index 4efa3981..c8d0de57 100644 --- a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteMineCommand.kt +++ b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/commands/quote/QuoteMineCommand.kt @@ -28,8 +28,8 @@ class QuoteMineCommand(category: Category, parent: QuoteCommand) : DiscordComman { event.reply(createEmbed(event.author.name, it)) }, { - logger.info("Error finding all quotes for " + event.author.name) - }) + logger.info("Error finding all quotes for " + event.author.name) + }) } private fun createEmbed(author: String, quoteDTOs: List): MessageEmbed { diff --git a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/listeners/HelpListener.kt b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/listeners/HelpListener.kt index ced38bf6..6bb63e6d 100644 --- a/bot/src/main/java/com/dongtronic/diabot/platforms/discord/listeners/HelpListener.kt +++ b/bot/src/main/java/com/dongtronic/diabot/platforms/discord/listeners/HelpListener.kt @@ -6,7 +6,6 @@ import com.dongtronic.diabot.util.logger import com.jagrosh.jdautilities.command.Command import com.jagrosh.jdautilities.command.CommandEvent import net.dv8tion.jda.api.EmbedBuilder -import net.dv8tion.jda.api.Permission import net.dv8tion.jda.api.entities.Message import net.dv8tion.jda.api.entities.channel.ChannelType import net.dv8tion.jda.api.exceptions.ErrorResponseException @@ -25,7 +24,7 @@ class HelpListener : Consumer { */ private fun sendingError(exc: Throwable, event: CommandEvent) { if (exc is ErrorResponseException && - exc.errorResponse != ErrorResponse.CANNOT_SEND_TO_USER + exc.errorResponse != ErrorResponse.CANNOT_SEND_TO_USER ) { // Print a warning in console if the error code was not related to DMs being blocked logger.warn("Unexpected error response when sending DM: ${exc.errorCode} - ${exc.meaning}") @@ -53,13 +52,13 @@ class HelpListener : Consumer { try { // Open the DM channel and send the message event.author.openPrivateChannel().submit() - .thenCompose { it.sendMessageEmbeds(embedBuilder.build()).submit() } - .whenComplete { _: Message?, exc: Throwable? -> - if (exc != null) { - // If there's a throwable then assume it failed - sendingError(exc, event) + .thenCompose { it.sendMessageEmbeds(embedBuilder.build()).submit() } + .whenComplete { _: Message?, exc: Throwable? -> + if (exc != null) { + // If there's a throwable then assume it failed + sendingError(exc, event) + } } - } } catch (ex: InsufficientPermissionException) { event.replyError("Couldn't build help message due to missing permission: `${ex.permission}`") } @@ -78,15 +77,15 @@ class HelpListener : Consumer { // Store the CompletableFuture in the queue, so we can cancel it later val message = channel.thenCompose { it.sendMessageEmbeds(categoryBuilder.build()).submit() } - .whenComplete { _: Message?, exc: Throwable? -> - if (exc != null) { - sendingError(exc, event) - // Cancel the other messages in the queue - messageQueue.forEach { it.cancel(true) } - } + .whenComplete { _: Message?, exc: Throwable? -> + if (exc != null) { + sendingError(exc, event) + // Cancel the other messages in the queue + messageQueue.forEach { it.cancel(true) } + } - messageQueue.clear() - } + messageQueue.clear() + } messageQueue.add(message) } } @@ -226,41 +225,20 @@ class HelpListener : Consumer { * @param event original CommandEvent. Used for checking permissions * @return list of commands the user is authorized to use */ - private fun filterAllowedCommands(commands: List, event: CommandEvent): ArrayList { - val allowedCommands = ArrayList() + private fun filterAllowedCommands(commands: List, event: CommandEvent): List { + val guildMember = event.member != null + val userPermissions = event.member?.permissions - // TODO: Refactor method to recude jump statements - for (command in commands) { - if (command.isHidden) { - continue - } - - val requiredPermissions = command.userPermissions - - if (requiredPermissions.isEmpty()) { - allowedCommands.add(command) - continue - } - - var userPermissions: EnumSet = Permission.getPermissions(Permission.ALL_PERMISSIONS) - if (event.member != null) { - userPermissions = event.member.permissions - } - - var userIsAllowedToUseCommand = true - - for (requiredPermission in requiredPermissions) { - if (!userPermissions.contains(requiredPermission)) { - userIsAllowedToUseCommand = false - } - } - - if (userIsAllowedToUseCommand) { - allowedCommands.add(command) - } + if (!guildMember) { + // Message in DM, no need to check for server permissions + return commands.filter { !it.isHidden } } - return allowedCommands + // Return all not-hidden commands that the user has permission for in the guild + return commands.filter { !it.isHidden } + .filter { command -> + userPermissions!!.containsAll(command.userPermissions.filterNotNull()) + } } /** diff --git a/build.gradle b/build.gradle index 02deb5ce..d7dbd478 100644 --- a/build.gradle +++ b/build.gradle @@ -42,10 +42,17 @@ allprojects { systemProperty "junit.jupiter.testinstance.lifecycle.default", "per_class" } - group = 'com.dongtronic.diabot' + java { + targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_17 + } + + kotlin { + targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_17 + } - sourceCompatibility = 17 - targetCompatibility = 17 + group = "com.dongtronic.diabot" detekt { buildUponDefaultConfig = true // preconfigure defaults @@ -90,33 +97,33 @@ allprojects { } -task stage(dependsOn: ['clean', 'shadowJar']) +task stage(dependsOn: ["clean", "shadowJar"]) shadowJar { - exclude 'logback-test.xml' - archiveBaseName.set('diabot') - archiveClassifier.set('') - archiveVersion.set('') + exclude "logback-test.xml" + archiveBaseName.set("diabot") + archiveClassifier.set("") + archiveVersion.set("") manifest { attributes( - 'Implementation-Title': 'Diabot - a diabetes Discord bot', - 'Implementation-Version': this.version, - 'Main-Class': 'com.dongtronic.diabot.Main', + "Implementation-Title": "Diabot - a diabetes Discord bot", + "Implementation-Version": this.version, + "Main-Class": "com.dongtronic.diabot.Main", // fixes retrofit v2.8 reflection warnings - 'Add-Opens': 'java.base/java.lang.invoke' + "Add-Opens": "java.base/java.lang.invoke" ) } } release { - tagTemplate = 'v$version' + tagTemplate = "v$version" git { - requireBranch.set('main') + requireBranch.set("main") signTag.set(true) - ignoredSnapshotDependencies = [ "pw.chew:jda-chewtils" ] + ignoredSnapshotDependencies = ["pw.chew:jda-chewtils"] } } dependencies { - implementation project(path: ':bot') + implementation project(path: ":bot") } diff --git a/gradle.properties b/gradle.properties index 76ec872b..c2b0d09b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,9 +3,9 @@ version = 1.19.6 # Plugins shadowVersion = 7.1.2 -detektVersion = 1.21.0 -kotlinVersion = 1.9.24 -kotlinCoroutinesVersion = 1.8.1 +detektVersion = 1.23.6 +kotlinVersion = 2.0.0 +kotlinCoroutinesVersion = 1.9.0-RC junitVersion = 5.8.2 releasePluginVersion = 3.0.2 @@ -18,9 +18,9 @@ reactorCoreVersion = 3.3.8.RELEASE reactorKotlinVersion = 1.0.2.RELEASE # JDA -jdaVersion = 5.0.0-beta.24 +jdaVersion = 5.0.1 jdaChewtilsVersion = 2.0-SNAPSHOT -jdaKtxVersion = 0.11.0-beta.19 +jdaKtxVersion = 0.12.0 # Parsing romeVersion = 1.11.1 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8fad3f5a..19cfad96 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/system.properties b/system.properties index 180a2734..eafd676c 100644 --- a/system.properties +++ b/system.properties @@ -1 +1 @@ -java.runtime.version=11 \ No newline at end of file +java.runtime.version=17