From 8c39f6882f6bea374e9b64f9baca667d8f98faa6 Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Wed, 22 May 2019 23:17:12 +0200 Subject: [PATCH] disable ci, its broken. Add role commands. Fix database eager loading. Add error message in console. --- .gitlab-ci.yml => no.gitlab-ci.yml | 0 .../nl/voidcorp/discord/command/Command.kt | 2 + .../discord/command/CommandMessage.kt | 6 +-- .../discord/commands/AddRoleCommand.kt | 38 +++++++++++++++++++ .../discord/commands/ListRolesCommand.kt | 20 ++++++++++ .../commands/PermissionLevelCommand.kt | 4 +- .../nl/voidcorp/discord/storage/GuildStore.kt | 14 +++---- 7 files changed, 71 insertions(+), 13 deletions(-) rename .gitlab-ci.yml => no.gitlab-ci.yml (100%) create mode 100644 src/main/kotlin/nl/voidcorp/discord/commands/AddRoleCommand.kt create mode 100644 src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt diff --git a/.gitlab-ci.yml b/no.gitlab-ci.yml similarity index 100% rename from .gitlab-ci.yml rename to no.gitlab-ci.yml diff --git a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt index 690300d..034b963 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/Command.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/Command.kt @@ -52,12 +52,14 @@ abstract class Command( if (isPermOK(commandLevel, getLevel(event.member!!))) try { handle(CommandMessage(event, translateCommandline(str))) } catch (e: Exception) { + e.printStackTrace() CommandResult.ERROR } else CommandResult.PERMISSIONS private fun privateStuff(event: MessageReceivedEvent, str: String) = try { handle(CommandMessage(event, translateCommandline(str))) } catch (e: Exception) { + e.printStackTrace() CommandResult.ERROR } diff --git a/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt b/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt index 7499692..db51935 100644 --- a/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt +++ b/src/main/kotlin/nl/voidcorp/discord/command/CommandMessage.kt @@ -1,15 +1,13 @@ package nl.voidcorp.discord.command import net.dv8tion.jda.api.MessageBuilder -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.User +import net.dv8tion.jda.api.entities.* import net.dv8tion.jda.api.events.message.MessageReceivedEvent data class CommandMessage( private val event: MessageReceivedEvent, val params: List, + val guild: Guild = event.guild, val message: Message = event.message, val user: User = event.author, val member: Member? = event.member diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/AddRoleCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/AddRoleCommand.kt new file mode 100644 index 0000000..9b4e958 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/commands/AddRoleCommand.kt @@ -0,0 +1,38 @@ +package nl.voidcorp.discord.commands + +import nl.voidcorp.discord.command.* +import nl.voidcorp.discord.storage.GuildRepo +import nl.voidcorp.discord.storage.GuildStore +import org.springframework.stereotype.Service + +@Service +class AddRoleCommand(val guildRepo: GuildRepo) : Command( + "addrole", + usage = "addrole rolename:id [rolename:id...]", + commandLevel = CommandGroup.MODERATOR, + location = CommandSource.GUILD +) { + val regex = "([\\w\\d-_+]+):(?:<@&!?)?(\\d+)>?".toRegex() + override fun handle(event: CommandMessage): CommandResult { + val guild = guildRepo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong) + if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS + val l = mutableListOf() + for (p in event.params.drop(1)) { + val res = regex.matchEntire(p) + if (res != null && res.groupValues.size == 3) { + if (event.guild.getRoleById(res.groupValues[2]) != null) { + guild.roleMap[res.groupValues[1]] = res.groupValues[2].toLong() + l += res.groupValues[1] + } else { + event.reply("There is no role with id `${res.groupValues[2]}`") + } + } + } + guildRepo.save(guild) + if (l.isNotEmpty()) + event.reply(l.joinToString(prefix = "Added the following groups: ") { "`$it`" }) + else + event.reply("Added no groups...") + return CommandResult.SUCCESS + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt new file mode 100644 index 0000000..517e153 --- /dev/null +++ b/src/main/kotlin/nl/voidcorp/discord/commands/ListRolesCommand.kt @@ -0,0 +1,20 @@ +package nl.voidcorp.discord.commands + +import nl.voidcorp.discord.command.Command +import nl.voidcorp.discord.command.CommandMessage +import nl.voidcorp.discord.command.CommandResult +import nl.voidcorp.discord.storage.GuildRepo +import nl.voidcorp.discord.storage.GuildStore +import org.springframework.stereotype.Service + +@Service +class ListRolesCommand(val guildRepo: GuildRepo) : Command("listroles", aliases = listOf("roles")) { + override fun handle(event: CommandMessage): CommandResult { + val guild = guildRepo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong) + if (guild.roleMap.isNotEmpty()) + event.reply(guild.roleMap.keys.joinToString(prefix = "The available roles are: ") { "`$it`" }) + else + event.reply("There are no roles to pick here...") + return CommandResult.SUCCESS + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt b/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt index 1c7a17d..5b51879 100644 --- a/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt +++ b/src/main/kotlin/nl/voidcorp/discord/commands/PermissionLevelCommand.kt @@ -4,9 +4,9 @@ import nl.voidcorp.discord.command.Command import nl.voidcorp.discord.command.CommandMessage import nl.voidcorp.discord.command.CommandResult import nl.voidcorp.discord.command.CommandSource -import org.springframework.stereotype.Component +import org.springframework.stereotype.Service -@Component +@Service class PermissionLevelCommand : Command("permissions", location = CommandSource.GUILD) { override fun handle(event: CommandMessage): CommandResult { event.reply("Your highest permission level is `${getLevel(event.member!!).levelName}`") diff --git a/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt b/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt index 3204aa3..915e653 100644 --- a/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt +++ b/src/main/kotlin/nl/voidcorp/discord/storage/GuildStore.kt @@ -1,18 +1,18 @@ package nl.voidcorp.discord.storage -import javax.annotation.Generated -import javax.persistence.ElementCollection -import javax.persistence.Entity -import javax.persistence.Id +import org.hibernate.annotations.LazyCollection +import org.hibernate.annotations.LazyCollectionOption +import javax.persistence.* @Entity data class GuildStore( var guildId: Long, - @ElementCollection var moderatorRoles: MutableList = mutableListOf(), - @ElementCollection var adminRoles: MutableList = mutableListOf(), + @ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var moderatorRoles: MutableList = mutableListOf(), + @ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var adminRoles: MutableList = mutableListOf(), var defaultVerified: Boolean = false, + @ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var roleMap: MutableMap = mutableMapOf(), @Id - @Generated + @GeneratedValue var id: Long? = null ) \ No newline at end of file