Clean up Util.kt
This commit is contained in:
parent
c49a122622
commit
e7f47b7420
|
@ -39,6 +39,7 @@ dependencies {
|
||||||
implementation("org.javacord:javacord:3.0.4")
|
implementation("org.javacord:javacord:3.0.4")
|
||||||
implementation("org.mapdb:mapdb:3.0.7")
|
implementation("org.mapdb:mapdb:3.0.7")
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1")
|
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.kotlintest:kotlintest-runner-junit5:3.4.1")
|
||||||
testImplementation("io.mockk:mockk:1.9.3")
|
testImplementation("io.mockk:mockk:1.9.3")
|
||||||
|
|
|
@ -34,10 +34,10 @@ object Kagebot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSecret() = File("secret").readText().trim()
|
private val secret by lazy { File("secret").readText().trim() }
|
||||||
|
|
||||||
fun init() {
|
fun init() {
|
||||||
Globals.api = DiscordApiBuilder().setToken(getSecret()).login().join()
|
val api = DiscordApiBuilder().setToken(secret).login().join()
|
||||||
try {
|
try {
|
||||||
ConfigParser.initialLoad(RawConfig.read())
|
ConfigParser.initialLoad(RawConfig.read())
|
||||||
} catch (e: IllegalArgumentException) {
|
} catch (e: IllegalArgumentException) {
|
||||||
|
@ -47,11 +47,12 @@ object Kagebot {
|
||||||
Runtime.getRuntime().addShutdownHook(Thread {
|
Runtime.getRuntime().addShutdownHook(Thread {
|
||||||
Log.info("Bot has been interrupted. Shutting down.")
|
Log.info("Bot has been interrupted. Shutting down.")
|
||||||
Dao.close()
|
Dao.close()
|
||||||
Globals.api.disconnect()
|
api.disconnect()
|
||||||
})
|
})
|
||||||
Log.info("kagebot Mk II running")
|
Log.info("kagebot Mk II running")
|
||||||
Globals.api.addMessageCreateListener { checked { it.process() } }
|
api.addMessageCreateListener { checked { it.process() } }
|
||||||
Config.features.eventFeatures().forEach { it.register(Globals.api) }
|
Config.features.eventFeatures().forEach { it.register(api) }
|
||||||
CronD.startAll()
|
CronD.startAll()
|
||||||
|
Globals.api = api
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,8 @@ object Util {
|
||||||
* Mimics the behavior of [Optional.ifPresent], but returns null if the optional is empty,
|
* Mimics the behavior of [Optional.ifPresent], but returns null if the optional is empty,
|
||||||
* allowing easier fallback behavior via Kotlin’s ?: operator.
|
* allowing easier fallback behavior via Kotlin’s ?: operator.
|
||||||
*/
|
*/
|
||||||
internal inline fun <T, R> Optional<T>.ifNotEmpty(op: (T) -> R): R? {
|
internal inline fun <T, R> Optional<T>.ifNotEmpty(op: (T) -> R): R? =
|
||||||
if (this.isPresent) {
|
if (this.isPresent) op(this.get()) else null
|
||||||
return op(this.get())
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun hasOneOf(messageAuthor: MessageAuthor, roles: Set<Role>): Boolean {
|
fun hasOneOf(messageAuthor: MessageAuthor, roles: Set<Role>): Boolean {
|
||||||
return messageAuthor.asUser().ifNotEmpty { user ->
|
return messageAuthor.asUser().ifNotEmpty { user ->
|
||||||
|
@ -43,13 +39,16 @@ object Util {
|
||||||
return when {
|
return when {
|
||||||
idOrName.isEntityId() -> server.getRoleById(idOrName).ifNotEmpty { it }
|
idOrName.isEntityId() -> server.getRoleById(idOrName).ifNotEmpty { it }
|
||||||
?: throw IllegalArgumentException("Role $idOrName not found.")
|
?: throw IllegalArgumentException("Role $idOrName not found.")
|
||||||
else -> server.getRolesByNameIgnoreCase(idOrName).let {
|
else -> server.getRolesByNameIgnoreCase(idOrName).getOnlyElementOrError(idOrName)
|
||||||
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")
|
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 {
|
try {
|
||||||
join()
|
join()
|
||||||
} catch (e: CompletionException) {
|
} 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:
|
"""Error during CompletableFuture:
|
||||||
|$e
|
|$e
|
||||||
|${e.localizedMessage}
|
|${e.localizedMessage}
|
||||||
|${e.stackTrace.joinToString("\n\t")}
|
|${e.stackTrace.joinToString("\n\t")}
|
||||||
""".trimMargin()
|
""".trimMargin()
|
||||||
)
|
)
|
||||||
// we don’t care about this error, but I don’t want to spam stdout
|
|
||||||
}
|
}
|
||||||
return isCompletedExceptionally
|
return isCompletedExceptionally
|
||||||
}
|
}
|
||||||
|
@ -92,22 +91,12 @@ object Util {
|
||||||
?: throw IllegalArgumentException("Channel ID $idOrName not found.")
|
?: throw IllegalArgumentException("Channel ID $idOrName not found.")
|
||||||
else -> if (idOrName.startsWith('@')) {
|
else -> if (idOrName.startsWith('@')) {
|
||||||
Globals.api.getCachedUserByDiscriminatedName(idOrName.removePrefix("@")).ifNotEmpty { user ->
|
Globals.api.getCachedUserByDiscriminatedName(idOrName.removePrefix("@")).ifNotEmpty { user ->
|
||||||
val channelFuture = user.openPrivateChannel()
|
user.openPrivateChannel().joinOr {
|
||||||
val channel = channelFuture.join()
|
|
||||||
if (channelFuture.isCompletedExceptionally) {
|
|
||||||
throw IllegalArgumentException("Could not open private channel with user $idOrName for redirection.")
|
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 {
|
} else {
|
||||||
server.getTextChannelsByName(idOrName).let {
|
server.getTextChannelsByName(idOrName).getOnlyElementOrError(idOrName)
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,11 +121,7 @@ object Util {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun userFromMessage(message: MessageCreateEvent): User? {
|
fun MessageCreateEvent.getUser(): User? = Config.server.getMemberById(messageAuthor.id).toNullable()
|
||||||
return message.messageAuthor.id.let { id ->
|
|
||||||
Config.server.getMemberById(id).orElse(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a list of elements to pairs, retaining order.
|
* Convert a list of elements to pairs, retaining order.
|
||||||
|
@ -148,4 +133,12 @@ object Util {
|
||||||
Pair(next(), next())
|
Pair(next(), next())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private inline fun <T> CompletableFuture<T>.joinOr(op: () -> Nothing): T {
|
||||||
|
val value = join()
|
||||||
|
if (isCompletedExceptionally) {
|
||||||
|
op()
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package moe.kageru.kagebot.command
|
||||||
|
|
||||||
import moe.kageru.kagebot.Log
|
import moe.kageru.kagebot.Log
|
||||||
import moe.kageru.kagebot.Util
|
import moe.kageru.kagebot.Util
|
||||||
|
import moe.kageru.kagebot.Util.getUser
|
||||||
import moe.kageru.kagebot.config.RawAssignment
|
import moe.kageru.kagebot.config.RawAssignment
|
||||||
import org.javacord.api.event.message.MessageCreateEvent
|
import org.javacord.api.event.message.MessageCreateEvent
|
||||||
|
|
||||||
|
@ -10,8 +11,7 @@ internal class RoleAssignment(rawAssignment: RawAssignment) {
|
||||||
Util.findRole(idOrName)
|
Util.findRole(idOrName)
|
||||||
} ?: throw IllegalArgumentException("Can’t find role “${rawAssignment.role}”")
|
} ?: throw IllegalArgumentException("Can’t find role “${rawAssignment.role}”")
|
||||||
|
|
||||||
fun assign(message: MessageCreateEvent) {
|
fun assign(message: MessageCreateEvent) =
|
||||||
Util.userFromMessage(message)?.addRole(role, "Requested via command.")
|
message.getUser()?.addRole(role, "Requested via command.")
|
||||||
?: Log.warn("Could not find user ${message.messageAuthor.name} for role assign")
|
?: Log.warn("Could not find user ${message.messageAuthor.name} for role assign")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user