disable ci, its broken. Add role commands. Fix database eager loading. Add error message in console.

This commit is contained in:
Julius de Jeu 2019-05-22 23:17:12 +02:00
parent d2dcf78374
commit 8c39f6882f
7 changed files with 71 additions and 13 deletions

View file

@ -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
}

View file

@ -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<String>,
val guild: Guild = event.guild,
val message: Message = event.message,
val user: User = event.author,
val member: Member? = event.member

View file

@ -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<String>()
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
}
}

View file

@ -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
}
}

View file

@ -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}`")

View file

@ -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<Long> = mutableListOf(),
@ElementCollection var adminRoles: MutableList<Long> = mutableListOf(),
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var moderatorRoles: MutableList<Long> = mutableListOf(),
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var adminRoles: MutableList<Long> = mutableListOf(),
var defaultVerified: Boolean = false,
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var roleMap: MutableMap<String, Long> = mutableMapOf(),
@Id
@Generated
@GeneratedValue
var id: Long? = null
)