Skip to content

Commit

Permalink
Event: support mute anonymous member, add invitor Id
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Jan 14, 2021
1 parent fe6e1fb commit a1eef4c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/main/kotlin/org/itxtech/mirainative/Bridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ object Bridge {
fun getRecord(pluginId: Int, file: ByteArray, format: ByteArray) =
MiraiBridge.getRecord(pluginId, file.fromNative(), format.fromNative()).toNative()

@JvmStatic
fun setGroupAnonymousBan(pluginId: Int, group: Long, id: ByteArray, duration: Long) =
MiraiBridge.setGroupAnonymousBan(pluginId, group, id.fromNative(), duration)

// Placeholder methods which mirai hasn't supported yet

@JvmStatic
Expand All @@ -263,9 +267,6 @@ object Bridge {
fun setGroupAdmin(pluginId: Int, group: Long, account: Long, admin: Boolean) = 0
//true => set, false => revoke

@JvmStatic
fun setGroupAnonymousBan(pluginId: Int, group: Long, id: ByteArray, duration: Long) = 0

// Wont' Implement

@JvmStatic
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/org/itxtech/mirainative/bridge/MiraiBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.MessageSourceKind
import net.mamoe.mirai.message.data.kind
import net.mamoe.mirai.utils.MiraiExperimentalApi
import org.itxtech.mirainative.Bridge
import org.itxtech.mirainative.MiraiNative
import org.itxtech.mirainative.fromNative
Expand All @@ -62,7 +63,7 @@ import java.security.MessageDigest
import kotlin.io.use
import kotlin.text.toByteArray

@OptIn(InternalAPI::class)
@OptIn(InternalAPI::class, MiraiExperimentalApi::class)
object MiraiBridge {
private fun logError(id: Int, e: String, err: Exception? = null) {
val plugin = PluginManager.plugins[id]
Expand Down Expand Up @@ -363,6 +364,14 @@ object MiraiBridge {
}
}

fun setGroupAnonymousBan(pluginId: Int, group: Long, id: String, duration: Long) =
call("CQ_setGroupAnonymousBan", pluginId, 0) {
runBlocking {
CacheManager.findAnonymousMember(group, id)?.mute(duration.toInt())
}
return 0
}

fun addLog(pluginId: Int, priority: Int, type: String, content: String) {
NativeLoggerHelper.log(PluginManager.plugins[pluginId]!!, priority, type, content)
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/kotlin/org/itxtech/mirainative/manager/CacheManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ package org.itxtech.mirainative.manager

import kotlinx.atomicfu.atomic
import kotlinx.coroutines.launch
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.AnonymousMember
import net.mamoe.mirai.contact.NormalMember
import net.mamoe.mirai.contact.User
import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.event.events.GroupTempMessageEvent
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource
Expand All @@ -42,7 +44,8 @@ import org.itxtech.mirainative.MiraiNative
object CacheManager {
private val msgCache = hashMapOf<Int, MessageSource>()
private val evCache = hashMapOf<Int, BotEvent>()
private val senders = hashMapOf<Long, Member>()
private val senders = hashMapOf<Long, NormalMember>()
private val anonymousMembers = hashMapOf<Long, HashMap<String, AnonymousMember>>()
private val records = hashMapOf<String, Voice>()
private val internalId = atomic(0)

Expand All @@ -67,6 +70,14 @@ object CacheManager {
return cacheMessage(message.message.source, id, message.message)
}

fun cacheAnonymousMember(ev: GroupMessageEvent) {
if (anonymousMembers[ev.group.id] == null) {
anonymousMembers[ev.group.id] = hashMapOf()
}
val sender = ev.sender as AnonymousMember
anonymousMembers[ev.group.id]!![sender.anonymousId] = sender
}

fun recall(id: Int): Boolean {
val message = msgCache[id] ?: return false
msgCache.remove(id)
Expand All @@ -92,9 +103,12 @@ object CacheManager {
return member
}

fun findAnonymousMember(group: Long, id: String): AnonymousMember? = anonymousMembers[group]?.get(id)

fun clear() {
msgCache.clear()
evCache.clear()
senders.clear()
anonymousMembers.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.itxtech.mirainative.manager

import net.mamoe.mirai.contact.AnonymousMember
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.event.globalEventChannel
Expand Down Expand Up @@ -58,13 +59,16 @@ object EventManager {
}
}
subscribeAlways<GroupMessageEvent> {
if (sender is AnonymousMember) {
CacheManager.cacheAnonymousMember(this)
}
launchEvent {
NativeBridge.eventGroupMessage(
1,
CacheManager.cacheMessage(message.source, chain = message),
group.id,
sender.id,
"",
if (sender is AnonymousMember) (sender as AnonymousMember).anonymousId else "",//可能不兼容某些插件
ChainCodeConverter.chainToCode(message),
0
)
Expand Down Expand Up @@ -105,7 +109,7 @@ object EventManager {
launchEvent {
NativeBridge.eventGroupMemberJoin(
if (ev is MemberJoinEvent.Invite) Bridge.MEMBER_JOIN_PERMITTED else Bridge.MEMBER_JOIN_INVITED_BY_ADMIN,
getTimestamp(), group.id, 0, member.id
getTimestamp(), group.id, if (ev is MemberJoinEvent.Invite) ev.invitor.id else 0, member.id
)
}
}
Expand Down

0 comments on commit a1eef4c

Please sign in to comment.