Skip to content

Commit

Permalink
增加图片页脚
Browse files Browse the repository at this point in the history
  • Loading branch information
Colter23 committed Feb 26, 2022
1 parent 8e80bf6 commit e3cb9f6
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 109 deletions.
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ font: HarmonyOS Sans SC,Source Han Sans
# {type} : 类型 如 动态、直播等
# {time} : 时间
# {link} : 链接
# {id} : ID
# \n : 换行
# 如有换行请使用双引号
# 2.0.4 版本之前的模板为 {name}@{uid}@{type}\n{time}\n{link}
Expand All @@ -109,6 +110,9 @@ pushTemplate: "{name}@{type}\n{link}"
# 图片模式下直播推送文字模板, 如果为空则与上方动态推送模板保持一致
livePushTemplate: ""

# 图标页脚模板 配置项与上方一致
footerTemplate: "{type}ID: {id}"

# cookie 请包在双引号内
# 如何获取见下文 手动获取 Cookie
# 或使用 /bili login 进行扫码登录
Expand Down Expand Up @@ -163,22 +167,25 @@ baiduTranslate:
| 权限名 | ID |
|-------|-------------------------------------------------------|
| 直播@全体 | top.colter.bilibili-dynamic-mirai-plugin:live.atall |
| 视频@全体 | top.colter.bilibili-dynamic-mirai-plugin:video.atall |

通过对群设置权限来控制是否开启直播@全体
`/perm add g123456789 top.colter.bilibili-dynamic-mirai-plugin:live.atall`

### 指令

| 指令 | 描述 |
|---------------------------------------|--------------------|
| /bili <login 登录> | bot管理员进行扫码登录 |
| /bili <add 添加> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 添加一个订阅 |
| /bili <list 列表> [群/Q号] | 查询目标 [群/Q号] 的订阅列表 |
| /bili <del 删除> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 删除一个订阅 |
| /bili <delAll 删除全部订阅> [群/Q号] | 将目标 [群/Q号] 的全部订阅删除 |
| /bili <set 设置> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 设置订阅内容 |
| /bili <color 颜色> &lt;uid&gt; <16进制颜色> | 为目标 UID 设置图片推送主题色 |
| /bili <list 列表> [群/Q号] | 查询目标 [群/Q号] 的订阅列表 |
| 指令 | 描述 |
|-----------------------------------------|--------------------|
| /bili <login / 登录> | bot管理员进行扫码登录 |
| /bili <add / 添加> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 添加一个订阅 |
| /bili <list / 列表> [群/Q号] | 查询目标 [群/Q号] 的订阅列表 |
| /bili <del / 删除> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 删除一个订阅 |
| /bili <delAll / 删除全部订阅> [群/Q号] | 将目标 [群/Q号] 的全部订阅删除 |
| /bili <set / 设置> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 设置订阅内容 |
| /bili <color / 颜色> &lt;uid&gt; <16进制颜色> | 为目标 UID 设置图片推送主题色 |
| /bili <list / 列表> [群/Q号] | 查询目标 [群/Q号] 的订阅列表 |
| /bili <listAll / la / 全部订阅列表> | bot管理员查询全部订阅列表 |
| /bili <listUser / lu / 用户列表> | bot管理员查询用户列表 |
```
# 说明
扫码登录请在配置文件中填写管理员账号
Expand All @@ -197,12 +204,12 @@ baiduTranslate:
包含:仅当动态文字内容匹配过滤表达式时**推送**动态
过滤表达式:支持正则表达式,普通用户用文字即可,^匹配开头 $匹配结尾
| 指令 | 描述 |
|------------------------------------------------------------|-------------------------|
| /bili <filter f 过滤> <过滤表达式> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 添加 uid 的动态过滤 |
| /bili <contain c 包含> <过滤表达式> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 添加 uid 的动态包含 |
| /bili <filterList fl 过滤列表> &lt;uid&gt; [群/Q号] | 列出 uid 的过滤包含列表 |
| /bili <filterDel fd 过滤删除> &lt;index&gt; &lt;uid&gt; [群/Q号] | 删除 uid 中 index 的过滤包含 |
| 指令 | 描述 |
|-----------------------------------------------------------|-------------------------|
| /bili <filter / f / 过滤> <过滤表达式> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 添加 uid 的动态过滤 |
| /bili <contain / c / 包含> <过滤表达式> &lt;uid&gt; [群/Q号] | 为目标 [群/Q号] 添加 uid 的动态包含 |
| /bili <filterList / fl / 过滤列表> &lt;uid&gt; [群/Q号] | 列出 uid 的过滤包含列表 |
| /bili <filterDel / fd / 过滤删除> &lt;index&gt; &lt;uid&gt; [群/Q号] | 删除 uid 中 index 的过滤包含 |
```
# 栗子
Expand Down
109 changes: 80 additions & 29 deletions src/main/kotlin/top/colter/mirai/plugin/bilibili/BiliBiliTasker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.permission.PermissionService.Companion.getPermittedPermissions
import net.mamoe.mirai.console.permission.PermitteeId.Companion.permitteeId
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.message.data.AtAll
import net.mamoe.mirai.message.data.Message
Expand All @@ -29,14 +30,12 @@ import kotlin.coroutines.CoroutineContext

