-
Notifications
You must be signed in to change notification settings - Fork 6
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 #29 from fsry/main
新增摸鱼人日历
- Loading branch information
Showing
12 changed files
with
222 additions
and
24 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
70 changes: 70 additions & 0 deletions
70
src/main/kotlin/online/ruin_of_future/reporter/chat_reply/TouchFishReply.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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package online.ruin_of_future.reporter.chat_reply | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin | ||
import net.mamoe.mirai.contact.Contact | ||
import net.mamoe.mirai.contact.Contact.Companion.sendImage | ||
import net.mamoe.mirai.event.globalEventChannel | ||
import net.mamoe.mirai.event.subscribeFriendMessages | ||
import net.mamoe.mirai.event.subscribeGroupMessages | ||
import online.ruin_of_future.reporter.ReporterPlugin | ||
import online.ruin_of_future.reporter.config.ReporterConfig | ||
import online.ruin_of_future.reporter.crawler.TouchFishCrawler | ||
import online.ruin_of_future.reporter.data.TouchFishGroupWhiteList | ||
import online.ruin_of_future.reporter.regexOrBuilder | ||
import java.io.ByteArrayInputStream | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.coroutineContext | ||
|
||
object TouchFishReply { | ||
private val touchfishCrawler = TouchFishCrawler.INSTANCE | ||
private suspend fun sendTouchFishToTarget(contact: Contact, context: CoroutineContext = Dispatchers.Default) { | ||
if (!touchfishCrawler.isCacheValid()) { | ||
contact.sendMessage(ReporterConfig.waitMessages.random()) | ||
} | ||
val stream = withContext(context) { | ||
try { | ||
ByteArrayInputStream(touchfishCrawler.touchfishToday()) | ||
} catch (e: Throwable) { | ||
contact.sendMessage(ReporterConfig.errorMessages.random()) | ||
ReporterPlugin.logger.error(e) | ||
return@withContext null | ||
} | ||
} | ||
if (stream != null) { | ||
contact.sendMessage(ReporterConfig.touchfishReplyMessages.random()) | ||
contact.sendImage(stream) | ||
} | ||
} | ||
|
||
private fun buildTrigger(): Regex { | ||
val dailyTrigger = regexOrBuilder(ReporterConfig.dailyTriggers) | ||
val separatorTrigger = regexOrBuilder(ReporterConfig.separators) | ||
val touchfishTrigger = regexOrBuilder(ReporterConfig.touchfishTriggers) | ||
return Regex("($separatorTrigger?)($touchfishTrigger)") | ||
} | ||
|
||
private val trigger = buildTrigger() | ||
|
||
fun registerToPlugin(plugin: KotlinPlugin) { | ||
plugin.globalEventChannel().subscribeGroupMessages { | ||
matching(trigger) { | ||
plugin.logger.info("$senderName 发起了摸鱼日历请求...") | ||
if (TouchFishGroupWhiteList.groupIdsPerBot[bot.id]?.contains(group.id) == true) { | ||
sendTouchFishToTarget(group, coroutineContext) | ||
} else { | ||
sender.sendMessage(ReporterConfig.noDisturbingGroupMessages.random()) | ||
sendTouchFishToTarget(sender, coroutineContext) | ||
} | ||
} | ||
} | ||
|
||
plugin.globalEventChannel().subscribeFriendMessages { | ||
matching(trigger) { | ||
plugin.logger.info("$senderName 发起了摸鱼日历请求...") | ||
sendTouchFishToTarget(sender, coroutineContext) | ||
} | ||
} | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/online/ruin_of_future/reporter/crawler/TouchFishCrawler.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package online.ruin_of_future.reporter.crawler | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.io.ByteArrayOutputStream | ||
import java.io.IOException | ||
import java.net.URL | ||
import javax.imageio.ImageIO | ||
|
||
|
||
class TouchFishCrawler(private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO) { | ||
private val byteArrayCache = Cached(byteArrayOf(), 1000 * 60 * 60 * 4L) | ||
|
||
|
||
fun isCacheValid(): Boolean { | ||
return byteArrayCache.isNotOutdated() | ||
} | ||
@Throws(IOException::class) | ||
suspend fun touchfishToday(): ByteArray { | ||
if (byteArrayCache.isNotOutdated()) { | ||
return byteArrayCache.value | ||
} | ||
|
||
val os = ByteArrayOutputStream() | ||
withContext(ioDispatcher) { | ||
var url = URL("https://api.vvhan.com/api/moyu") | ||
var bufferedImage = ImageIO.read(url.openConnection().getInputStream()) | ||
ImageIO.write(bufferedImage, "png", os) | ||
} | ||
byteArrayCache.value = os.toByteArray() | ||
return byteArrayCache.value | ||
} | ||
|
||
companion object { | ||
val INSTANCE = TouchFishCrawler() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/online/ruin_of_future/reporter/data/TouchFishGroupWhiteList.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package online.ruin_of_future.reporter.data | ||
|
||
import net.mamoe.mirai.console.data.AutoSavePluginConfig | ||
import net.mamoe.mirai.console.data.value | ||
|
||
object TouchFishGroupWhiteList : AutoSavePluginConfig("newsGroupWhiteList"), GroupWhiteList { | ||
override val groupIdsPerBot: MutableMap<Long, MutableList<Long>> by value() | ||
override val tag: String | ||
get() = "TouchFish" | ||
} |
Oops, something went wrong.