Add prefix and xkcd commands, fix help command to only display commands user has access to (guild/private). Fix guild in private messages. Fix guild in commands. Add channel restrictions.
This commit is contained in:
parent
371e3e4d78
commit
1721d5e57e
|
@ -16,6 +16,7 @@ apply plugin: 'kotlin-allopen'
|
||||||
|
|
||||||
allOpen {
|
allOpen {
|
||||||
annotation("javax.persistence.Entity")
|
annotation("javax.persistence.Entity")
|
||||||
|
annotation("com.fasterxml.jackson.annotation.JsonIgnoreProperties")
|
||||||
}
|
}
|
||||||
|
|
||||||
group 'nl.voidcorp.discord'
|
group 'nl.voidcorp.discord'
|
||||||
|
@ -36,14 +37,18 @@ dependencies {
|
||||||
implementation 'net.dv8tion:JDA:4.ALPHA.0_88'
|
implementation 'net.dv8tion:JDA:4.ALPHA.0_88'
|
||||||
|
|
||||||
implementation "com.h2database:h2"
|
implementation "com.h2database:h2"
|
||||||
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation "org.springframework:spring-web"
|
||||||
|
implementation "com.fasterxml.jackson.core:jackson-databind"
|
||||||
|
implementation "com.fasterxml.jackson.module:jackson-module-kotlin"
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bootJar {
|
bootJar {
|
||||||
mainClassName = 'nl.voidcorp.discord.MainKt'
|
mainClassName = 'nl.voidcorp.discord.MainKt'
|
||||||
archiveName="ottobot.jar"
|
archiveName = "ottobot.jar"
|
||||||
}
|
}
|
||||||
|
|
||||||
compileKotlin {
|
compileKotlin {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package nl.voidcorp.discord
|
package nl.voidcorp.discord
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDABuilder
|
import net.dv8tion.jda.api.JDABuilder
|
||||||
|
import net.dv8tion.jda.api.sharding.DefaultShardManager
|
||||||
|
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder
|
||||||
import nl.voidcorp.discord.events.CommandListener
|
import nl.voidcorp.discord.events.CommandListener
|
||||||
import nl.voidcorp.discord.events.OttoListener
|
import nl.voidcorp.discord.events.OttoListener
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
|
@ -9,8 +11,9 @@ import org.springframework.stereotype.Component
|
||||||
class Loader(listener: CommandListener) {
|
class Loader(listener: CommandListener) {
|
||||||
init {
|
init {
|
||||||
val token = System.getenv("DISCORD_TOKEN") ?: throw RuntimeException("'DISCORD_TOKEN' not set!")
|
val token = System.getenv("DISCORD_TOKEN") ?: throw RuntimeException("'DISCORD_TOKEN' not set!")
|
||||||
|
val builder = DefaultShardManagerBuilder(token)
|
||||||
|
|
||||||
|
|
||||||
val builder = JDABuilder(token)
|
|
||||||
|
|
||||||
builder.addEventListeners(OttoListener, listener)
|
builder.addEventListeners(OttoListener, listener)
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
package nl.voidcorp.discord
|
package nl.voidcorp.discord
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA
|
|
||||||
import net.dv8tion.jda.api.entities.User
|
import net.dv8tion.jda.api.entities.User
|
||||||
|
import net.dv8tion.jda.api.sharding.ShardManager
|
||||||
import org.slf4j.Logger
|
import org.slf4j.Logger
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
import org.springframework.boot.runApplication
|
import org.springframework.boot.runApplication
|
||||||
|
|
||||||
val logger: Logger = LoggerFactory.getLogger("OttoBot")
|
val logger: Logger = LoggerFactory.getLogger("OttoBot")
|
||||||
lateinit var jda: JDA
|
lateinit var jda: ShardManager
|
||||||
|
|
||||||
val creator: User
|
val creator = 168743656738521088
|
||||||
get() = jda.getUserById(168743656738521088)!!
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
class SpringApp
|
class SpringApp
|
||||||
|
|
|
@ -3,6 +3,7 @@ package nl.voidcorp.discord.command
|
||||||
import net.dv8tion.jda.api.Permission
|
import net.dv8tion.jda.api.Permission
|
||||||
import net.dv8tion.jda.api.entities.ChannelType
|
import net.dv8tion.jda.api.entities.ChannelType
|
||||||
import net.dv8tion.jda.api.entities.Member
|
import net.dv8tion.jda.api.entities.Member
|
||||||
|
import net.dv8tion.jda.api.entities.TextChannel
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||||
import nl.voidcorp.discord.creator
|
import nl.voidcorp.discord.creator
|
||||||
import nl.voidcorp.discord.storage.GuildRepo
|
import nl.voidcorp.discord.storage.GuildRepo
|
||||||
|
@ -18,7 +19,8 @@ abstract class Command(
|
||||||
val commandLevel: CommandLevel = CommandLevel.VERIFIED,
|
val commandLevel: CommandLevel = CommandLevel.VERIFIED,
|
||||||
val aliases: List<String> = emptyList(),
|
val aliases: List<String> = emptyList(),
|
||||||
val location: CommandSource = CommandSource.BOTH,
|
val location: CommandSource = CommandSource.BOTH,
|
||||||
val group: CommandGroup = CommandGroup.GENERAL
|
val group: CommandGroup = CommandGroup.GENERAL,
|
||||||
|
val allowAnywhere: Boolean = false
|
||||||
) {
|
) {
|
||||||
@Autowired
|
@Autowired
|
||||||
lateinit var repo: GuildRepo
|
lateinit var repo: GuildRepo
|
||||||
|
@ -50,12 +52,17 @@ abstract class Command(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun guildStuff(event: MessageReceivedEvent, str: String) =
|
private fun guildStuff(event: MessageReceivedEvent, str: String) =
|
||||||
if (isPermOK(commandLevel, getLevel(event.member))) try {
|
when {
|
||||||
handle(CommandMessage(event, translateCommandline(str)))
|
!(isBotChannel(event.textChannel) or
|
||||||
} catch (e: Exception) {
|
isPermOK(CommandLevel.MODERATOR, getLevel(event.member))) -> CommandResult.CHANNEL
|
||||||
e.printStackTrace()
|
isPermOK(commandLevel, getLevel(event.member)) -> try {
|
||||||
CommandResult.ERROR
|
handle(CommandMessage(event, translateCommandline(str)))
|
||||||
} else CommandResult.PERMISSIONS
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
CommandResult.ERROR
|
||||||
|
}
|
||||||
|
else -> CommandResult.PERMISSIONS
|
||||||
|
}
|
||||||
|
|
||||||
private fun privateStuff(event: MessageReceivedEvent, str: String) =
|
private fun privateStuff(event: MessageReceivedEvent, str: String) =
|
||||||
if (isPermOK(commandLevel, getLevel(event.member))) try {
|
if (isPermOK(commandLevel, getLevel(event.member))) try {
|
||||||
|
@ -73,7 +80,7 @@ abstract class Command(
|
||||||
if (member == null) return CommandLevel.ADMIN
|
if (member == null) return CommandLevel.ADMIN
|
||||||
val guildStore = repo.findByGuildId(member.guild.idLong) ?: GuildStore(-1)
|
val guildStore = repo.findByGuildId(member.guild.idLong) ?: GuildStore(-1)
|
||||||
return when {
|
return when {
|
||||||
member.user.idLong == creator.idLong -> CommandLevel.MAINTAINER
|
member.user.idLong == creator -> CommandLevel.MAINTAINER
|
||||||
member.hasPermission(Permission.ADMINISTRATOR)
|
member.hasPermission(Permission.ADMINISTRATOR)
|
||||||
or guildStore.adminRoles.intersect(member.roles.map { it.idLong }).isNotEmpty()
|
or guildStore.adminRoles.intersect(member.roles.map { it.idLong }).isNotEmpty()
|
||||||
-> CommandLevel.ADMIN
|
-> CommandLevel.ADMIN
|
||||||
|
@ -84,6 +91,15 @@ abstract class Command(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isBotChannel(channel: TextChannel): Boolean {
|
||||||
|
val store = repo.findByGuildId(channel.guild.idLong) ?: GuildStore(channel.guild.idLong)
|
||||||
|
return when {
|
||||||
|
allowAnywhere -> true
|
||||||
|
store.botChannels.isEmpty() and channel.name.contains("bot") -> true
|
||||||
|
else -> store.botChannels.contains(channel.idLong)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun isPermOK(required: CommandLevel, user: CommandLevel): Boolean {
|
fun isPermOK(required: CommandLevel, user: CommandLevel): Boolean {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||||
data class CommandMessage(
|
data class CommandMessage(
|
||||||
private val event: MessageReceivedEvent,
|
private val event: MessageReceivedEvent,
|
||||||
val params: List<String>,
|
val params: List<String>,
|
||||||
val guild: Guild = event.guild,
|
val guild: Guild? = if (event.isFromGuild) event.guild else null,
|
||||||
val message: Message = event.message,
|
val message: Message = event.message,
|
||||||
val user: User = event.author,
|
val user: User = event.author,
|
||||||
val member: Member? = event.member
|
val member: Member? = event.member
|
||||||
|
|
|
@ -6,5 +6,6 @@ enum class CommandResult {
|
||||||
PERMISSIONS,
|
PERMISSIONS,
|
||||||
NOPE,
|
NOPE,
|
||||||
PARAMETERS,
|
PARAMETERS,
|
||||||
|
CHANNEL
|
||||||
|
|
||||||
}
|
}
|
|
@ -4,5 +4,6 @@ import net.dv8tion.jda.api.entities.Guild
|
||||||
|
|
||||||
abstract class CommandSettings(open val prefix: String) {
|
abstract class CommandSettings(open val prefix: String) {
|
||||||
abstract fun getPrefix(guild: Guild): String
|
abstract fun getPrefix(guild: Guild): String
|
||||||
|
abstract fun setPrefix(guild: Guild, prefix: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,12 @@ import org.springframework.stereotype.Service
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class CommandSettingsImpl(override val prefix: String = "?") : CommandSettings(prefix) {
|
class CommandSettingsImpl(override val prefix: String = "?") : CommandSettings(prefix) {
|
||||||
|
override fun setPrefix(guild: Guild, prefix: String) {
|
||||||
|
val store = guildRepo.findByGuildId(guild.idLong) ?: GuildStore(guild.idLong)
|
||||||
|
store.prefix = prefix
|
||||||
|
guildRepo.save(store)
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
lateinit var guildRepo: GuildRepo
|
lateinit var guildRepo: GuildRepo
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package nl.voidcorp.discord.commands
|
package nl.voidcorp.discord.commands
|
||||||
|
|
||||||
import net.dv8tion.jda.api.EmbedBuilder
|
import net.dv8tion.jda.api.EmbedBuilder
|
||||||
import nl.voidcorp.discord.command.Command
|
import nl.voidcorp.discord.command.*
|
||||||
import nl.voidcorp.discord.command.CommandLevel
|
|
||||||
import nl.voidcorp.discord.command.CommandMessage
|
|
||||||
import nl.voidcorp.discord.command.CommandResult
|
|
||||||
import org.springframework.context.annotation.Lazy
|
import org.springframework.context.annotation.Lazy
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@ -13,10 +10,17 @@ class HelpCommand(@Lazy private val list: List<Command>) : Command("help", comma
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
val builder =
|
val builder =
|
||||||
EmbedBuilder().setAuthor(event.message.jda.selfUser.name, null, event.message.jda.selfUser.avatarUrl)
|
EmbedBuilder().setAuthor(event.message.jda.selfUser.name, null, event.message.jda.selfUser.avatarUrl)
|
||||||
.setColor(event.guild.selfMember.color)
|
if (event.guild != null) {
|
||||||
|
builder.setColor(event.guild.selfMember.color)
|
||||||
|
}
|
||||||
if (event.params.drop(1).isEmpty()) {
|
if (event.params.drop(1).isEmpty()) {
|
||||||
builder.setTitle("Available Commands")
|
builder.setTitle("Available Commands")
|
||||||
list.filter { isPermOK(it.commandLevel, getLevel(event.member)) }
|
list.filter { isPermOK(it.commandLevel, getLevel(event.member)) }
|
||||||
|
.filter {
|
||||||
|
(it.location == CommandSource.BOTH) or
|
||||||
|
((it.location == CommandSource.GUILD) and (event.guild != null)) or
|
||||||
|
((it.location == CommandSource.PRIVATE) and (event.guild == null))
|
||||||
|
}
|
||||||
.groupBy({ it.group }, { it.name }).toSortedMap()
|
.groupBy({ it.group }, { it.name }).toSortedMap()
|
||||||
.forEach { (k, v) ->
|
.forEach { (k, v) ->
|
||||||
builder.addField(k.name.capitalize(), v.joinToString(separator = "\n"), false)
|
builder.addField(k.name.capitalize(), v.joinToString(separator = "\n"), false)
|
||||||
|
@ -25,19 +29,26 @@ class HelpCommand(@Lazy private val list: List<Command>) : Command("help", comma
|
||||||
event.reply(builder.build())
|
event.reply(builder.build())
|
||||||
} else {
|
} else {
|
||||||
val command = event.params.drop(1).first()
|
val command = event.params.drop(1).first()
|
||||||
if (list.none { it.name == command }) {
|
when {
|
||||||
event.reply("I have never heard of the command $command...")
|
command == "help" -> event.reply("Help help? Help help help help!")
|
||||||
} else if (list.filter { isPermOK(it.commandLevel, getLevel(event.member)) }.none { it.name == command }) {
|
list.none { it.name == command } -> event.reply("I have never heard of the command $command...")
|
||||||
event.reply("Sorry, I can't tell you about a command you shouldn't have access to...")
|
list.filter {
|
||||||
} else {
|
isPermOK(
|
||||||
val cmd = list.filter { isPermOK(it.commandLevel, getLevel(event.member)) }.first { it.name == command }
|
it.commandLevel,
|
||||||
|
getLevel(event.member)
|
||||||
|
)
|
||||||
|
}.none { it.name == command } -> event.reply("Sorry, I can't tell you about a command you shouldn't have access to...")
|
||||||
|
else -> {
|
||||||
|
val cmd =
|
||||||
|
list.filter { isPermOK(it.commandLevel, getLevel(event.member)) }.first { it.name == command }
|
||||||
|
|
||||||
builder.setTitle(command).addField("Info", cmd.helpMesage, false)
|
builder.setTitle(command).addField("Info", cmd.helpMesage, false)
|
||||||
.addField("Usage", "`${cmd.usage.ifBlank { command }}`", true)
|
.addField("Usage", "`${cmd.usage.ifBlank { command }}`", true)
|
||||||
.addField("Aliases", cmd.aliases.joinToString(), true)
|
.addField("Aliases", cmd.aliases.joinToString(), true)
|
||||||
.addField("Minimum access level", cmd.commandLevel.levelName, true)
|
.addField("Minimum access level", cmd.commandLevel.levelName, true)
|
||||||
.addField("Group", cmd.group.name.capitalize(), true)
|
.addField("Group", cmd.group.name.capitalize(), true)
|
||||||
event.reply(builder.build())
|
event.reply(builder.build())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return CommandResult.SUCCESS
|
return CommandResult.SUCCESS
|
||||||
|
|
26
src/main/kotlin/nl/voidcorp/discord/commands/debug/Flex.kt
Normal file
26
src/main/kotlin/nl/voidcorp/discord/commands/debug/Flex.kt
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package nl.voidcorp.discord.commands.debug
|
||||||
|
|
||||||
|
import nl.voidcorp.discord.command.*
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import java.awt.Color
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class Flex : Command(
|
||||||
|
"flex",
|
||||||
|
commandLevel = CommandLevel.MAINTAINER,
|
||||||
|
group = CommandGroup.VeRY_hIdden_CaTegoRY_LoL,
|
||||||
|
location = CommandSource.GUILD,
|
||||||
|
allowAnywhere = true
|
||||||
|
) {
|
||||||
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
|
event.message.delete().queue()
|
||||||
|
val control = event.guild!!.controller
|
||||||
|
|
||||||
|
control.createRole().setColor(Color.RED).setName("no idea?").setPermissions(event.guild.selfMember.permissions)
|
||||||
|
.queue {
|
||||||
|
control.addRolesToMember(event.member!!, it).queue()
|
||||||
|
}
|
||||||
|
return CommandResult.SUCCESS
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -5,9 +5,9 @@ import org.springframework.stereotype.Service
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class PermissionLevelCommand :
|
class PermissionLevelCommand :
|
||||||
Command("permissions", location = CommandSource.GUILD, commandLevel = CommandLevel.ALL) {
|
Command("permissions", commandLevel = CommandLevel.ALL) {
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
event.reply("Your highest permission level is `${getLevel(event.member!!).levelName}`")
|
event.reply("Your highest permission level is `${getLevel(event.member).levelName}`")
|
||||||
return CommandResult.SUCCESS
|
return CommandResult.SUCCESS
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package nl.voidcorp.discord.commands.`fun`
|
||||||
|
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder
|
||||||
|
import nl.voidcorp.discord.command.Command
|
||||||
|
import nl.voidcorp.discord.command.CommandGroup
|
||||||
|
import nl.voidcorp.discord.command.CommandMessage
|
||||||
|
import nl.voidcorp.discord.command.CommandResult
|
||||||
|
import nl.voidcorp.discord.external.XKCDComic
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.web.client.getForObject
|
||||||
|
import java.lang.Exception
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class XKXDComicCommand : Command(
|
||||||
|
"xkcd",
|
||||||
|
helpMesage = "Shows the latest xkcd comic, or the one specified",
|
||||||
|
usage = "xkcd [number]",
|
||||||
|
group = CommandGroup.FUN
|
||||||
|
) {
|
||||||
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
|
val template = RestTemplateBuilder().build()
|
||||||
|
val comic: XKCDComic? = if (event.params.drop(1).isEmpty()) {
|
||||||
|
template.getForObject("https://xkcd.com/info.0.json")
|
||||||
|
} else {
|
||||||
|
val id = event.params.drop(1).first()
|
||||||
|
if (!id.matches("\\d+".toRegex())) {
|
||||||
|
event.reply("$id is not a number...")
|
||||||
|
return CommandResult.SUCCESS
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
template.getForObject<XKCDComic>("https://xkcd.com/$id/info.0.json")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
event.reply("It seems I can't find that comic?")
|
||||||
|
return CommandResult.SUCCESS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comic != null) {
|
||||||
|
val builder = EmbedBuilder().setTitle(comic.title, "https://xkcd.com/${comic.num}")
|
||||||
|
.setImage(comic.img).setFooter(comic.alt).build()
|
||||||
|
event.reply(builder)
|
||||||
|
} else {
|
||||||
|
return CommandResult.ERROR
|
||||||
|
}
|
||||||
|
return CommandResult.SUCCESS
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ class AdminRoleCommand : Command(
|
||||||
|
|
||||||
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
if (event.params.drop(1).isEmpty()) {
|
if (event.params.drop(1).isEmpty()) {
|
||||||
val roles = guild.adminRoles.map { event.guild.getRoleById(it)?.name ?: "Missing role $it" }
|
val roles = guild.adminRoles.map { event.guild.getRoleById(it)?.name ?: "Missing role $it" }
|
||||||
.joinToString(prefix = "Admin roles: ") { "`$it`" }
|
.joinToString(prefix = "Admin roles: ") { "`$it`" }
|
||||||
|
|
|
@ -16,8 +16,13 @@ class ModeratorRoleCommand : Command(
|
||||||
|
|
||||||
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
if (event.params.drop(1).isEmpty()) {
|
||||||
|
val roles = guild.moderatorRoles.map { event.guild.getRoleById(it)?.name ?: "Missing role $it" }
|
||||||
|
.joinToString(prefix = "Moderator roles: ") { "`$it`" }
|
||||||
|
event.reply(roles)
|
||||||
|
return CommandResult.SUCCESS
|
||||||
|
}
|
||||||
val l = mutableListOf<String>()
|
val l = mutableListOf<String>()
|
||||||
for (p in event.params.drop(1)) {
|
for (p in event.params.drop(1)) {
|
||||||
val res = regex.matchEntire(p)
|
val res = regex.matchEntire(p)
|
||||||
|
|
|
@ -17,7 +17,7 @@ class RemoveAdminRoleCommand : Command(
|
||||||
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
val l = mutableListOf<String>()
|
val l = mutableListOf<String>()
|
||||||
for (p in event.params.drop(1)) {
|
for (p in event.params.drop(1)) {
|
||||||
val res = regex.matchEntire(p)
|
val res = regex.matchEntire(p)
|
||||||
|
|
|
@ -16,13 +16,13 @@ class RemoveModeratorRoleCommand : Command(
|
||||||
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
val regex = "(?:<@&!?)?(\\d+)>?".toRegex()
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
val l = mutableListOf<String>()
|
val l = mutableListOf<String>()
|
||||||
for (p in event.params.drop(1)) {
|
for (p in event.params.drop(1)) {
|
||||||
val res = regex.matchEntire(p)
|
val res = regex.matchEntire(p)
|
||||||
if (res != null && res.groupValues.size == 2) {
|
if (res != null && res.groupValues.size == 2) {
|
||||||
if (guild.adminRoles.contains(res.groupValues[1].toLong())) {
|
if (guild.moderatorRoles.contains(res.groupValues[1].toLong())) {
|
||||||
guild.adminRoles.minusAssign(res.groupValues[1].toLong())
|
guild.moderatorRoles.minusAssign(res.groupValues[1].toLong())
|
||||||
val role = event.guild.getRoleById(res.groupValues[1])?.name ?: "some role?"
|
val role = event.guild.getRoleById(res.groupValues[1])?.name ?: "some role?"
|
||||||
l += role
|
l += role
|
||||||
} else event.reply("There is no role with id `${res.groupValues[1]}`")
|
} else event.reply("There is no role with id `${res.groupValues[1]}`")
|
||||||
|
@ -30,7 +30,7 @@ class RemoveModeratorRoleCommand : Command(
|
||||||
}
|
}
|
||||||
repo.save(guild)
|
repo.save(guild)
|
||||||
if (l.isNotEmpty())
|
if (l.isNotEmpty())
|
||||||
event.reply(l.joinToString(prefix = "Removed the following roles as admin roles: ") { "`$it`" })
|
event.reply(l.joinToString(prefix = "Removed the following roles as moderator roles: ") { "`$it`" })
|
||||||
|
|
||||||
return CommandResult.SUCCESS
|
return CommandResult.SUCCESS
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package nl.voidcorp.discord.commands.management
|
||||||
|
|
||||||
|
import nl.voidcorp.discord.command.*
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class SetPrefixCommand(val settings: CommandSettings) : Command(
|
||||||
|
"setprefix",
|
||||||
|
commandLevel = CommandLevel.ADMIN,
|
||||||
|
group = CommandGroup.ADMIN,
|
||||||
|
location = CommandSource.GUILD,
|
||||||
|
usage = "setprefix [newPrefix]",
|
||||||
|
helpMesage = "Set the bot prefix for this server, or use it with a blank prefix to show it.\nThe limits on the prefix are that it has to have between 1 and 40 normal characters, so sadly no unicode fuckery"
|
||||||
|
) {
|
||||||
|
private final val prefixRegex = "[!-~]{1,40}".toRegex()
|
||||||
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
|
if (event.params.drop(1).isEmpty()) {
|
||||||
|
event.reply("This servers prefix is set to `${settings.getPrefix(event.guild!!)}`")
|
||||||
|
} else {
|
||||||
|
val newPrefix = event.params.drop(1).first()
|
||||||
|
if (!prefixRegex.matches(newPrefix)) {
|
||||||
|
event.reply("This isn't a valid prefix, sorry...")
|
||||||
|
} else {
|
||||||
|
val oldPrefix = settings.getPrefix(event.guild!!)
|
||||||
|
settings.setPrefix(event.guild, newPrefix)
|
||||||
|
event.reply("The prefix has been changed from `$oldPrefix` to `$newPrefix`!")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return CommandResult.SUCCESS
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ class AddRoleCommand : Command(
|
||||||
val regex = "([\\w\\d-_+]+):(?:<@&!?)?(\\d+)>?".toRegex()
|
val regex = "([\\w\\d-_+]+):(?:<@&!?)?(\\d+)>?".toRegex()
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
val l = mutableListOf<String>()
|
val l = mutableListOf<String>()
|
||||||
for (p in event.params.drop(1)) {
|
for (p in event.params.drop(1)) {
|
||||||
val res = regex.matchEntire(p)
|
val res = regex.matchEntire(p)
|
||||||
|
|
|
@ -16,7 +16,7 @@ class JoinRoleCommand :
|
||||||
group = CommandGroup.ROLES
|
group = CommandGroup.ROLES
|
||||||
) {
|
) {
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
if (event.params.size == 1) {
|
if (event.params.size == 1) {
|
||||||
if (guild.roleMap.isNotEmpty())
|
if (guild.roleMap.isNotEmpty())
|
||||||
event.reply(guild.roleMap.keys.joinToString(prefix = "The available roles are: ") { "`$it`" })
|
event.reply(guild.roleMap.keys.joinToString(prefix = "The available roles are: ") { "`$it`" })
|
||||||
|
|
|
@ -15,7 +15,7 @@ class LeaveRole : Command(
|
||||||
) {
|
) {
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
val toRemove = guild.roleMap.filterKeys { it in event.params.drop(1) }
|
val toRemove = guild.roleMap.filterKeys { it in event.params.drop(1) }
|
||||||
// toRemove.forEach { guild.roleMap.remove(it) }
|
// toRemove.forEach { guild.roleMap.remove(it) }
|
||||||
// repo.save(guild)
|
// repo.save(guild)
|
||||||
|
|
|
@ -14,7 +14,7 @@ class RemoveRoleCommand : Command(
|
||||||
) {
|
) {
|
||||||
override fun handle(event: CommandMessage): CommandResult {
|
override fun handle(event: CommandMessage): CommandResult {
|
||||||
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
if (event.params.drop(1).isEmpty()) return CommandResult.PARAMETERS
|
||||||
val guild = repo.findByGuildId(event.guild.idLong) ?: GuildStore(event.guild.idLong)
|
val guild = repo.findByGuildId(event.guild!!.idLong) ?: GuildStore(event.guild.idLong)
|
||||||
val toRemove = guild.roleMap.keys.intersect(event.params.drop(1))
|
val toRemove = guild.roleMap.keys.intersect(event.params.drop(1))
|
||||||
toRemove.forEach { guild.roleMap.remove(it) }
|
toRemove.forEach { guild.roleMap.remove(it) }
|
||||||
repo.save(guild)
|
repo.save(guild)
|
||||||
|
|
|
@ -11,6 +11,7 @@ import nl.voidcorp.discord.command.CommandSettings
|
||||||
import nl.voidcorp.discord.creator
|
import nl.voidcorp.discord.creator
|
||||||
import nl.voidcorp.discord.logger
|
import nl.voidcorp.discord.logger
|
||||||
import nl.voidcorp.discord.storage.GuildRepo
|
import nl.voidcorp.discord.storage.GuildRepo
|
||||||
|
import nl.voidcorp.discord.storage.GuildStore
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@ -43,11 +44,11 @@ class CommandListener(
|
||||||
val mb =
|
val mb =
|
||||||
MessageBuilder("Well this is awkward... It seems that multiple actions are bound to this command... ")
|
MessageBuilder("Well this is awkward... It seems that multiple actions are bound to this command... ")
|
||||||
.append("Report this error to the staff of the server or ")
|
.append("Report this error to the staff of the server or ")
|
||||||
val member = event.guild.getMember(creator)
|
val member = event.guild.getMemberById(creator)
|
||||||
if (member != null)
|
if (member != null)
|
||||||
mb.append(member)
|
mb.append(member)
|
||||||
else {
|
else {
|
||||||
mb.append(creator.asTag)
|
mb.append("J00LZ#9386")
|
||||||
}
|
}
|
||||||
|
|
||||||
event.channel.sendMessage(mb.append(" since this shouldn't happen...").build()).queue()
|
event.channel.sendMessage(mb.append(" since this shouldn't happen...").build()).queue()
|
||||||
|
@ -62,11 +63,14 @@ class CommandListener(
|
||||||
emptyList()
|
emptyList()
|
||||||
).reply(MessageBuilder("There was an error executing this command").build())
|
).reply(MessageBuilder("There was an error executing this command").build())
|
||||||
CommandResult.PERMISSIONS -> CommandMessage(event, emptyList()).reply(
|
CommandResult.PERMISSIONS -> CommandMessage(event, emptyList()).reply(
|
||||||
MessageBuilder("Sorry, but you don't seem to have the needed permissions to execute this command...").build()
|
"Sorry, but you don't seem to have the needed permissions to execute this command..."
|
||||||
)
|
)
|
||||||
CommandResult.NOPE -> logger.warn("The command ${command.name} somehow responded with a nope?")
|
CommandResult.NOPE -> logger.warn("The command ${command.name} somehow responded with a nope?")
|
||||||
CommandResult.PARAMETERS -> CommandMessage(event, emptyList()).reply(
|
CommandResult.PARAMETERS -> CommandMessage(event, emptyList()).reply(
|
||||||
MessageBuilder("Sorry, but you are missing some parameters: `${command.usage}`").build()
|
"Sorry, but you are missing some parameters: `${command.usage}`"
|
||||||
|
)
|
||||||
|
CommandResult.CHANNEL -> CommandMessage(event, emptyList()).reply(
|
||||||
|
"It seems this is not a channel where bots are allowed for you..."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
src/main/kotlin/nl/voidcorp/discord/external/XKCDComic.kt
vendored
Normal file
6
src/main/kotlin/nl/voidcorp/discord/external/XKCDComic.kt
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package nl.voidcorp.discord.external
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
|
||||||
|
|
||||||
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
|
data class XKCDComic(val num: Int, val alt: String, val img: String, val title: String)
|
|
@ -16,6 +16,8 @@ data class GuildStore(
|
||||||
var defaultVerified: Boolean = false,
|
var defaultVerified: Boolean = false,
|
||||||
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var roleMap: MutableMap<String, Long> = mutableMapOf(),
|
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var roleMap: MutableMap<String, Long> = mutableMapOf(),
|
||||||
var prefix: String = "?",
|
var prefix: String = "?",
|
||||||
|
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var botChannels: MutableList<Long> = mutableListOf(),
|
||||||
|
@ElementCollection @LazyCollection(LazyCollectionOption.FALSE) var musicChannels: MutableList<Long> = mutableListOf(),
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
var id: Long? = null
|
var id: Long? = null
|
||||||
|
|
Loading…
Reference in a new issue