internal val logger by PluginMain::logger

object DynamicTasker : CoroutineScope {
object DynamicTasker: CoroutineScope {

override val coroutineContext: CoroutineContext = Dispatchers.IO + CoroutineName("DynamicTasker")

private var listener: MutableList<Job>? = null

private var listener: MutableMap<String,Job>? = null
private val seleniumMutex = Mutex()

private val httpUtils = HttpUtils()

val mutex = Mutex()
Expand All @@ -58,20 +57,23 @@ object DynamicTasker : CoroutineScope {
private var errorMessage = "SUCCESS"
private var liveErrorMessage = "SUCCESS"

private var adminContact: Contact? = null

fun start() {
runCatching {
lsl = lowSpeed.split("-","x").map { it.toInt() }
isLowSpeed = lsl[0] != lsl[1]
}.onFailure {
logger.error("低频检测参数错误 ${it.message}")
}
adminContact = findContact(BiliPluginConfig.admin)?:findContact("-${BiliPluginConfig.admin}")

listener = listen()
}

fun stop() {
listener?.forEach {
it.cancel()
it.value.cancel()
}
}

Expand All @@ -91,7 +93,7 @@ object DynamicTasker : CoroutineScope {

private fun followUser(uid: Long): String {
if (uid == PluginMain.mid) {
return "不能关注自己哦"
return ""
}
val attr = httpUtils.getAndDecode<IsFollow>(IS_FOLLOW(uid)).attribute
if (attr == 0) {
Expand Down Expand Up @@ -140,7 +142,7 @@ object DynamicTasker : CoroutineScope {
val subData = SubData(httpUtils.getAndDecode<User>(USER_INFO(uid)).name)
subData.contacts[subject] = "11"
dynamic[uid] = subData
"订阅 ${dynamic[uid]?.name} 成功! \n默认检测 动态+视频+直播 如果需要调整请发送/bili set $uid\n如要设置主题色请发送/bili color <16进制颜色>"
"订阅 ${dynamic[uid]?.name} 成功! \n默认检测 动态+视频+直播 如果需要调整请发送/bili set $uid\n如要设置主题色请发送/bili color $uid <16进制颜色>"
} else {
if (user.contacts.contains(subject)) {
"之前订阅过这个人哦"
Expand Down Expand Up @@ -247,6 +249,45 @@ object DynamicTasker : CoroutineScope {
}
}

suspend fun listAll( ) = mutex.withLock {
var count = 0
buildString {
appendLine("名称@UID#订阅人数")
appendLine()
dynamic.forEach { (uid, sub) ->
appendLine("${sub.name}@$uid#${sub.contacts.keys.size}")
count++
}
appendLine()
append("$count 个订阅")
}
}

suspend fun listUser() = mutex.withLock {
buildString {
val user = mutableSetOf<String>()
dynamic.forEach { (uid, sub) ->
user.addAll(sub.contacts.keys)
}
val group = StringBuilder()
val friend = StringBuilder()
user.forEach {
findContact(it).apply {
when (this){
is Group -> group.appendLine("${name}@${id}")
is Friend -> friend.appendLine("${nick}@${id}")
}
}
}
appendLine("====群====")
append(group.ifEmpty { "\n" })
appendLine("====好友====")
append(friend.ifEmpty { "\n" })
appendLine()
append("${user.size} 名用户")
}
}

suspend fun login(contact: Contact){
val loginData = httpUtils.getAndDecode<LoginData>(LOGIN_URL)
val qrCodeWriter = QRCodeWriter()
Expand Down Expand Up @@ -284,12 +325,29 @@ object DynamicTasker : CoroutineScope {
file.delete()
}

private fun listen(): MutableMap<String, Job> {
val jobMap = mutableMapOf<String, Job>()
jobMap["动态"] = normalJob()
jobMap["直播"] = liveJob()
jobMap["检测"] = checkJobActive()
return jobMap
}

private fun listen(): MutableList<Job> {
val jobList = mutableListOf<Job>()
jobList.add(normalJob())
jobList.add(liveJob())
return jobList
private fun checkJobActive() = launch {
while (isActive){
logger.debug("Check")
listener?.keys?.forEach {
var job = listener!![it]!!
if (!job.isActive){
adminContact?.sendMessage("$it 任务已停止工作,正在尝试重启任务")
logger.error("$it 任务已停止工作")
job.cancel()
if (it=="动态") listener!![it] = normalJob()
else if (it=="直播") listener!![it] = liveJob()
}
}
delay(60000)
}
}

private fun getHistoryDynamic() = runCatching {
Expand All @@ -309,7 +367,7 @@ object DynamicTasker : CoroutineScope {
delay(10000L)
while (isActive) {
if (PluginMain.sessData == ""){
logger.debug("未登录")
logger.error("未登录")
delay(100000)
continue
}
Expand All @@ -333,10 +391,10 @@ object DynamicTasker : CoroutineScope {
di.dynamicContent = di.card.dynamicContent(di.type)
list = list.filterContent(describe.uid, di.dynamicContent!!.textContent(describe.type))
if (list.size > 0){
val color = dynamic[describe.uid]?.color ?: "#d3edfa"
seleniumMutex.withLock {
val color = dynamic[describe.uid]?.color ?: "#d3edfa"
history.add(describe.dynamicId)
withTimeout(30000) {
withTimeout(45000) {
list.sendMessage(di.type) { di.build(it, color) }
}
}
Expand All @@ -348,12 +406,8 @@ object DynamicTasker : CoroutineScope {
delay((intervalTime..(intervalTime + 5000L)).random())
}.onFailure {
logger.error("ERROR $it")
if (it.message != errorMessage){
(findContact(BiliPluginConfig.admin)?:findContact("-${BiliPluginConfig.admin}"))?.sendMessage("动态检测失败\n" + it.message)
}
if (errorMessage == "SUCCESS"){
errorMessage = it.message.toString()
}
if (it.message != errorMessage) adminContact?.sendMessage("动态检测失败\n" + it.message)
if (errorMessage == "SUCCESS") errorMessage = it.message.toString()
delay((60000L..120000L).random())
}
}
Expand All @@ -380,8 +434,9 @@ object DynamicTasker : CoroutineScope {
val list = getLiveContactList(ll.uid)
if (list != null && list.size != 0) {
seleniumMutex.withLock {
withTimeout(30000) {
list.sendMessage(DynamicType.LIVE){ ll.build(it) }
val color = dynamic[ll.uid]?.color ?: "#d3edfa"
withTimeout(20000) {
list.sendMessage(DynamicType.LIVE){ ll.build(it, color) }
}
}
lastLive = ll.liveTime
Expand All @@ -392,12 +447,8 @@ object DynamicTasker : CoroutineScope {
delay((liveIntervalTime..(liveIntervalTime + 5000L)).random())
}.onFailure {
logger.error("ERROR $it")
if (it.message != liveErrorMessage){
(findContact(BiliPluginConfig.admin)?:findContact("-${BiliPluginConfig.admin}"))?.sendMessage("直播检测失败\n" + it.message)
}
if (liveErrorMessage == "SUCCESS"){
liveErrorMessage = it.message.toString()
}
if (it.message != liveErrorMessage) adminContact?.sendMessage("直播检测失败\n" + it.message)
if (liveErrorMessage == "SUCCESS") liveErrorMessage = it.message.toString()
delay((60000L..120000L).random())
}
}
Expand Down
Loading

0 comments on commit e3cb9f6

Please sign in to comment.