Skip to content

Commit

Permalink
reduce logging
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Nov 2, 2023
1 parent 19a1219 commit d7dddf3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 112 deletions.
25 changes: 3 additions & 22 deletions src/main/kotlin/moe/nikky/RoleChooserConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ data class RoleMappingConfig(
val id = emoji.substringAfterLast(":").substringBefore(">")
return guildBehavior.emojis.first { it.id.toString() == id }
.let {
logger.traceF { "found emoji ${it.name}, turning into reaction emoji" }
// logger.traceF { "found emoji ${it.name}, turning into reaction emoji" }
ReactionEmoji.from(it)
}
}
Expand All @@ -85,34 +85,15 @@ data class RoleMappingConfig(

return guildEmoji
?.let {
logger.traceF { "found emoji ${it.name}, turning into reaction emoji" }
// logger.traceF { "found emoji ${it.name}, turning into reaction emoji" }
ReactionEmoji.from(it)
}
?: run {
logger.traceF { "creating unicode emoji from '$emoji'" }
// logger.traceF { "creating unicode emoji from '$emoji'" }
ReactionEmoji.Unicode(emoji)
}
}
suspend fun getRole(guildBehavior: GuildBehavior): Role {
return guildBehavior.getRole(role)
}
}


@Serializable
data class RoleChooserConfigOld(
val section: String,
@SerialComment("channel")
val channelId: Snowflake,
@SerialComment("message")
val messageId: Snowflake,
val roleMapping: List<RoleMappingConfigOld>,
) : Data


@Serializable
data class RoleMappingConfigOld(
val reaction: String,
val role: Snowflake,
val roleName: String,
) : Data
128 changes: 38 additions & 90 deletions src/main/kotlin/moe/nikky/RoleManagementExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,7 @@ class RoleManagementExtension : Extension(), Klogging {
}
try {
roleMapping.forEach { entry ->
logger.traceF { "parse emoji" }
val reactionEmoji: ReactionEmoji = entry.reactionEmoji(guild)
logger.traceF { "get role ${entry.role}" }
val role = entry.getRole(guild)
logger.traceF { "adding reaction $reactionEmoji for role ${role.name}" }
message.addReaction(reactionEmoji)
Expand Down Expand Up @@ -877,98 +875,48 @@ class RoleManagementExtension : Extension(), Klogging {
}

suspend fun migrateConfig(guild: GuildBehavior) {
try {
val oldConfig = StorageUnit(
storageType = StorageType.Config,
namespace = name,
identifier = "role-management",
dataType = RoleManagementConfigOld::class
).withGuild(guild).get() ?: return

val newData = RoleManagementConfig(
roleChoosers = oldConfig.roleChoosers.mapValues { (_, roleChooserConfig) ->
RoleChooserConfig(
section = roleChooserConfig.section,
channelId = roleChooserConfig.channelId,
messageId = roleChooserConfig.messageId,
roleMapping = roleChooserConfig.roleMapping.map { mapping ->
val emoji =
guild.emojis.firstOrNull { it.mention == mapping.reaction }
if(emoji != null) {
RoleMappingConfig(
emoji = emoji.id.toString(),
emojiName = emoji.name,
role = mapping.role,
roleName = mapping.roleName
)
} else {
RoleMappingConfig(
emoji = mapping.reaction,
emojiName = mapping.reaction,
role = mapping.role,
roleName = mapping.roleName
)
}
}
)
}
)
guild.config().save(newData)
return
} catch (e: Exception) {

}

try {
val oldConfig = StorageUnit(
storageType = StorageType.Config,
namespace = name,
identifier = "role-management",
dataType = RoleManagementConfig::class
).withGuild(guild).get() ?: return

val newData = RoleManagementConfig(
roleChoosers = oldConfig.roleChoosers.mapValues { (_, roleChooserConfig) ->
roleChooserConfig.copy(
roleMapping = roleChooserConfig.roleMapping.map { mapping ->

val emoji = if(mapping.emoji.startsWith("<") && mapping.emoji.endsWith(">")) {
val id = mapping.emoji.substringAfterLast(":").substringBefore(">")
guild.emojis.firstOrNull { it.id.toString() == id }
} else {
guild.emojis.firstOrNull { it.name == mapping.emoji || it.id.toString() == mapping.emoji }
}
if(emoji != null) {
mapping.copy(
emojiName = emoji.name,
emoji = emoji.id.toString(),
)
} else {
mapping.copy(
emojiName = mapping.emoji
)
}
}
)
}
)
guild.config().save(newData)
return
} catch (e: Exception) {

}
// try {
// val oldConfig = StorageUnit(
// storageType = StorageType.Config,
// namespace = name,
// identifier = "role-management",
// dataType = RoleManagementConfig::class
// ).withGuild(guild).get() ?: return
//
// val newData = RoleManagementConfig(
// roleChoosers = oldConfig.roleChoosers.mapValues { (_, roleChooserConfig) ->
// roleChooserConfig.copy(
// roleMapping = roleChooserConfig.roleMapping.map { mapping ->
//
// val emoji = if(mapping.emoji.startsWith("<") && mapping.emoji.endsWith(">")) {
// val id = mapping.emoji.substringAfterLast(":").substringBefore(">")
// guild.emojis.firstOrNull { it.id.toString() == id }
// } else {
// guild.emojis.firstOrNull { it.name == mapping.emoji || it.id.toString() == mapping.emoji }
// }
// if(emoji != null) {
// mapping.copy(
// emojiName = emoji.name,
// emoji = emoji.id.toString(),
// )
// } else {
// mapping.copy(
// emojiName = mapping.emoji
// )
// }
// }
// )
// }
// )
// guild.config().save(newData)
// return
// } catch (e: Exception) {
//
// }

}
}

@Serializable
data class RoleManagementConfigOld(
val roleChoosers: Map<String, RoleChooserConfigOld> = emptyMap(),
) : Data
{

}

@Serializable
data class RoleManagementConfig(
val roleChoosers: Map<String, RoleChooserConfig> = emptyMap(),
Expand Down

0 comments on commit d7dddf3

Please sign in to comment.