Skip to content

Commit

Permalink
Merge pull request #14 from ddivad195/feat/slash-command-migration
Browse files Browse the repository at this point in the history
feat: slash command migration
  • Loading branch information
ddivad195 authored Jan 26, 2022
2 parents 219838f + 64b6af7 commit d8ce624
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 129 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {

dependencies {
implementation("me.jakejmattson:DiscordKt:${Versions.DISCORDKT}")
implementation("org.slf4j:slf4j-simple:1.7.30")
implementation("io.github.microutils:kotlin-logging-jvm:2.1.20")
}

tasks {
Expand Down
7 changes: 3 additions & 4 deletions commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
| setRole | Role | Set the role required to use this bot. |

## General
| Commands | Arguments | Description |
| -------- | --------- | --------------------------------------------------- |
| bookmark | Message | Bookmark a message using Keeper |
| delete | Message | Delete a Keeper bookmark by ID inside of DM channel |
| Commands | Arguments | Description |
| -------- | --------- | ------------------------------- |
| bookmark | Message | Bookmark a message using Keeper |

## Operation
| Commands | Arguments | Description |
Expand Down
20 changes: 8 additions & 12 deletions src/main/kotlin/me/ddivad/keeper/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.ddivad.keeper

import dev.kord.common.annotation.KordPreview
import dev.kord.common.entity.Snowflake
import dev.kord.common.kColor
import dev.kord.core.supplier.EntitySupplyStrategy
import dev.kord.gateway.Intent
import dev.kord.gateway.Intents
Expand All @@ -11,11 +10,8 @@ import me.ddivad.keeper.dataclasses.Configuration
import me.ddivad.keeper.dataclasses.Permissions
import me.ddivad.keeper.services.CacheService
import me.ddivad.keeper.services.StatisticsService
import me.jakejmattson.discordkt.api.dsl.bot
import me.jakejmattson.discordkt.api.extensions.addField
import me.jakejmattson.discordkt.api.extensions.addInlineField
import me.jakejmattson.discordkt.api.extensions.profileLink
import me.jakejmattson.discordkt.api.extensions.toSnowflake
import me.jakejmattson.discordkt.dsl.bot
import me.jakejmattson.discordkt.extensions.*
import java.awt.Color

@KordPreview
Expand All @@ -29,7 +25,7 @@ suspend fun main() {
val configuration = data("config/config.json") { Configuration() }

prefix {
guild?.let { configuration[guild!!.id.value]?.prefix } ?: defaultPrefix
guild?.let { configuration[guild!!.id]?.prefix } ?: defaultPrefix
}

configure {
Expand All @@ -41,15 +37,15 @@ suspend fun main() {
Intent.GuildMessageReactions,
Intent.DirectMessagesReactions
)
permissions(Permissions.STAFF)
permissions = Permissions
}

mentionEmbed {
val (configuration, statsService) = it.discord.getInjectionObjects(Configuration::class, StatisticsService::class)
val guild = it.guild ?: return@mentionEmbed
val guildConfiguration = configuration[it.guild!!.id.value] ?: return@mentionEmbed
val guildConfiguration = configuration[it.guild!!.id] ?: return@mentionEmbed
val self = it.channel.kord.getSelf()
val liveRole = guild.getRole(guildConfiguration.requiredRoleId.toSnowflake())
val liveRole = guild.getRole(guildConfiguration.requiredRoleId)
author {
val user = guild.kord.getUser(Snowflake(394484823944593409))
icon = user?.avatar?.url
Expand All @@ -59,7 +55,7 @@ suspend fun main() {

title = "Keeper"
thumbnail {
url = self.avatar.url
url = self.pfpUrl
}
color = it.discord.configuration.theme
description = "A bot for saving useful messages to a DM by reacting to them."
Expand All @@ -70,7 +66,7 @@ suspend fun main() {
"Reaction: ${guildConfiguration.bookmarkReaction}\n" +
"```")
addField("Bot Info", "```" +
"Version: 1.6.1\n" +
"Version: 1.7.0\n" +
"DiscordKt: ${it.discord.versions.library}\n" +
"Kord: ${it.discord.versions.kord}\n" +
"Kotlin: ${KotlinVersion.CURRENT}\n" +
Expand Down
41 changes: 19 additions & 22 deletions src/main/kotlin/me/ddivad/keeper/commands/GeneralCommands.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
package me.ddivad.keeper.commands

import dev.kord.rest.request.KtorRequestException
import dev.kord.x.emoji.Emojis
import dev.kord.x.emoji.addReaction
import me.ddivad.keeper.dataclasses.Configuration
import me.ddivad.keeper.dataclasses.Permissions
import me.ddivad.keeper.embeds.buildSavedMessageEmbed
import me.ddivad.keeper.services.StatisticsService
import me.jakejmattson.discordkt.api.arguments.MessageArg
import me.jakejmattson.discordkt.api.commands.commands
import me.jakejmattson.discordkt.api.extensions.sendPrivateMessage
import me.jakejmattson.discordkt.arguments.MessageArg
import me.jakejmattson.discordkt.commands.commands
import me.jakejmattson.discordkt.extensions.sendPrivateMessage
import mu.KotlinLogging

private val logger = KotlinLogging.logger { }

@Suppress("unused")
fun generalCommands(configuration: Configuration, statsService: StatisticsService) = commands("General") {
dmCommand("delete") {
description = "Delete a Keeper bookmark by ID inside of DM channel"
requiredPermission = Permissions.NONE
execute(MessageArg) {
val message = args.first

if (message.author == this.channel.kord.getSelf().asUser()) {
message.delete()
} else {
respond("Can only delete messages sent by ${this.channel.kord.getSelf().mention} in direct message channels.")
}
}
}

slash("bookmark", "Bookmark") {
description = "Bookmark a message using Keeper"
requiredPermission = Permissions.NONE
execute(MessageArg) {
val guild = guild?.asGuildOrNull() ?: return@execute
val guild = guild.asGuildOrNull()
statsService.bookmarkAdded(guild)
this.author.sendPrivateMessage {
buildSavedMessageEmbed(args.first.asMessage(), guild)
}.addReaction(Emojis.x)
respond("Message Bookmarked ${Emojis.bookmark}")
try {
this.author.sendPrivateMessage {
buildSavedMessageEmbed(args.first.asMessage(), guild)
}.addReaction(Emojis.x)
respond("Message Bookmarked ${Emojis.bookmark}")
logger.info { "Message Bookmarked by ${this.author.username}" }
} catch (e: KtorRequestException) {
respond("Looks like you have DMs disabled. To bookmark messages, this needs to be enabled.")
logger.error { "Bookmark DM could not be sent" }
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,66 @@ package me.ddivad.keeper.commands
import me.ddivad.keeper.conversations.ConfigurationConversation
import me.ddivad.keeper.dataclasses.Configuration
import me.ddivad.keeper.dataclasses.Permissions
import me.jakejmattson.discordkt.api.arguments.*
import me.jakejmattson.discordkt.api.commands.commands
import me.jakejmattson.discordkt.arguments.*
import me.jakejmattson.discordkt.commands.commands

@Suppress("unused")
fun guildConfigurationCommands(configuration: Configuration) = commands("Configuration", Permissions.STAFF) {
command("configure") {
description = "Configure a guild to use Keeper."
execute {
if (configuration.hasGuildConfig(guild.id.value)) {
if (configuration.hasGuildConfig(guild.id)) {
respond("Guild configuration exists. To modify it use the commands to set values.")
return@execute
}
ConfigurationConversation(configuration)
.createConfigurationConversation(guild)
.startPublicly(discord, author, channel)
respond("Guild setup")
}
}

command("setPrefix") {
slash("setPrefix") {
description = "Set the prefix required for the bot to register a command."
execute(AnyArg("Prefix")) {
if (!configuration.hasGuildConfig(guild.id.value)) {
respond("Guild configuration exists. To modify it use the commands to set values.")
if (!configuration.hasGuildConfig(guild.id)) {
respond("Guild configuration exists. To modify it use the commands to set values.", false)
return@execute
}

val prefix = args.first
configuration[guild.id.value]?.prefix = prefix
configuration[guild.id]?.prefix = prefix
configuration.save()
respond("Prefix set to: $prefix")
respond("Prefix set to: $prefix", false)
}
}

command("setRole") {
slash("setRole") {
description = "Set the role required to use this bot."
execute(RoleArg) {
if (!configuration.hasGuildConfig(guild.id.value)) {
respond("Guild configuration exists. To modify it use the commands to set values.")
if (!configuration.hasGuildConfig(guild.id)) {
respond("Guild configuration exists. To modify it use the commands to set values.", false)
return@execute
}

val requiredRole = args.first
configuration[guild.id.value]?.requiredRoleId = requiredRole.id.value
configuration[guild.id]?.requiredRoleId = requiredRole.id
configuration.save()
respond("Required role set to: ${requiredRole.name}")
respond("Required role set to: ${requiredRole.name}", false)
}


command("setReaction") {
slash("setReaction") {
description = "Set the reaction used to save messages"
execute(UnicodeEmojiArg) {
if (!configuration.hasGuildConfig(guild.id.value)) {
respond("Guild configuration exists. To modify it use the commands to set values.")
if (!configuration.hasGuildConfig(guild.id)) {
respond("Guild configuration exists. To modify it use the commands to set values.", false)
return@execute
}

val reaction = args.first
configuration[guild.id.value]?.bookmarkReaction = reaction.unicode
configuration[guild.id]?.bookmarkReaction = reaction.unicode
configuration.save()
respond("Reaction set to: $reaction")
respond("Reaction set to: $reaction", false)
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/me/ddivad/keeper/commands/OperationCommands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ import me.ddivad.keeper.dataclasses.Configuration
import me.ddivad.keeper.dataclasses.Permissions
import me.ddivad.keeper.services.StatisticsService
import me.ddivad.keeper.embeds.buildStatsEmbed
import me.jakejmattson.discordkt.api.commands.commands
import me.jakejmattson.discordkt.commands.commands

@Suppress("unused")
fun operationCommands(configuration: Configuration, statsService: StatisticsService) = commands("Operation", Permissions.STAFF) {
command("enable") {
slash("enable") {
description = "Enable the bot reactions"
execute {
if (!configuration.hasGuildConfig(guild.id.value)) {
if (!configuration.hasGuildConfig(guild.id)) {
respond("Guild configuration exists. To modify it use the commands to set values.")
return@execute
}
configuration[guild.id.value]?.enabled = true
configuration[guild.id]?.enabled = true
configuration.save()
respond("Bot enabled")
respond("Bot enabled", false)
}
}

command("disable") {
slash("disable") {
description = "Disabled the bot reactions"
execute {
if (!configuration.hasGuildConfig(guild.id.value)) {
if (!configuration.hasGuildConfig(guild.id)) {
respond("Guild configuration exists. To modify it use the commands to set values.")
return@execute
}
configuration[guild.id.value]?.enabled = false
configuration[guild.id]?.enabled = false
configuration.save()
respond("Bot disabled")
respond("Bot disabled", false)
}
}

command("stats") {
slash("stats") {
description = "View statistics about Keeper"
execute {
respond {
respond(false) {
buildStatsEmbed(guild, configuration, statsService)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package me.ddivad.keeper.conversations

import dev.kord.core.entity.Guild
import me.ddivad.keeper.dataclasses.Configuration
import me.jakejmattson.discordkt.api.arguments.EveryArg
import me.jakejmattson.discordkt.api.arguments.RoleArg
import me.jakejmattson.discordkt.api.arguments.UnicodeEmojiArg
import me.jakejmattson.discordkt.api.conversations.conversation
import me.jakejmattson.discordkt.arguments.EveryArg
import me.jakejmattson.discordkt.arguments.RoleArg
import me.jakejmattson.discordkt.arguments.UnicodeEmojiArg
import me.jakejmattson.discordkt.conversations.conversation

class ConfigurationConversation(private val configuration: Configuration) {
fun createConfigurationConversation(guild: Guild) = conversation {
Expand All @@ -14,5 +14,6 @@ class ConfigurationConversation(private val configuration: Configuration) {
val reaction = prompt(UnicodeEmojiArg, "Save message reaction:")

configuration.setup(guild, prefix, role, reaction.unicode)
respond("Guild setup")
}
}
20 changes: 11 additions & 9 deletions src/main/kotlin/me/ddivad/keeper/dataclasses/Configuration.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package me.ddivad.keeper.dataclasses

import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.Guild
import dev.kord.core.entity.Role
import kotlinx.serialization.Serializable
import me.jakejmattson.discordkt.api.dsl.Data
import me.jakejmattson.discordkt.dsl.Data
import me.jakejmattson.discordkt.extensions.toSnowflake

@Serializable
data class Configuration(val botOwner: Long = 394484823944593409,
data class Configuration(val botOwner: Snowflake = "394484823944593409".toSnowflake(),
var totalBookmarks: Int = 0,
val guildConfigurations: MutableMap<Long, GuildConfiguration> = mutableMapOf()) : Data() {
operator fun get(id: Long) = guildConfigurations[id]
val guildConfigurations: MutableMap<Snowflake, GuildConfiguration> = mutableMapOf()) : Data() {
operator fun get(id: Snowflake) = guildConfigurations[id]

fun setup(guild: Guild, prefix: String, role: Role, reaction: String) {
if (guildConfigurations[guild.id.value] != null) return
if (guildConfigurations[guild.id] != null) return

val newConfiguration = GuildConfiguration(prefix, role.id.value, reaction, true)
val newConfiguration = GuildConfiguration(prefix, role.id, reaction, true)

guildConfigurations[guild.id.value] = newConfiguration
guildConfigurations[guild.id] = newConfiguration
save()
}
fun hasGuildConfig(guildId: Long) = guildConfigurations.containsKey(guildId)
fun hasGuildConfig(guildId: Snowflake) = guildConfigurations.containsKey(guildId)
}

@Serializable
data class GuildConfiguration(
var prefix: String,
var requiredRoleId: Long,
var requiredRoleId: Snowflake,
var bookmarkReaction: String,
var enabled: Boolean,
var bookmarkCount: Int = 0
Expand Down
Loading

0 comments on commit d8ce624

Please sign in to comment.