Properly await future before checking their return code

This commit is contained in:
kageru 2019-06-13 00:19:02 +02:00
parent 7ebafc9958
commit 62bdb582de
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
4 changed files with 14 additions and 3 deletions

View File

@ -26,7 +26,7 @@ class Kagebot {
Globals.config.features.welcome!!.let { welcome ->
val message = event.user.sendMessage(welcome.embed)
// If the user disabled direct messages, try the fallback (if defined)
if (message.isCompletedExceptionally
if (!Util.wasSuccessful(message)
&& welcome.fallbackChannel != null
&& welcome.fallbackMessage != null
) {

View File

@ -6,6 +6,8 @@ import org.javacord.api.entity.channel.TextChannel
import org.javacord.api.entity.message.MessageAuthor
import org.javacord.api.entity.permission.Role
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
object Util {
inline fun <T> T.doIf(condition: (T) -> Boolean, op: (T) -> T): T {
@ -47,6 +49,15 @@ object Util {
}
}
fun <T> wasSuccessful(future: CompletableFuture<T>): Boolean {
try {
future.join()
} catch (e: CompletionException) {
// we don’t care about this error, but I don’t want to spam stdout
}
return !future.isCompletedExceptionally
}
@Throws(IllegalArgumentException::class)
fun findChannel(idOrName: String): TextChannel {
return when {

View File

@ -62,7 +62,7 @@ class Redirect(rawRedirect: RawRedirect) {
}
}
if (target.sendMessage(embed).isCompletedExceptionally) {
if (Util.wasSuccessful(target.sendMessage(embed))) {
log.warning("Could not redirect message to channel $target")
}
}

View File

@ -13,7 +13,7 @@ messageDeleted = "Your message was deleted because it contained a banned word or
# If no fallback channel or message is specified, no fallback will be sent.
[feature.welcome]
enabled = true
fallbackChannel = 555097559023222825
fallbackChannel = "555097559023222825"
fallbackMessage = "@@ I would like to greet you, but I can’t. :("
# This is a list of pairs where the key is the title and the value the content of the paragraph.
# Do not use empty strings to get empty headings or paragraphs. The discord API rejects those.