Clean up Util.kt

This commit is contained in:
kageru 2019-09-17 22:59:55 +02:00
parent c49a122622
commit e7f47b7420
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
4 changed files with 36 additions and 41 deletions

View File

@ -39,6 +39,7 @@ dependencies {
implementation("org.javacord:javacord:3.0.4")
implementation("org.mapdb:mapdb:3.0.7")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.3.50")
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.4.1")
testImplementation("io.mockk:mockk:1.9.3")

View File

@ -34,10 +34,10 @@ object Kagebot {
}
}
private fun getSecret() = File("secret").readText().trim()
private val secret by lazy { File("secret").readText().trim() }
fun init() {
Globals.api = DiscordApiBuilder().setToken(getSecret()).login().join()
val api = DiscordApiBuilder().setToken(secret).login().join()
try {
ConfigParser.initialLoad(RawConfig.read())
} catch (e: IllegalArgumentException) {
@ -47,11 +47,12 @@ object Kagebot {
Runtime.getRuntime().addShutdownHook(Thread {
Log.info("Bot has been interrupted. Shutting down.")
Dao.close()
Globals.api.disconnect()
api.disconnect()
})
Log.info("kagebot Mk II running")
Globals.api.addMessageCreateListener { checked { it.process() } }
Config.features.eventFeatures().forEach { it.register(Globals.api) }
api.addMessageCreateListener { checked { it.process() } }
Config.features.eventFeatures().forEach { it.register(api) }
CronD.startAll()
Globals.api = api
}
}

View File

@ -22,12 +22,8 @@ object Util {
* Mimics the behavior of [Optional.ifPresent], but returns null if the optional is empty,
* allowing easier fallback behavior via Kotlins ?: operator.
*/
internal inline fun <T, R> Optional<T>.ifNotEmpty(op: (T) -> R): R? {
if (this.isPresent) {
return op(this.get())
}
return null
}
internal inline fun <T, R> Optional<T>.ifNotEmpty(op: (T) -> R): R? =
if (this.isPresent) op(this.get()) else null
fun hasOneOf(messageAuthor: MessageAuthor, roles: Set<Role>): Boolean {
return messageAuthor.asUser().ifNotEmpty { user ->
@ -43,13 +39,16 @@ object Util {
return when {
idOrName.isEntityId() -> server.getRoleById(idOrName).ifNotEmpty { it }
?: throw IllegalArgumentException("Role $idOrName not found.")
else -> server.getRolesByNameIgnoreCase(idOrName).let {
when (it.size) {
0 -> throw IllegalArgumentException("Role $idOrName not found.")
1 -> it[0]
else -> throw IllegalArgumentException("More than one role found with name $idOrName. Please specify the role ID instead")
}
}
else -> server.getRolesByNameIgnoreCase(idOrName).getOnlyElementOrError(idOrName)
}
}
private inline fun <reified T> List<T>.getOnlyElementOrError(identifier: String): T {
val className = T::class.simpleName!!
return when (size) {
0 -> throw IllegalArgumentException("$className $identifier not found.")
1 -> first()
else -> throw IllegalArgumentException("More than one ${className.toLowerCase()} found with name $identifier. Please specify the role ID instead")
}
}
@ -73,14 +72,14 @@ object Util {
try {
join()
} catch (e: CompletionException) {
Log.warn(
// we don’t care about this error, but I at least want to log it for debugging
Log.info(
"""Error during CompletableFuture:
|$e
|${e.localizedMessage}
|${e.stackTrace.joinToString("\n\t")}
""".trimMargin()
)
// we don’t care about this error, but I don’t want to spam stdout
}
return isCompletedExceptionally
}
@ -92,22 +91,12 @@ object Util {
?: throw IllegalArgumentException("Channel ID $idOrName not found.")
else -> if (idOrName.startsWith('@')) {
Globals.api.getCachedUserByDiscriminatedName(idOrName.removePrefix("@")).ifNotEmpty { user ->
val channelFuture = user.openPrivateChannel()
val channel = channelFuture.join()
if (channelFuture.isCompletedExceptionally) {
user.openPrivateChannel().joinOr {
throw IllegalArgumentException("Could not open private channel with user $idOrName for redirection.")
}
channel
}
?: throw IllegalArgumentException("Can’t find user $idOrName for redirection.")
} ?: throw IllegalArgumentException("Can’t find user $idOrName for redirection.")
} else {
server.getTextChannelsByName(idOrName).let {
when (it.size) {
0 -> throw IllegalArgumentException("Channel $idOrName not found.")
1 -> it[0]
else -> throw IllegalArgumentException("More than one channel found with name $idOrName. Please specify the channel ID instead")
}
}
server.getTextChannelsByName(idOrName).getOnlyElementOrError(idOrName)
}
}
}
@ -132,11 +121,7 @@ object Util {
}
}
fun userFromMessage(message: MessageCreateEvent): User? {
return message.messageAuthor.id.let { id ->
Config.server.getMemberById(id).orElse(null)
}
}
fun MessageCreateEvent.getUser(): User? = Config.server.getMemberById(messageAuthor.id).toNullable()
/**
* Convert a list of elements to pairs, retaining order.
@ -148,4 +133,12 @@ object Util {
Pair(next(), next())
}
}
private inline fun <T> CompletableFuture<T>.joinOr(op: () -> Nothing): T {
val value = join()
if (isCompletedExceptionally) {
op()
}
return value
}
}

View File

@ -2,6 +2,7 @@ package moe.kageru.kagebot.command
import moe.kageru.kagebot.Log
import moe.kageru.kagebot.Util
import moe.kageru.kagebot.Util.getUser
import moe.kageru.kagebot.config.RawAssignment
import org.javacord.api.event.message.MessageCreateEvent
@ -10,8 +11,7 @@ internal class RoleAssignment(rawAssignment: RawAssignment) {
Util.findRole(idOrName)
} ?: throw IllegalArgumentException("Can’t find role “${rawAssignment.role}")
fun assign(message: MessageCreateEvent) {
Util.userFromMessage(message)?.addRole(role, "Requested via command.")
fun assign(message: MessageCreateEvent) =
message.getUser()?.addRole(role, "Requested via command.")
?: Log.warn("Could not find user ${message.messageAuthor.name} for role assign")
}
}