Skip to content

Commit

Permalink
Merge pull request #29 from fsry/main
Browse files Browse the repository at this point in the history
新增摸鱼人日历
  • Loading branch information
LinHeLurking authored Aug 11, 2023
2 parents 81f41de + aa2fa3e commit 05902c2
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,4 @@ gradle-app.setting

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml

/debug-sandbox/
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ https://github.com/LinHeLurking/mirai-news-reporter

1. 爬取知乎的一个每日新闻页面, 以图片形式分享. 向机器人说 "今日新闻", "今日速报" 即可触发.
2. 爬取 B 站的今日番剧列表, 以图片形式分享. 用 "今日动画", "今日番剧" 触发.
3. 从微信公众号摸鱼人日历的 API 获取日历图片并发送。用“摸鱼日历”,”摸鱼人日历“触发。

### 白名单

**为了避免打扰网友, 群聊使用白名单管理. 只有通过命令指定的群聊, 才会在群聊中触发本机器人.**

番剧群组白名单和新闻播报群组白名单是分开的两个名单,你可以使用 `/reporter_list` 命令来管理这两个名单
番剧群组白名单,新闻播报群组白名单和摸鱼人日历白名单是分开的三个名单,你可以使用 `/reporter_list` 命令来管理这三个名单
该命令允许 `show`, `add`, `remove` 三种后缀。其中 `add`, `remove` 两个后缀需要跟一个群号。
在群号之后你可以用 `anime`, `news` 来指定操作哪一个白名单(留空表示二者都操作)。
在群号之后你可以用 `anime`, `news``moyu` 来指定操作哪一个白名单(留空表示二者都操作)。

举例如下:

Expand Down Expand Up @@ -56,6 +57,8 @@ Bot 在回复命令时的很多语句,都可以通过 `/reporter_msg` 命令
|newsReplyMessages|"这是今天的新闻速报 \nq(≧▽≦q)"| 用关键词触发播报后的回复语 |
|noDisturbingGroupMessages|为了防止打扰到网友,这个群不在日报白名单呢QwQ| 白名单提示 |
|errorMessages|出错啦,等会再试试吧 ̄へ ̄| 错误提示 |
|touchfishDailyMessages|"摸鱼时间到~~这是今天的摸鱼日历 \n(≖‿≖)✧"))| 早上的自动摸鱼提示 |
|touchfishReplyMessages|"这是今天的摸鱼日历 \n(≖‿≖)✧"))| 用关键词触发摸鱼后的回复语 |

> 上述的 key,写成单数形式也 OK
Expand All @@ -73,4 +76,8 @@ Bot 在回复命令时的很多语句,都可以通过 `/reporter_msg` 命令

