Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Fix RoleArgument Regex #22

Open
wants to merge 2 commits into
base: 0.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import com.gitlab.kordlib.kordx.commands.argument.result.ArgumentResult
import com.gitlab.kordlib.kordx.commands.argument.SingleWordArgument
import com.gitlab.kordlib.kordx.commands.argument.result.WordResult

private val mentionRegex = Regex("""^<#&\d+>$""")
private val mentionRegex = Regex("""^<@&\d+>$""")

internal class InternalRoleArgument(
override val name: String = "Role"
) : SingleWordArgument<Role, MessageCreateEvent>() {

override suspend fun parse(word: String, context: MessageCreateEvent): WordResult<Role> {
val guildId = context.message.getGuildOrNull()?.id ?: return failure("Can't get role outside of guilds.")
val guildId = context.guildId ?: return failure("Can't get role outside of guilds.")
val number = word.toLongOrNull()
val snowflake = when {
number != null -> Snowflake(number)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.gitlab.kordlib.kordx.commands.kord.argument

import com.gitlab.kordlib.common.entity.Snowflake
import com.gitlab.kordlib.core.Kord
import com.gitlab.kordlib.core.event.message.MessageCreateEvent
import com.gitlab.kordlib.kordx.commands.argument.result.ArgumentResult
import io.mockk.*
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

internal class InternalRoleArgumentTest {

@Test
fun `role matches against role mention`(): Unit = runBlocking {
val roleId = 165511591545143296L
val text = "sample <@&$roleId> text"
val argument = RoleArgument
val guildId = 556525343595298826L

val kordMock: Kord = mockk {
every { defaultSupplier } returns mockk()

//You might have many questions about the validity and sanity of what is written here
//fear not, it works. Unless you're here because it doesn't work. In which case: fear, a lot.
coEvery { getGuild(Snowflake(any<Long>()), resources.defaultStrategy) } returns mockk {
coEvery { getRole(Snowflake(any<Long>())) } returns mockk role@{
every { [email protected] } answers object : Answer<Any> {
override fun answer(call: Call): Any = guildId
} as Answer<Snowflake>

every { [email protected] } answers object : Answer<Any> {
override fun answer(call: Call): Any = roleId
} as Answer<Snowflake>
}
}
}

val result = argument.parse(text, 6, MessageCreateEvent(
guildId = Snowflake(guildId),
member = mockk(),
message = mockk() {
every { kord } returns kordMock
},
supplier = kordMock.defaultSupplier,
shard = 0
))

require(result is ArgumentResult.Success) { "result is failure $result" }

assertEquals(roleId, result.item.id.longValue)
assertEquals(guildId, result.item.guildId.longValue)
}

}