Added configurable role assignments (closes #3)
This commit is contained in:
parent
344148cd03
commit
cb753b47ed
|
@ -7,6 +7,8 @@ import org.javacord.api.entity.channel.TextChannel
|
|||
import org.javacord.api.entity.message.MessageAuthor
|
||||
import org.javacord.api.entity.message.embed.EmbedBuilder
|
||||
import org.javacord.api.entity.permission.Role
|
||||
import org.javacord.api.entity.user.User
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
import java.awt.Color
|
||||
import java.util.*
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
@ -102,4 +104,10 @@ object Util {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun userFromMessage(message: MessageCreateEvent): User? {
|
||||
return message.messageAuthor.id.let { id ->
|
||||
Globals.server.getMemberById(id).orElse(null)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,21 +3,20 @@ package moe.kageru.kagebot.command
|
|||
import moe.kageru.kagebot.Globals.config
|
||||
import moe.kageru.kagebot.Log.log
|
||||
import moe.kageru.kagebot.MessageUtil
|
||||
import moe.kageru.kagebot.Util
|
||||
import moe.kageru.kagebot.config.RawMessageActions
|
||||
import moe.kageru.kagebot.config.RawRedirect
|
||||
import org.javacord.api.entity.channel.TextChannel
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
|
||||
class MessageActions(rawActions: RawMessageActions) {
|
||||
private val delete: Boolean = rawActions.delete
|
||||
private val redirect: Redirect? = rawActions.redirect?.let { Redirect(it) }
|
||||
private val redirect: MessageRedirect? = rawActions.redirect?.let(::MessageRedirect)
|
||||
private val assignment: RoleAssignment? = rawActions.assign?.let(::RoleAssignment)
|
||||
|
||||
fun run(message: MessageCreateEvent, command: Command) {
|
||||
if (delete) {
|
||||
deleteMessage(message)
|
||||
}
|
||||
redirect?.execute(message, command)
|
||||
assignment?.assign(message)
|
||||
}
|
||||
|
||||
private fun deleteMessage(message: MessageCreateEvent) {
|
||||
|
@ -36,34 +35,3 @@ class MessageActions(rawActions: RawMessageActions) {
|
|||
}
|
||||
}
|
||||
|
||||
class Redirect(rawRedirect: RawRedirect) {
|
||||
private val target: TextChannel = rawRedirect.target?.let(Util::findChannel)
|
||||
?: throw IllegalArgumentException("Every redirect needs to have a target.")
|
||||
private val anonymous: Boolean = rawRedirect.anonymous
|
||||
|
||||
fun execute(message: MessageCreateEvent, command: Command) {
|
||||
val embed = MessageUtil.getEmbedBuilder()
|
||||
.addField(
|
||||
config.localization.redirectedMessage,
|
||||
message.readableMessageContent.let { content ->
|
||||
when (command.matchType) {
|
||||
MatchType.PREFIX -> content.removePrefix(command.trigger).trim()
|
||||
else -> content
|
||||
}
|
||||
}
|
||||
)
|
||||
// No inlined if/else because the types are different.
|
||||
// Passing the full message author will also include the avatar in the embed.
|
||||
embed.apply {
|
||||
if (anonymous) {
|
||||
setAuthor("Anonymous")
|
||||
} else {
|
||||
setAuthor(message.messageAuthor)
|
||||
}
|
||||
}
|
||||
|
||||
if (Util.wasSuccessful(target.sendMessage(embed))) {
|
||||
log.warning("Could not redirect message to channel $target")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package moe.kageru.kagebot.command
|
||||
|
||||
import moe.kageru.kagebot.Globals.config
|
||||
import moe.kageru.kagebot.Log.log
|
||||
import moe.kageru.kagebot.MessageUtil
|
||||
import moe.kageru.kagebot.Util
|
||||
import moe.kageru.kagebot.config.RawRedirect
|
||||
import org.javacord.api.entity.channel.TextChannel
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
|
||||
internal class MessageRedirect(rawRedirect: RawRedirect) {
|
||||
private val target: TextChannel = rawRedirect.target?.let(Util::findChannel)
|
||||
?: throw IllegalArgumentException("Every redirect needs to have a target.")
|
||||
private val anonymous: Boolean = rawRedirect.anonymous
|
||||
|
||||
fun execute(message: MessageCreateEvent, command: Command) {
|
||||
val embed = MessageUtil.getEmbedBuilder()
|
||||
.addField(
|
||||
config.localization.redirectedMessage,
|
||||
message.readableMessageContent.let { content ->
|
||||
when (command.matchType) {
|
||||
MatchType.PREFIX -> content.removePrefix(command.trigger).trim()
|
||||
else -> content
|
||||
}
|
||||
}
|
||||
)
|
||||
// No inlined if/else because the types are different.
|
||||
// Passing the full message author will also include the avatar in the embed.
|
||||
embed.apply {
|
||||
if (anonymous) {
|
||||
setAuthor("Anonymous")
|
||||
} else {
|
||||
setAuthor(message.messageAuthor)
|
||||
}
|
||||
}
|
||||
|
||||
if (Util.wasSuccessful(target.sendMessage(embed))) {
|
||||
log.warning("Could not redirect message to channel $target")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package moe.kageru.kagebot.command
|
||||
|
||||
import moe.kageru.kagebot.Log.log
|
||||
import moe.kageru.kagebot.Util
|
||||
import moe.kageru.kagebot.config.RawAssignment
|
||||
import org.javacord.api.event.message.MessageCreateEvent
|
||||
|
||||
internal class RoleAssignment(rawAssignment: RawAssignment) {
|
||||
private val role = rawAssignment.role?.let { idOrName ->
|
||||
Util.findRole(idOrName)
|
||||
} ?: throw IllegalArgumentException("Can’t find role “${rawAssignment.role}”")
|
||||
|
||||
fun assign(message: MessageCreateEvent) {
|
||||
Util.userFromMessage(message)?.addRole(role, "Requested via command.")
|
||||
?: log.warning("Could not find user ${message.messageAuthor.name} for role assign")
|
||||
}
|
||||
}
|
|
@ -13,5 +13,6 @@ class RawCommand(
|
|||
)
|
||||
|
||||
class RawPermissions(val hasOneOf: List<String>?, val hasNoneOf: List<String>?, val onlyDM: Boolean)
|
||||
class RawMessageActions(val delete: Boolean, val redirect: RawRedirect?)
|
||||
class RawRedirect(val target: String?, val anonymous: Boolean)
|
||||
class RawMessageActions(val delete: Boolean, val redirect: RawRedirect?, val assign: RawAssignment?)
|
||||
class RawRedirect(val target: String?, val anonymous: Boolean)
|
||||
class RawAssignment(var role: String?)
|
|
@ -86,6 +86,11 @@ response = "redirected"
|
|||
target = "555097559023222825"
|
||||
anonymous = true
|
||||
|
||||
[[command]]
|
||||
trigger = "!assign"
|
||||
[command.action.assign]
|
||||
role = "new role"
|
||||
|
||||
[[command]]
|
||||
trigger = "!debug"
|
||||
feature = "debug"
|
||||
|
|
|
@ -12,14 +12,18 @@ import moe.kageru.kagebot.TestUtil
|
|||
import moe.kageru.kagebot.TestUtil.embedToString
|
||||
import moe.kageru.kagebot.TestUtil.messageableAuthor
|
||||
import moe.kageru.kagebot.TestUtil.mockMessage
|
||||
import moe.kageru.kagebot.TestUtil.prepareTestEnvironment
|
||||
import moe.kageru.kagebot.TestUtil.testMessageSuccess
|
||||
import moe.kageru.kagebot.TestUtil.withCommands
|
||||
import moe.kageru.kagebot.TestUtil.withLocalization
|
||||
import moe.kageru.kagebot.Util
|
||||
import org.javacord.api.entity.message.embed.EmbedBuilder
|
||||
import org.javacord.api.entity.permission.Role
|
||||
import org.javacord.api.entity.user.User
|
||||
import java.util.*
|
||||
|
||||
class CommandTest : StringSpec({
|
||||
TestUtil.prepareTestEnvironment()
|
||||
prepareTestEnvironment()
|
||||
"should match prefix command" {
|
||||
withCommands(
|
||||
"""
|
||||
|
@ -33,7 +37,7 @@ class CommandTest : StringSpec({
|
|||
}
|
||||
"should print embed for command" {
|
||||
val calls = mutableListOf<EmbedBuilder>()
|
||||
TestUtil.prepareTestEnvironment(calls)
|
||||
prepareTestEnvironment(calls)
|
||||
val heading = "heading 1"
|
||||
val content = "this is the first paragraph of the embed"
|
||||
withCommands(
|
||||
|
@ -237,7 +241,7 @@ class CommandTest : StringSpec({
|
|||
*/
|
||||
"should redirect" {
|
||||
val calls = mutableListOf<EmbedBuilder>()
|
||||
TestUtil.prepareTestEnvironment(calls)
|
||||
prepareTestEnvironment(calls)
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
|
@ -254,4 +258,22 @@ class CommandTest : StringSpec({
|
|||
embedToString(calls[0]) shouldContain "\"$message\""
|
||||
}
|
||||
}
|
||||
"should assign" {
|
||||
withCommands(
|
||||
"""
|
||||
[[command]]
|
||||
trigger = "!assign"
|
||||
[command.action.assign]
|
||||
role = "testrole"
|
||||
""".trimIndent()
|
||||
) {
|
||||
val roles = mutableListOf<Role>()
|
||||
val user = mockk<User> {
|
||||
every { addRole(capture(roles), "Requested via command.") } returns mockk()
|
||||
}
|
||||
every { Globals.server.getMemberById(1) } returns Optional.of(user)
|
||||
Kagebot.processMessage(mockMessage("!assign"))
|
||||
roles shouldBe mutableListOf(Util.findRole("testrole"))
|
||||
}
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user