Skip to content

Commit

Permalink
fix: censor dict load
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Feb 20, 2023
1 parent 61c1897 commit 2ded8b9
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/main/kotlin/xyz/cssxsh/mirai/admin/data/AdminSetting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ internal object AdminSetting : ReadOnlyPluginConfig("AdminSetting"), MiraiConten

internal const val OWNER_DEFAULT = 12345L

private val list: MutableMap<String, List<String>> = HashMap()
private val dict: MutableMap<String, List<String>> = HashMap()

private val cache: MutableMap<String, Regex> = WeakHashMap()

override val censorRegex: Sequence<Regex> = sequence {
for ((_, patterns) in list) {
for ((_, patterns) in dict) {
for (pattern in patterns) {
if (pattern.isEmpty()) continue
val regex = try {
Expand All @@ -45,7 +45,7 @@ internal object AdminSetting : ReadOnlyPluginConfig("AdminSetting"), MiraiConten
plugin.logger.info("读取审核库 ${path.fileName}")

try {
list[path.name] = path.readLines()
dict[path.name] = path.readLines()
} catch (cause: IOException) {
plugin.logger.warning("读取失败 $path", cause)
}
Expand All @@ -66,24 +66,25 @@ internal object AdminSetting : ReadOnlyPluginConfig("AdminSetting"), MiraiConten
val path = event.context() as? Path ?: continue
if (!path.toString().endsWith(".txt")) continue

when (event.kind()) {
when (val kind = event.kind()) {
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY -> {
val file = folder.resolve(path)
if (!file.exists()) {
plugin.logger.info("移除审核库 $path , ${event.kind().name()}")
list.remove(file.name)
plugin.logger.info("移除审核库 $path , ${kind.name()}")
dict.remove(file.name)
continue
}
plugin.logger.info("更新审核库 $path , ${event.kind().name()}")
plugin.logger.info("更新审核库 $path , ${kind.name()}")
try {
list[file.name] = file.readLines()
dict[file.name] = file.readLines()
} catch (cause: IOException) {
plugin.logger.warning("更新审核库 $file", cause)
plugin.logger.warning("更新审核库 $file 失败 , ${kind.name()}", cause)
}
}
StandardWatchEventKinds.ENTRY_CREATE -> {
list.remove(path.name)
StandardWatchEventKinds.ENTRY_DELETE -> {
plugin.logger.info("移除审核库 $path , ${kind.name()}")
dict.remove(path.name)
}
else -> Unit
}
Expand Down

0 comments on commit 2ded8b9

Please sign in to comment.