Reduce code redundancy

This commit is contained in:
Him188 2020-03-24 15:28:24 +08:00
parent ed46780983
commit a9972dc030
7 changed files with 57 additions and 83 deletions

View File

@ -198,7 +198,7 @@ internal class MessageSvc {
if (group.members.contains(msg.msgHead.authUin)) {
return@mapNotNull null
}
return@mapNotNull MemberJoinEvent(group.Member(object : MemberInfo {
return@mapNotNull MemberJoinEvent(group.newMember(object : MemberInfo {
override val nameCard: String get() = ""
override val permission: MemberPermission get() = MemberPermission.MEMBER
override val specialTitle: String get() = ""

View File

@ -32,7 +32,6 @@ import net.mamoe.mirai.utils.WeakRefProperty
*
* @author Him188moe
*/
@Suppress("INAPPLICABLE_JVM_NAME")
@OptIn(MiraiInternalAPI::class, JavaHappyAPI::class)
actual abstract class Contact : CoroutineScope, ContactJavaHappyAPI() {
/**

View File

@ -25,7 +25,6 @@ import net.mamoe.mirai.utils.OverFileSizeMaxException
/**
* . QQ Android 中叫做 "Troop"
*/
@Suppress("INAPPLICABLE_JVM_NAME")
actual abstract class Group : Contact(), CoroutineScope {
/**
* 群名称.
@ -101,7 +100,7 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract val owner: Member
/**
* [Bot] 在群内的 [Member] 实例
* [Bot] 在群内的 [newMember] 实例
*/
@MiraiExperimentalAPI
actual abstract val botAsMember: Member
@ -158,13 +157,12 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract suspend fun quit(): Boolean
/**
* 构造一个 [Member].
* 构造一个 [newMember].
* 非特殊情况请不要使用这个函数. 优先使用 [get].
*/
@JvmName("newMember")
@Suppress("INAPPLICABLE_JVM_NAME", "FunctionName")
@Suppress("FunctionName")
@MiraiExperimentalAPI("dangerous")
actual abstract fun Member(memberInfo: MemberInfo): Member
actual abstract fun newMember(memberInfo: MemberInfo): Member
/**
* 向这个对象发送消息.
@ -193,40 +191,11 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract override suspend fun uploadImage(image: ExternalImage): OfflineGroupImage
actual companion object {
/**
* by @kar98k
*/
actual fun calculateGroupUinByGroupCode(groupCode: Long): Long {
var left: Long = groupCode / 1000000L
actual fun calculateGroupUinByGroupCode(groupCode: Long): Long =
CommonGroupCalculations.calculateGroupUinByGroupCode(groupCode)
when (left) {
in 0..10 -> left += 202
in 11..19 -> left += 480 - 11
in 20..66 -> left += 2100 - 20
in 67..156 -> left += 2010 - 67
in 157..209 -> left += 2147 - 157
in 210..309 -> left += 4100 - 210
in 310..499 -> left += 3800 - 310
}
return left * 1000000L + groupCode % 1000000L
}
actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long {
var left: Long = groupUin / 1000000L
when (left) {
in 0 + 202..10 + 202 -> left -= 202
in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11
in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20
in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67
in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157
in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210
in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310
}
return left * 1000000L + groupUin % 1000000L
}
actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long =
CommonGroupCalculations.calculateGroupCodeByGroupUin(groupUin)
}
@MiraiExperimentalAPI

View File

@ -36,7 +36,6 @@ import kotlin.jvm.JvmSynthetic
* @author Him188moe
*/ // 不要删除多平台结构 !!! kotlin bug
@OptIn(MiraiInternalAPI::class, JavaHappyAPI::class)
@Suppress("INAPPLICABLE_JVM_NAME")
expect abstract class Contact() : CoroutineScope, ContactJavaHappyAPI {
/**
* 这个联系人所属 [Bot].

View File

@ -99,7 +99,7 @@ expect abstract class Group() : Contact, CoroutineScope {
abstract val owner: Member
/**
* [Bot] 在群内的 [Member] 实例
* [Bot] 在群内的 [newMember] 实例
*/
@MiraiExperimentalAPI
abstract val botAsMember: Member
@ -158,13 +158,13 @@ expect abstract class Group() : Contact, CoroutineScope {
abstract suspend fun quit(): Boolean
/**
* 构造一个 [Member].
* 构造一个 [newMember].
* 非特殊情况请不要使用这个函数. 优先使用 [get].
*/
@MiraiExperimentalAPI("dangerous")
@Suppress("INAPPLICABLE_JVM_NAME", "FunctionName")
@JvmName("newMember")
abstract fun Member(memberInfo: MemberInfo): Member
abstract fun newMember(memberInfo: MemberInfo): Member
/**
* 向这个对象发送消息.
@ -203,9 +203,46 @@ expect abstract class Group() : Contact, CoroutineScope {
fun toFullString(): String
}
internal object CommonGroupCalculations {
/**
* by @kar98k
*/
fun calculateGroupUinByGroupCode(groupCode: Long): Long {
var left: Long = groupCode / 1000000L
when (left) {
in 0..10 -> left += 202
in 11..19 -> left += 480 - 11
in 20..66 -> left += 2100 - 20
in 67..156 -> left += 2010 - 67
in 157..209 -> left += 2147 - 157
in 210..309 -> left += 4100 - 210
in 310..499 -> left += 3800 - 310
}
return left * 1000000L + groupCode % 1000000L
}
fun calculateGroupCodeByGroupUin(groupUin: Long): Long {
var left: Long = groupUin / 1000000L
when (left) {
in 0 + 202..10 + 202 -> left -= 202
in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11
in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20
in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67
in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157
in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210
in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310
}
return left * 1000000L + groupUin % 1000000L
}
}
/**
* 返回机器人是否正在被禁言
*
* @see Group.botMuteRemaining 剩余禁言时间
*/
val Group.isBotMuted: Boolean get() = this.botMuteRemaining != 0
inline val Group.isBotMuted: Boolean get() = this.botMuteRemaining != 0

View File

@ -31,7 +31,6 @@ import net.mamoe.mirai.utils.WeakRefProperty
*
* @author Him188moe
*/
@Suppress("INAPPLICABLE_JVM_NAME")
@OptIn(MiraiInternalAPI::class, JavaHappyAPI::class)
actual abstract class Contact : CoroutineScope, ContactJavaHappyAPI() {
/**

View File

@ -101,7 +101,7 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract val owner: Member
/**
* [Bot] 在群内的 [Member] 实例
* [Bot] 在群内的 [newMember] 实例
*/
@MiraiExperimentalAPI
actual abstract val botAsMember: Member
@ -159,13 +159,13 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract suspend fun quit(): Boolean
/**
* 构造一个 [Member].
* 构造一个 [newMember].
* 非特殊情况请不要使用这个函数. 优先使用 [get].
*/
@JvmName("newMember")
@Suppress("INAPPLICABLE_JVM_NAME", "FunctionName")
@MiraiExperimentalAPI("dangerous")
actual abstract fun Member(memberInfo: MemberInfo): Member
actual abstract fun newMember(memberInfo: MemberInfo): Member
/**
* 向这个对象发送消息.
@ -194,40 +194,11 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract override suspend fun uploadImage(image: ExternalImage): OfflineGroupImage
actual companion object {
/**
* by @kar98k
*/
actual fun calculateGroupUinByGroupCode(groupCode: Long): Long {
var left: Long = groupCode / 1000000L
actual fun calculateGroupUinByGroupCode(groupCode: Long): Long =
CommonGroupCalculations.calculateGroupUinByGroupCode(groupCode)
when (left) {
in 0..10 -> left += 202
in 11..19 -> left += 480 - 11
in 20..66 -> left += 2100 - 20
in 67..156 -> left += 2010 - 67
in 157..209 -> left += 2147 - 157
in 210..309 -> left += 4100 - 210
in 310..499 -> left += 3800 - 310
}
return left * 1000000L + groupCode % 1000000L
}
actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long {
var left: Long = groupUin / 1000000L
when (left) {
in 0 + 202..10 + 202 -> left -= 202
in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11
in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20
in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67
in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157
in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210
in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310
}
return left * 1000000L + groupUin % 1000000L
}
actual fun calculateGroupCodeByGroupUin(groupUin: Long): Long =
CommonGroupCalculations.calculateGroupCodeByGroupUin(groupUin)
}
@MiraiExperimentalAPI