![000000000-000000000-97C34B229D32E4E897AE6F268A950E3B](https://user-images.githubusercontent.com/35602373/132117096-cff83df8-0316-4283-b3ec-197f9b2cb444.png)

摸鱼日历:

![image](https://github.com/fsry/mirai-news-reporter/assets/53202887/df67a2e6-db45-4e5a-9b46-dc2ed8cf6536)


26 changes: 12 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.5.30"
kotlin("plugin.serialization") version "1.5.20"
kotlin("jvm") version "1.8.0"
kotlin("plugin.serialization") version "1.8.0"

id("net.mamoe.mirai-console") version "2.7.0"
id("net.mamoe.mirai-console") version "2.15.0"
`maven-publish`
signing
}

group = "online.ruin_of_future"
version = "1.4.7"
version = "1.5.0"

repositories {
maven(url = "https://maven.aliyun.com/repository/public")
Expand All @@ -26,13 +24,13 @@ dependencies {
}

tasks.compileJava {
sourceCompatibility = JavaVersion.VERSION_11.toString()
targetCompatibility = JavaVersion.VERSION_11.toString()
sourceCompatibility = JavaVersion.VERSION_17.toString()
targetCompatibility = JavaVersion.VERSION_17.toString()
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
// kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
jvmTarget = "11"
}
}
//tasks.withType<KotlinCompile>().configureEach {
// kotlinOptions {
//// kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
// jvmTarget = "11"
// }
//}
17 changes: 11 additions & 6 deletions src/main/kotlin/online/ruin_of_future/reporter/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import online.ruin_of_future.reporter.chat_reply.AnimeChatReply
import online.ruin_of_future.reporter.chat_reply.NewsChatReply
import online.ruin_of_future.reporter.command.AnimeGroupCommand
import online.ruin_of_future.reporter.chat_reply.TouchFishReply

import online.ruin_of_future.reporter.command.ChatMessageConfigCommand
import online.ruin_of_future.reporter.command.NewsGroupCommand

import online.ruin_of_future.reporter.command.WhiteListCommand
import online.ruin_of_future.reporter.config.ReporterConfig
import online.ruin_of_future.reporter.data.AnimeGroupWhiteList
import online.ruin_of_future.reporter.data.NewsGroupWhiteList
import online.ruin_of_future.reporter.data.TouchFishGroupWhiteList
import online.ruin_of_future.reporter.tasks.MorningReportTask
import java.time.ZoneId
import java.util.*

object ReporterPlugin : KotlinPlugin(
JvmPluginDescription(
id = "online.ruin_of_future.reporter",
version = "1.4.6",
version = "1.5.0",
) {
name("Reporter")
author("LinHeLurking")
Expand All @@ -37,9 +39,11 @@ object ReporterPlugin : KotlinPlugin(

NewsGroupWhiteList.reload()
AnimeGroupWhiteList.reload()
TouchFishGroupWhiteList.reload()

commands.add(AnimeGroupCommand)
commands.add(NewsGroupCommand)
// commands.add(AnimeGroupCommand)
// commands.add(NewsGroupCommand)
// commands.add(TouchFishGroupCommand)
commands.add(WhiteListCommand)
commands.add(ChatMessageConfigCommand)

Expand All @@ -51,7 +55,7 @@ object ReporterPlugin : KotlinPlugin(
Date().toInstant().atZone(ZoneId.systemDefault())
.toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant()
) // midnight today
date.time += 7 * 60 * 60 * 1000
date.time += 10 * 60 * 60 * 1000 //更新时间改为10点

if (date.before(Date())) {
date.time += 24 * 60 * 60 * 1000
Expand All @@ -62,6 +66,7 @@ object ReporterPlugin : KotlinPlugin(

NewsChatReply.registerToPlugin(this)
AnimeChatReply.registerToPlugin(this)
TouchFishReply.registerToPlugin(this)
}

override fun onDisable() {
Expand Down
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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ object ChatMessageConfigCommand : CompositeCommand(
sender.sendMessage("设置 newsTriggers 为 $list")
}

@SubCommand("touchfishTriggers", "touchfishTrigger")
suspend fun setTouchfishTriggers(sender: CommandSender, args: String) {
val list = splitList(args)
ReporterConfig.touchfishTriggers.clear()
list.forEach {
ReporterConfig.touchfishTriggers.add(it)
}

sender.sendMessage("设置 touchfishTriggers 为 $list")
}

@SubCommand("separators", "separator")
suspend fun setSeparators(sender: CommandSender, args: String) {
val list = splitList(args)
Expand Down Expand Up @@ -122,6 +133,28 @@ object ChatMessageConfigCommand : CompositeCommand(
sender.sendMessage("设置 newsReplyMessages 为 $list")
}

@SubCommand("touchfishDailyMessages", "touchfishDailyMessage")
suspend fun setTouchfishDailyMessages(sender: CommandSender, args: String) {
val list = splitList(args)
ReporterConfig.touchfishDailyMessages.clear()
list.forEach {
ReporterConfig.touchfishDailyMessages.add(it)
}

sender.sendMessage("设置 touchfishDailyMessages 为 $list")
}

@SubCommand("touchfishReplyMessages", "touchfishReplyMessage")
suspend fun setTouchfishReplyMessages(sender: CommandSender, args: String) {
val list = splitList(args)
ReporterConfig.touchfishReplyMessages.clear()
list.forEach {
ReporterConfig.touchfishReplyMessages.add(it)
}

sender.sendMessage("设置 touchfishReplyMessages 为 $list")
}

@SubCommand("noDisturbingGroupMessages", "noDisturbingGroupMessage")
suspend fun setNoDisturbingGroupMessages(sender: CommandSender, args: String) {
val list = splitList(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import online.ruin_of_future.reporter.ReporterPlugin
import online.ruin_of_future.reporter.data.AnimeGroupWhiteList
import online.ruin_of_future.reporter.data.GroupWhiteList
import online.ruin_of_future.reporter.data.NewsGroupWhiteList
import online.ruin_of_future.reporter.data.TouchFishGroupWhiteList

object WhiteListCommand : CompositeCommand(
ReporterPlugin, "reporter_list", description = "统一的白名单管理"
) {
private fun getWhiteList(category: String?): List<GroupWhiteList> {
val animeRegex = Regex("anime|Anime|动画")
val newsRegex = Regex("news|News|新闻|速报")
val touchfishRegex = Regex("TouchFish|touchfish|moyu|摸鱼")
return if (category == null) {
listOf(AnimeGroupWhiteList, NewsGroupWhiteList)
listOf(AnimeGroupWhiteList, NewsGroupWhiteList, TouchFishGroupWhiteList)
} else if (category.matches(animeRegex)) {
listOf(AnimeGroupWhiteList)
} else if (category.matches(newsRegex)) {
listOf(NewsGroupWhiteList)
} else if (category.matches(touchfishRegex)){
listOf(TouchFishGroupWhiteList)
} else {
emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ object ReporterConfig : AutoSavePluginConfig("ReporterConfig") {
val dailyTriggers by value(mutableListOf("今日", "每日", "日常", "daily", "Daily"))
val animeTriggers by value(mutableListOf("番剧", "动画", "B 站番剧", "B站番剧"))
val newsTriggers by value(mutableListOf("新闻", "速报", "新闻速报"))
val touchfishTriggers by value(mutableListOf("摸鱼日历","摸鱼人日历"))
val separators by value(mutableListOf(" ", "-"))
val waitMessages by value(mutableListOf("稍等哦 QwQ"))
val animeDailyMessages by value(mutableListOf("早上好呀, 这是今天的 B 站番剧 \n( •̀ ω •́ )✧"))
val animeReplyMessages by value(mutableListOf("这是今天的 B 站番剧 \n( •̀ ω •́ )✧"))
val noAnimeMessages by value(mutableListOf("好像今天没有放送呢 >_<"))
val newsDailyMessages by value(mutableListOf("早上好呀, 这是今天的新闻速报 \nq(≧▽≦q)"))
val newsReplyMessages by value(mutableListOf("这是今天的新闻速报 \nq(≧▽≦q)"))
val touchfishDailyMessages by value(mutableListOf("摸鱼时间到~~这是今天的摸鱼日历 \n(≖‿≖)✧"))
val touchfishReplyMessages by value(mutableListOf("这是今天的摸鱼日历 \n(≖‿≖)✧"))
val noDisturbingGroupMessages by value(mutableListOf("为了防止打扰到网友,这个群不在日报白名单呢 QwQ"))
val errorMessages by value(mutableListOf("出错啦, 等会再试试吧  ̄へ ̄"))
}
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()
}
}
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"
}
Loading

0 comments on commit 05902c2

Please sign in to comment.