Skip to content

Commit

Permalink
Merge pull request #15 from ddivad195/feat/reaction-error-handling
Browse files Browse the repository at this point in the history
feat: add error handling to sedning user dms
  • Loading branch information
ddivad195 authored Jan 28, 2022
2 parents d8ce624 + 1f99aa0 commit 32bab43
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/main/kotlin/me/ddivad/keeper/listeners/MessageListener.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.ddivad.keeper.listeners

import dev.kord.core.event.message.ReactionAddEvent
import dev.kord.rest.request.KtorRequestException
import dev.kord.x.emoji.Emojis
import dev.kord.x.emoji.addReaction
import me.ddivad.keeper.dataclasses.Configuration
Expand All @@ -9,24 +10,35 @@ import me.ddivad.keeper.embeds.buildSavedMessageEmbed
import me.jakejmattson.discordkt.dsl.listeners
import me.jakejmattson.discordkt.extensions.isSelf
import me.jakejmattson.discordkt.extensions.sendPrivateMessage
import mu.KotlinLogging

private val logger = KotlinLogging.logger { }

@Suppress("unused")
fun onGuildMessageReactionAddEvent(configuration: Configuration, statsService: StatisticsService) = listeners {
on<ReactionAddEvent> {
if (guild !== null) {
val guild = guild?.asGuildOrNull() ?: return@on
val guildConfiguration = configuration[guild.id] ?: return@on
val msg = message.asMessageOrNull() ?: return@on

if (!guildConfiguration.enabled) return@on

if (this.emoji.name == configuration[guild.id]?.bookmarkReaction) {
statsService.bookmarkAdded(guild)
this.user.sendPrivateMessage {
buildSavedMessageEmbed(message.asMessage(), guild)
}.addReaction(Emojis.x)
try {
this.user.sendPrivateMessage {
buildSavedMessageEmbed(msg, guild)
}.addReaction(Emojis.x)
logger.info { "Message Bookmarked by ${msg.author?.username}" }
} catch (e: KtorRequestException) {
logger.error { "Bookmark DM could not be sent" }
}
}
} else {
if (this.emoji.name == "" && !this.user.isSelf()) {
this.message.delete()
logger.info { "Bookmark Deleted" }
}
}
}
Expand Down

0 comments on commit 32bab43

Please sign in to comment.