replace CompletableFuture.failed with Option folding
This commit is contained in:
parent
ccf76e7c64
commit
f1b727662a
|
@ -1,7 +1,5 @@
|
|||
package moe.kageru.kagebot
|
||||
|
||||
import moe.kageru.kagebot.Util.failed
|
||||
import moe.kageru.kagebot.Util.toPairs
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import moe.kageru.kagebot.config.SystemSpec
|
||||
import org.javacord.api.entity.message.Message
|
||||
|
@ -33,9 +31,7 @@ object MessageUtil {
|
|||
setTimestampToNow()
|
||||
op()
|
||||
}
|
||||
val sent = sendMessage(embed)
|
||||
// for logging
|
||||
sent.failed()
|
||||
sendMessage(embed)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,4 +54,15 @@ object MessageUtil {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a list of elements to pairs, retaining order.
|
||||
* The last element is dropped if the input size is odd.
|
||||
* [1, 2, 3, 4, 5] -> [[1, 2], [3, 4]]
|
||||
*/
|
||||
private fun <T> Collection<T>.toPairs(): List<Pair<T, T>> = this.iterator().run {
|
||||
(0 until size / 2).map {
|
||||
Pair(next(), next())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,21 +59,7 @@ object Util {
|
|||
}
|
||||
}
|
||||
|
||||
fun <T> CompletableFuture<T>.failed(): Boolean {
|
||||
try {
|
||||
join()
|
||||
} catch (e: CompletionException) {
|
||||
// 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()
|
||||
)
|
||||
}
|
||||
return isCompletedExceptionally
|
||||
}
|
||||
fun <T> Optional<T>.asOption(): Option<T> = if (this.isPresent) Option.just(this.get()) else Option.empty()
|
||||
|
||||
fun findChannel(idOrName: String): Either<String, TextChannel> {
|
||||
return when {
|
||||
|
@ -87,8 +73,6 @@ object Util {
|
|||
}
|
||||
}
|
||||
|
||||
fun <T> Optional<T>.asOption(): Option<T> = if (this.isPresent) Option.just(this.get()) else Option.empty()
|
||||
|
||||
inline fun checked(op: (() -> Unit)) {
|
||||
try {
|
||||
op()
|
||||
|
@ -102,21 +86,10 @@ object Util {
|
|||
.addField("Error", "kagebot has encountered an error")
|
||||
.addField(
|
||||
"$e", """```
|
||||
${e.stackTrace.joinToString("\n")}
|
||||
${e.stackTrace.joinToString("\n\t")}
|
||||
```""".trimIndent().run { applyIf(length > 1800) { substring(1..1800) } }
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a list of elements to pairs, retaining order.
|
||||
* The last element is dropped if the input size is odd.
|
||||
* [1, 2, 3, 4, 5] -> [[1, 2], [3, 4]]
|
||||
*/
|
||||
fun <T> Collection<T>.toPairs(): List<Pair<T, T>> = this.iterator().run {
|
||||
(0 until size / 2).map {
|
||||
Pair(next(), next())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import moe.kageru.kagebot.Log
|
|||
import moe.kageru.kagebot.MessageUtil
|
||||
import moe.kageru.kagebot.Util
|
||||
import moe.kageru.kagebot.Util.applyIf
|
||||
import moe.kageru.kagebot.Util.failed
|
||||
import moe.kageru.kagebot.Util.asOption
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import moe.kageru.kagebot.config.LocalizationSpec
|
||||
import moe.kageru.kagebot.extensions.unwrap
|
||||
|
@ -33,8 +33,7 @@ class MessageRedirect(target: String, private val anonymous: Boolean = false) {
|
|||
}
|
||||
}
|
||||
|
||||
if (MessageUtil.sendEmbed(targetChannel, embed).failed()) {
|
||||
targetChannel.sendMessage("Error: could not redirect message.")
|
||||
if (MessageUtil.sendEmbed(targetChannel, embed).asOption().isEmpty()) {
|
||||
Log.warn("Could not redirect message to channel $targetChannel")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package moe.kageru.kagebot.features
|
|||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import moe.kageru.kagebot.Log
|
||||
import moe.kageru.kagebot.Util.failed
|
||||
import moe.kageru.kagebot.Util.asOption
|
||||
import moe.kageru.kagebot.config.Config
|
||||
import moe.kageru.kagebot.extensions.categoriesByName
|
||||
import moe.kageru.kagebot.persistence.Dao
|
||||
|
@ -38,14 +38,11 @@ class TempVCFeature(@JsonProperty("category") category: String? = null) : EventF
|
|||
}
|
||||
}
|
||||
|
||||
private fun deleteChannel(channel: ServerVoiceChannel) {
|
||||
val deletion = channel.delete("Empty temporary channel")
|
||||
if (deletion.failed()) {
|
||||
Log.warn("Attempted to delete temporary VC without the necessary permissions")
|
||||
} else {
|
||||
Dao.removeTemporaryVC(channel.idAsString)
|
||||
}
|
||||
}
|
||||
private fun deleteChannel(channel: ServerVoiceChannel) =
|
||||
channel.delete("Empty temporary channel").asOption().fold(
|
||||
{ Log.warn("Attempted to delete temporary VC without the necessary permissions") },
|
||||
{ Dao.removeTemporaryVC(channel.idAsString) }
|
||||
)
|
||||
|
||||
private fun createChannel(message: MessageCreateEvent, limit: Long) {
|
||||
val creation = Config.server.createVoiceChannelBuilder().apply {
|
||||
|
|
|
@ -4,7 +4,7 @@ import moe.kageru.kagebot.Log
|
|||
import moe.kageru.kagebot.MessageUtil
|
||||
import moe.kageru.kagebot.Util
|
||||
import moe.kageru.kagebot.Util.checked
|
||||
import moe.kageru.kagebot.Util.failed
|
||||
import moe.kageru.kagebot.Util.asOption
|
||||
import moe.kageru.kagebot.extensions.unwrap
|
||||
import org.javacord.api.DiscordApi
|
||||
import org.javacord.api.entity.channel.TextChannel
|
||||
|
@ -29,7 +29,7 @@ class WelcomeFeature(
|
|||
Log.info("User ${event.user.discriminatedName} joined")
|
||||
val message = event.user.sendMessage(embed)
|
||||
// If the user disabled direct messages, try the fallback (if defined)
|
||||
if (message.failed() && hasFallback()) {
|
||||
if (message.asOption().isEmpty() && hasFallback()) {
|
||||
fallbackChannel!!.sendMessage(
|
||||
fallbackMessage!!.replace("@@", MessageUtil.mention(event.user))
|
||||
)
|
||||
|
@ -38,9 +38,7 @@ class WelcomeFeature(
|
|||
|
||||
override fun handle(message: MessageCreateEvent) {
|
||||
embed?.let {
|
||||
val sent = MessageUtil.sendEmbed(message.channel, it)
|
||||
// invoke this for logging
|
||||
sent.failed()
|
||||
MessageUtil.sendEmbed(message.channel, it)
|
||||
} ?: Log.info("Welcome command was triggered, but no welcome embed defined.")
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user