From b53c60b58f6e716354154d2ebc1fefd43693aec4 Mon Sep 17 00:00:00 2001 From: NoComment1105 Date: Sun, 19 Jun 2022 11:17:21 +0100 Subject: [PATCH] Re fix a fix that I accidentally unfixed immediately after fixing it --- .../extensions/events/MemberJoinLeave.kt | 102 +++++++++--------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/main/kotlin/net/irisshaders/lilybot/extensions/events/MemberJoinLeave.kt b/src/main/kotlin/net/irisshaders/lilybot/extensions/events/MemberJoinLeave.kt index 3708231b..91a2c220 100644 --- a/src/main/kotlin/net/irisshaders/lilybot/extensions/events/MemberJoinLeave.kt +++ b/src/main/kotlin/net/irisshaders/lilybot/extensions/events/MemberJoinLeave.kt @@ -4,15 +4,15 @@ import com.kotlindiscord.kord.extensions.DISCORD_GREEN import com.kotlindiscord.kord.extensions.DISCORD_RED import com.kotlindiscord.kord.extensions.extensions.Extension import com.kotlindiscord.kord.extensions.extensions.event -import dev.kord.common.entity.Permission -import dev.kord.common.entity.Permissions import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.behavior.getChannelOf -import dev.kord.core.entity.channel.TextChannel +import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.event.guild.MemberJoinEvent import dev.kord.core.event.guild.MemberLeaveEvent +import dev.kord.rest.request.KtorRequestException import kotlinx.coroutines.flow.count import kotlinx.datetime.Clock +import mu.KotlinLogging import net.irisshaders.lilybot.utils.DatabaseHelper import net.irisshaders.lilybot.utils.configPresent @@ -24,6 +24,8 @@ import net.irisshaders.lilybot.utils.configPresent class MemberJoinLeave : Extension() { override val name = "member-join-leave" + private val joinLeaveLogger = KotlinLogging.logger("Join Leave logger") + override suspend fun setup() { /** Create an embed in the join channel on user join */ event { @@ -37,32 +39,33 @@ class MemberJoinLeave : Extension() { val eventMember = event.member val guildMemberCount = event.getGuild().members.count() - val joinChannel = event.getGuild().getChannelOf(config.joinChannel) - - if (!joinChannel.getEffectivePermissions(kord.selfId).contains( - Permissions(Permission.SendMessages, Permission.EmbedLinks) - ) - ) { - return@action - } + val joinChannel = event.getGuild().getChannelOf(config.joinChannel) - joinChannel.createEmbed { - title = "User joined the server!" - field { - name = "Welcome:" - value = "${eventMember.mention} (${eventMember.tag})" - inline = true - } - field { - name = "ID:" - value = eventMember.id.toString() - inline = false + try { + joinChannel.createEmbed { + title = "User joined the server!" + field { + name = "Welcome:" + value = "${eventMember.mention} (${eventMember.tag})" + inline = true + } + field { + name = "ID:" + value = eventMember.id.toString() + inline = false + } + footer { + text = "Member Count: $guildMemberCount" + } + timestamp = Clock.System.now() + color = DISCORD_GREEN } - footer { - text = "Member Count: $guildMemberCount" + } catch (e: KtorRequestException) { + if (e.httpResponse.status.value == 400) { + return@action + } else { + joinLeaveLogger.warn("Join embed failed to send. This was not due to a permission error!") } - timestamp = Clock.System.now() - color = DISCORD_GREEN } } } @@ -78,32 +81,33 @@ class MemberJoinLeave : Extension() { val eventUser = event.user val guildMemberCount = event.getGuild().members.count() - val joinChannel = event.getGuild().getChannelOf(config.joinChannel) + val joinChannel = event.getGuild().getChannelOf(config.joinChannel) - if (!joinChannel.getEffectivePermissions(kord.selfId).contains( - Permissions(Permission.EmbedLinks, Permission.SendMessages) - ) - ) { - return@action - } - - joinChannel.createEmbed { - title = "User left the server!" - field { - name = "Goodbye:" - value = eventUser.tag - inline = true - } - field { - name = "ID:" - value = eventUser.id.toString() - inline = false + try { + joinChannel.createEmbed { + title = "User left the server!" + field { + name = "Goodbye:" + value = eventUser.tag + inline = true + } + field { + name = "ID:" + value = eventUser.id.toString() + inline = false + } + footer { + text = "Member count: $guildMemberCount" + } + timestamp = Clock.System.now() + color = DISCORD_RED } - footer { - text = "Member count: $guildMemberCount" + } catch (e: KtorRequestException) { + if (e.httpResponse.status.value == 400) { + return@action + } else { + joinLeaveLogger.warn("Leave embed failed to send. This was not due to a permission error!") } - timestamp = Clock.System.now() - color = DISCORD_RED } } }