-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from the-programmers-hangout/feat/miscellaneou…
…s-refactoring chore: miscellaneous refactoring to cleanup code
- Loading branch information
Showing
6 changed files
with
48 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 32 additions & 28 deletions
60
src/main/kotlin/me/ddivad/hawk/services/StartupService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,53 @@ | ||
package me.ddivad.hawk.services | ||
|
||
import dev.kord.core.behavior.getChannelOfOrNull | ||
import dev.kord.core.entity.Guild | ||
import dev.kord.core.entity.channel.GuildMessageChannel | ||
import dev.kord.core.entity.channel.TextChannel | ||
import me.ddivad.hawk.dataclasses.Configuration | ||
import me.ddivad.hawk.dataclasses.ReactionRole | ||
import me.ddivad.hawk.embeds.createReactionRoleMenu | ||
import me.jakejmattson.discordkt.Discord | ||
import me.jakejmattson.discordkt.annotations.Service | ||
import me.jakejmattson.discordkt.extensions.createMenu | ||
import mu.KotlinLogging | ||
|
||
val logger = KotlinLogging.logger { } | ||
|
||
@Service | ||
class StartupService( | ||
private val configuration: Configuration, | ||
private val discord: Discord, | ||
private val loggingService: LoggingService | ||
) { | ||
suspend fun refreshReactionRoleInteractions() { | ||
configuration.guildConfigurations.forEach { config -> | ||
val guild = config.value.let { discord.kord.getGuild(config.key) } ?: return@forEach | ||
val guildConfig = configuration[guild.id] ?: return@forEach | ||
configuration.guildConfigurations.forEach { (guildId, guildConfig) -> | ||
val guild = discord.kord.getGuild(guildId) ?: return@forEach | ||
if (guildConfig.reactionRoles.isEmpty()) return@forEach | ||
logger.info { | ||
buildGuildLogMessage( | ||
guild, | ||
"Refreshing ${guildConfig.reactionRoles.size} reaction roles for ${guild.name}" | ||
) | ||
} | ||
|
||
guildConfig.reactionRoles.forEach { | ||
val channel = guild.getChannelOfOrNull<GuildMessageChannel>(it.channel) ?: return | ||
val message = channel.getMessageOrNull(it.messageId!!) ?: return | ||
logger.info { | ||
buildGuildLogMessage( | ||
guild, | ||
"Refreshing reaction role ${it.id} - ${it.messageId} - ${(it.roles).joinToString()}" | ||
) | ||
} | ||
message.delete() | ||
it.messageId = channel.createMenu { | ||
createReactionRoleMenu(discord, guild, it) | ||
}.id | ||
} | ||
loggingService.guildLog( | ||
guild, | ||
"Refreshing ${guildConfig.reactionRoles.size} reaction roles for ${guild.name}" | ||
) | ||
val reactionRolesToDelete = guildConfig.reactionRoles.filterNot { it.refresh(guild) }.toMutableList() | ||
reactionRolesToDelete.forEach { guildConfig.reactionRoles.remove(it) } | ||
configuration.save() | ||
} | ||
} | ||
|
||
private suspend fun ReactionRole.refresh(guild: Guild): Boolean { | ||
loggingService.guildLog(guild, "Attempting to refresh reaction role $id - $messageId - ${roles.joinToString()}") | ||
val channel = guild.getChannelOfOrNull<GuildMessageChannel>(channel) | ||
val message = messageId?.let { channel?.getMessageOrNull(it) } | ||
val reactionRole = this | ||
if (channel != null && message != null) { | ||
message.delete() | ||
val newMessage = channel.createMenu { createReactionRoleMenu(discord, guild, reactionRole) } | ||
newMessage.pin() | ||
messageId = newMessage.id | ||
loggingService.guildLog(guild, "Reaction role $id refreshed. New message ID: ${newMessage.id}") | ||
return true | ||
} else { | ||
loggingService.guildLog( | ||
guild, | ||
"Channel or message for reaction role $id no longer exist, flagging for deletion" | ||
) | ||
return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters