1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-03-26 07:20:09 +08:00

Replace use of Bot.uin with Bot.id

This commit is contained in:
Him188 2020-04-02 19:27:40 +08:00
parent d5868ed7c1
commit 3773548983
21 changed files with 110 additions and 136 deletions
mirai-core-qqandroid/src
androidMain/kotlin/net/mamoe/mirai/qqandroid
commonMain/kotlin/net/mamoe/mirai/qqandroid
jvmMain/kotlin/net/mamoe/mirai/qqandroid
mirai-core/src
androidMain/kotlin/net/mamoe/mirai
commonMain/kotlin/net.mamoe.mirai
jvmMain/kotlin/net/mamoe/mirai

View File

@ -7,10 +7,11 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("DEPRECATION_ERROR")
package net.mamoe.mirai.qqandroid
import net.mamoe.mirai.Bot
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.BotFactory
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.Context

View File

@ -16,22 +16,11 @@ import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.io.*
import kotlinx.io.core.*
import kotlinx.io.pool.useInstance
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.qqandroid.utils.ByteArrayPool
import net.mamoe.mirai.qqandroid.utils.toReadPacket
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.Context
import net.mamoe.mirai.utils.MiraiInternalAPI
import java.nio.ByteBuffer
@OptIn(MiraiInternalAPI::class)
internal actual class QQAndroidBot
actual constructor(
context: Context,
account: BotAccount,
configuration: BotConfiguration
) : QQAndroidBotBase(context, account, configuration)
@OptIn(MiraiInternalAPI::class)
@Suppress("DEPRECATION")
internal actual fun ByteReadChannel.toKotlinByteReadChannel(): kotlinx.coroutines.io.ByteReadChannel {

View File

@ -7,29 +7,27 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE")
@file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR")
package net.mamoe.mirai
package net.mamoe.mirai.qqandroid
import kotlinx.io.core.toByteArray
import net.mamoe.mirai.qqandroid.utils.MiraiPlatformUtils
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.utils.internal.md5
import kotlin.annotation.AnnotationTarget.*
import kotlin.jvm.JvmSynthetic
@MiraiInternalAPI
data class BotAccount(
/**
* **注意**: Android 协议, 总是使用 `QQAndroidClient.uin` [Bot.uin], 而不要使用 [BotAccount.id]. 将来 [BotAccount.id] 可能会变为 [String]
*/
@RawAccountIdUse
val id: Long,
internal data class BotAccount(
@JvmSynthetic
internal val id: Long,
@JvmSynthetic
@MiraiExperimentalAPI
@MiraiInternalAPI
val passwordMd5: ByteArray // md5
) {
constructor(id: Long, passwordPlainText: String) : this(id, passwordPlainText.toByteArray().md5())
constructor(id: Long, passwordPlainText: String) : this(id, MiraiPlatformUtils.md5(passwordPlainText.toByteArray()))
@OptIn(MiraiInternalAPI::class)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
@ -42,18 +40,10 @@ data class BotAccount(
return true
}
@OptIn(MiraiInternalAPI::class)
override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + passwordMd5.contentHashCode()
return result
}
}
/**
* 标记直接访问 [BotAccount.id], 而不是访问 [Bot.uin]. 这可能会不兼容未来的 API 修改.
*/
@MiraiInternalAPI
@Retention(AnnotationRetention.SOURCE)
@Target(CLASS, TYPEALIAS, FUNCTION, PROPERTY, FIELD, CONSTRUCTOR)
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
annotation class RawAccountIdUse
}

View File

@ -6,6 +6,10 @@
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress(
"FunctionName", "INAPPLICABLE_JVM_NAME", "DEPRECATION_ERROR", "DeprecatedCallableAddReplaceWith",
"OverridingDeprecatedMember"
)
package net.mamoe.mirai.qqandroid

View File

@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE")
@file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR")
package net.mamoe.mirai.qqandroid
@ -24,7 +24,6 @@ import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.json.int
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.BotImpl
import net.mamoe.mirai.LowLevelAPI
import net.mamoe.mirai.contact.*
@ -52,22 +51,23 @@ import net.mamoe.mirai.qqandroid.utils.toReadPacket
import net.mamoe.mirai.utils.*
import kotlin.collections.asSequence
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.JvmSynthetic
import kotlin.math.absoluteValue
import kotlin.random.Random
@OptIn(MiraiInternalAPI::class)
internal expect class QQAndroidBot constructor(
internal class QQAndroidBot constructor(
context: Context,
account: BotAccount,
configuration: BotConfiguration
) : QQAndroidBotBase
) : QQAndroidBotBase(context, account, configuration)
@OptIn(MiraiInternalAPI::class, MiraiExperimentalAPI::class)
internal abstract class QQAndroidBotBase constructor(
context: Context,
account: BotAccount,
private val account: BotAccount,
configuration: BotConfiguration
) : BotImpl<QQAndroidBotNetworkHandler>(context, account, configuration) {
) : BotImpl<QQAndroidBotNetworkHandler>(context, configuration) {
val client: QQAndroidClient =
QQAndroidClient(
context,
@ -76,7 +76,9 @@ internal abstract class QQAndroidBotBase constructor(
device = configuration.deviceInfo?.invoke(context) ?: SystemDeviceInfo(context)
)
internal var firstLoginSucceed: Boolean = false
override val uin: Long get() = client.uin
override val id: Long
get() = account.id
companion object {
@OptIn(UnstableDefault::class)
@ -88,7 +90,7 @@ internal abstract class QQAndroidBotBase constructor(
override val selfQQ: QQ by lazy {
@OptIn(LowLevelAPI::class)
_lowLevelNewQQ(object : FriendInfo {
override val uin: Long get() = this@QQAndroidBotBase.uin
override val uin: Long get() = this@QQAndroidBotBase.id
override val nick: String get() = this@QQAndroidBotBase.nick
})
}
@ -167,7 +169,7 @@ internal abstract class QQAndroidBotBase constructor(
@ExperimentalMessageSource
override suspend fun recall(source: MessageSource) {
if (source.senderId != uin && source.groupId != 0L) {
if (source.senderId != id && source.groupId != 0L) {
getGroup(source.groupId).checkBotPermissionOperator()
}
@ -370,9 +372,10 @@ internal abstract class QQAndroidBotBase constructor(
return json.parse(GroupActiveData.serializer(), rep)
}
@JvmSynthetic
@LowLevelAPI
@MiraiExperimentalAPI
internal suspend fun _lowLevelSendLongGroupMessage(groupCode: Long, message: MessageChain): MessageReceipt<Group> {
internal suspend fun lowLevelSendLongGroupMessage(groupCode: Long, message: MessageChain): MessageReceipt<Group> {
val group = getGroup(groupCode)
val time = currentTimeSeconds
@ -432,8 +435,8 @@ internal abstract class QQAndroidBotBase constructor(
return group.sendMessage(
RichMessage.longMessage(
brief = message.joinToString(limit = 27){
when(it){
brief = message.joinToString(limit = 27) {
when (it) {
is PlainText -> it.stringValue
is At -> it.display
is AtAll -> it.display

View File

@ -82,7 +82,7 @@ internal class GroupImpl(
override val muteTimestamp: Int
get() = botMuteRemaining
override val uin: Long
get() = bot.uin
get() = bot.id
override val nick: String
get() = bot.nick
})
@ -101,7 +101,7 @@ internal class GroupImpl(
}
override val members: ContactList<Member> = ContactList(members.mapNotNull {
if (it.uin == bot.uin) {
if (it.uin == bot.id) {
botPermission = it.permission
if (it.permission == MemberPermission.OWNER) {
owner = botAsMember
@ -298,7 +298,7 @@ internal class GroupImpl(
)
}
if (length >= 800) {
return bot._lowLevelSendLongGroupMessage(this.id, event.message)
return bot.lowLevelSendLongGroupMessage(this.id, event.message)
}
msg = event.message
@ -334,7 +334,7 @@ internal class GroupImpl(
bot.network.run {
val response: ImgStore.GroupPicUp.Response = ImgStore.GroupPicUp(
bot.client,
uin = bot.uin,
uin = bot.id,
groupCode = id,
md5 = image.md5,
size = image.inputSize,

View File

@ -105,7 +105,7 @@ internal class QQImpl(
bot.network.run {
val response = LongConn.OffPicUp(
bot.client, Cmd0x352.TryUpImgReq(
srcUin = bot.uin.toInt(),
srcUin = bot.id.toInt(),
dstUin = id.toInt(),
fileId = 0,
fileMd5 = image.md5,
@ -133,7 +133,7 @@ internal class QQImpl(
is LongConn.OffPicUp.Response.RequireUpload -> {
MiraiPlatformUtils.Http.postImage(
"0x6ff0070",
bot.uin,
bot.id,
null,
imageInput = image.input,
inputSize = image.inputSize,

View File

@ -7,15 +7,15 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_API_USAGE")
@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR")
package net.mamoe.mirai.qqandroid.network
import kotlinx.atomicfu.AtomicInt
import kotlinx.atomicfu.atomic
import kotlinx.io.core.*
import net.mamoe.mirai.RawAccountIdUse
import net.mamoe.mirai.data.OnlineStatus
import net.mamoe.mirai.qqandroid.BotAccount
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.qqandroid.network.protocol.packet.PacketLogger
@ -58,7 +58,7 @@ internal fun getRandomByteArray(length: Int): ByteArray = ByteArray(length) { Ra
@PublishedApi
internal open class QQAndroidClient(
context: Context,
val id: Long,
val account: BotAccount,
val ecdh: ECDH = ECDH(),
val device: DeviceInfo = SystemDeviceInfo(context),
bot: QQAndroidBot
@ -89,7 +89,7 @@ internal open class QQAndroidClient(
}
override fun toString(): String { // extremely slow
return "QQAndroidClient(id=$id, ecdh=$ecdh, device=$device, tgtgtKey=${tgtgtKey.toUHexString()}, randomKey=${randomKey.toUHexString()}, miscBitMap=$miscBitMap, mainSigMap=$mainSigMap, subSigMap=$subSigMap, openAppId=$openAppId, apkVersionName=${apkVersionName.toUHexString()}, loginState=$loginState, appClientVersion=$appClientVersion, networkType=$networkType, apkSignatureMd5=${apkSignatureMd5.toUHexString()}, protocolVersion=$protocolVersion, apkId=${apkId.toUHexString()}, t150=${t150?.value?.toUHexString()}, rollbackSig=${rollbackSig?.toUHexString()}, ipFromT149=${ipFromT149?.toUHexString()}, timeDifference=$timeDifference, uin=$uin, t530=${t530?.toUHexString()}, t528=${t528?.toUHexString()}, ksid='$ksid', pwdFlag=$pwdFlag, loginExtraData=$loginExtraData, wFastLoginInfo=$wFastLoginInfo, reserveUinInfo=$reserveUinInfo, wLoginSigInfo=$wLoginSigInfo, tlv113=${tlv113?.toUHexString()}, qrPushSig=${qrPushSig.toUHexString()}, mainDisplayName='$mainDisplayName')"
return "QQAndroidClient(account=$account, ecdh=$ecdh, device=$device, tgtgtKey=${tgtgtKey.toUHexString()}, randomKey=${randomKey.toUHexString()}, miscBitMap=$miscBitMap, mainSigMap=$mainSigMap, subSigMap=$subSigMap, openAppId=$openAppId, apkVersionName=${apkVersionName.toUHexString()}, loginState=$loginState, appClientVersion=$appClientVersion, networkType=$networkType, apkSignatureMd5=${apkSignatureMd5.toUHexString()}, protocolVersion=$protocolVersion, apkId=${apkId.toUHexString()}, t150=${t150?.value?.toUHexString()}, rollbackSig=${rollbackSig?.toUHexString()}, ipFromT149=${ipFromT149?.toUHexString()}, timeDifference=$timeDifference, uin=$uin, t530=${t530?.toUHexString()}, t528=${t528?.toUHexString()}, ksid='$ksid', pwdFlag=$pwdFlag, loginExtraData=$loginExtraData, wFastLoginInfo=$wFastLoginInfo, reserveUinInfo=$reserveUinInfo, wLoginSigInfo=$wLoginSigInfo, tlv113=${tlv113?.toUHexString()}, qrPushSig=${qrPushSig.toUHexString()}, mainDisplayName='$mainDisplayName')"
}
var onlineStatus: OnlineStatus = OnlineStatus.ONLINE
@ -174,9 +174,8 @@ internal open class QQAndroidClient(
*/
val uin: Long get() = _uin
@OptIn(RawAccountIdUse::class)
@Suppress("PropertyName", "DEPRECATION_ERROR")
internal var _uin: Long = bot.account.id
@Suppress("PropertyName")
internal var _uin: Long = account.id
var t530: ByteArray? = null
var t528: ByteArray? = null

View File

@ -10,11 +10,11 @@
package net.mamoe.mirai.qqandroid.network.protocol.packet.chat
import kotlinx.io.core.ByteReadPacket
import net.mamoe.mirai.qqandroid.network.Packet
import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.io.serialization.readProtoBuf
import net.mamoe.mirai.qqandroid.io.serialization.toByteArray
import net.mamoe.mirai.qqandroid.io.serialization.writeProtoBuf
import net.mamoe.mirai.qqandroid.network.Packet
import net.mamoe.mirai.qqandroid.network.QQAndroidClient
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgRevokeUserDef
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgSvc
@ -87,7 +87,7 @@ internal class PbMessageSvc {
subCmd = 1,
msgInfo = listOf(
MsgSvc.PbC2CMsgWithDrawReq.MsgInfo(
fromUin = client.bot.uin,
fromUin = client.bot.id,
toUin = toUin,
msgSeq = messageSequenceId,
msgUid = messageUid,

View File

@ -154,7 +154,7 @@ internal class MessageSvc {
when (msg.msgHead.msgType) {
33 -> {
val group = bot.getGroupByUinOrNull(msg.msgHead.fromUin)
if (msg.msgHead.authUin == bot.uin) {
if (msg.msgHead.authUin == bot.id) {
if (group != null) {
return@mapNotNull null
}
@ -217,7 +217,7 @@ internal class MessageSvc {
val friend = bot.getFriendOrNull(msg.msgHead.fromUin) ?: return@mapNotNull null
friend.checkIsQQImpl()
if (msg.msgHead.fromUin == bot.uin || !bot.firstLoginSucceed) {
if (msg.msgHead.fromUin == bot.id || !bot.firstLoginSucceed) {
return@mapNotNull null
}

View File

@ -42,10 +42,10 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.IncomingPacketFactory
import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.qqandroid.network.protocol.packet.buildResponseUniPacket
import net.mamoe.mirai.qqandroid.utils.io.readString
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.utils.debug
import net.mamoe.mirai.qqandroid.utils.read
import net.mamoe.mirai.qqandroid.utils.toUHexString
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.utils.debug
internal class OnlinePush {
/**
@ -70,7 +70,7 @@ internal class OnlinePush {
val extraInfo: ImMsgBody.ExtraInfo? =
pbPushMsg.msg.msgBody.richText.elems.firstOrNull { it.extraInfo != null }?.extraInfo
if (pbPushMsg.msg.msgHead.fromUin == bot.uin) {
if (pbPushMsg.msg.msgHead.fromUin == bot.id) {
return SendGroupMessageReceipt(
pbPushMsg.msg.msgBody.richText.attr!!.random,
pbPushMsg.msg.msgHead.msgSeq
@ -122,7 +122,7 @@ internal class OnlinePush {
.toInt() == 1
) MemberPermission.ADMINISTRATOR else MemberPermission.MEMBER
return if (target == bot.uin) {
return if (target == bot.id) {
BotGroupPermissionChangeEvent(
group,
group.botPermission.also { group.botPermission = newPermission },
@ -211,7 +211,7 @@ internal class OnlinePush {
when (val internalType = this.readByte().toInt().also { this.discardExact(1) }) {
0x0c -> { // mute
val operatorUin = this.readUInt().toLong()
if (operatorUin == bot.uin) {
if (operatorUin == bot.id) {
return@flatMap sequenceOf()
}
val operator = group.getOrNull(operatorUin) ?: return@flatMap sequenceOf()
@ -245,7 +245,7 @@ internal class OnlinePush {
)
}
} else {
if (target == bot.uin) {
if (target == bot.id) {
if (group._botMuteTimestamp == time) {
return@flatMap sequenceOf()
}
@ -362,7 +362,7 @@ internal class OnlinePush {
group.getOrNull(recallReminder.uin) ?: return@flatMap sequenceOf()
return@flatMap recallReminder.recalledMsgList.asSequence()
.mapNotNull { meta ->
if (meta.authorUin == bot.uin) {
if (meta.authorUin == bot.id) {
null
} else MessageRecallEvent.GroupRecall(
bot,

View File

@ -7,12 +7,11 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("FunctionName")
@file:Suppress("FunctionName", "OverridingDeprecatedMember")
package net.mamoe.mirai.qqandroid
import net.mamoe.mirai.Bot
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.BotFactory
import net.mamoe.mirai.qqandroid.QQAndroid.Bot
import net.mamoe.mirai.utils.BotConfiguration

View File

@ -16,13 +16,11 @@ import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.io.*
import kotlinx.io.core.*
import kotlinx.io.pool.useInstance
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.qqandroid.utils.ByteArrayPool
import net.mamoe.mirai.qqandroid.utils.toReadPacket
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.Context
import net.mamoe.mirai.utils.ContextImpl
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.qqandroid.utils.toReadPacket
import java.nio.ByteBuffer
@OptIn(MiraiInternalAPI::class)
@ -30,13 +28,6 @@ import java.nio.ByteBuffer
internal fun QQAndroidBot(account: BotAccount, configuration: BotConfiguration): QQAndroidBot =
QQAndroidBot(ContextImpl(), account, configuration)
@OptIn(MiraiInternalAPI::class)
internal actual class QQAndroidBot actual constructor(
context: Context,
account: BotAccount,
configuration: BotConfiguration
) : QQAndroidBotBase(context, account, configuration)
@OptIn(MiraiInternalAPI::class)
@Suppress("DEPRECATION")
internal actual fun ByteReadChannel.toKotlinByteReadChannel(): kotlinx.coroutines.io.ByteReadChannel {

View File

@ -57,10 +57,15 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
*/
actual abstract val context: Context
@PlannedRemoval("1.0.0")
@Deprecated("use id instead", replaceWith = ReplaceWith("id"))
actual abstract val uin: Long
/**
* QQ 号码. 实际类型为 uint
*/
actual abstract val uin: Long
@SinceMirai("0.32.0")
actual abstract val id: Long
/**
* 昵称
@ -87,9 +92,9 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
* 获取一个好友对象. 若没有这个好友, 则会抛出异常 [NoSuchElementException]
*/
actual fun getFriend(id: Long): QQ {
if (id == uin) return selfQQ
if (id == this.id) return selfQQ
return friends.delegate.getOrNull(id)
?: throw NoSuchElementException("No such friend $id for bot ${this.uin}")
?: throw NoSuchElementException("No such friend $id for bot ${this.id}")
}
/**
@ -104,7 +109,7 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
*/
actual fun getGroup(id: Long): Group {
return groups.delegate.getOrNull(id)
?: throw NoSuchElementException("No such group $id for bot ${this.uin}")
?: throw NoSuchElementException("No such group $id for bot ${this.id}")
}
// endregion
@ -197,5 +202,5 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
actual abstract fun close(cause: Throwable?)
@OptIn(LowLevelAPI::class, MiraiExperimentalAPI::class)
actual final override fun toString(): String = "Bot(${uin})"
actual final override fun toString(): String = "Bot($id)"
}

View File

@ -77,10 +77,15 @@ expect abstract class Bot() : CoroutineScope, LowLevelBotAPIAccessor {
*/
abstract val context: Context
@PlannedRemoval("1.0.0")
@Deprecated("use id instead", replaceWith = ReplaceWith("id"))
abstract val uin: Long
/**
* QQ 号码. 实际类型为 uint
*/
abstract val uin: Long
@SinceMirai("0.32.0")
abstract val id: Long
/**
* 昵称

View File

@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("FunctionName")
@file:Suppress("FunctionName", "INAPPLICABLE_JVM_NAME", "DEPRECATION_ERROR", "DeprecatedCallableAddReplaceWith")
package net.mamoe.mirai
@ -22,7 +22,6 @@ import kotlin.jvm.JvmName
* - `mirai-core-timpc`: `TIMPC`
* - `mirai-core-qqandroid`: `QQAndroid`
*/
@Suppress("INAPPLICABLE_JVM_NAME")
interface BotFactory {
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
@ -55,8 +54,7 @@ inline fun BotFactory.Bot(
qq: Long,
password: String,
configuration: (BotConfiguration.() -> Unit)
): Bot =
this.Bot(context, qq, password, BotConfiguration().apply(configuration))
): Bot = this.Bot(context, qq, password, BotConfiguration().apply(configuration))
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
@ -64,7 +62,6 @@ inline fun BotFactory.Bot(
inline fun BotFactory.Bot(
context: Context,
qq: Long,
passwordMd5: ByteArray,
password: ByteArray,
configuration: (BotConfiguration.() -> Unit)
): Bot =
this.Bot(context, qq, passwordMd5, BotConfiguration().apply(configuration))
): Bot = this.Bot(context, qq, password, BotConfiguration().apply(configuration))

View File

@ -7,7 +7,7 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR")
@file:Suppress("EXPERIMENTAL_API_USAGE", "DEPRECATION_ERROR", "OverridingDeprecatedMember")
package net.mamoe.mirai
@ -32,27 +32,23 @@ import kotlin.coroutines.CoroutineContext
@MiraiInternalAPI
abstract class BotImpl<N : BotNetworkHandler> constructor(
context: Context,
account: BotAccount,
val configuration: BotConfiguration
) : Bot(), CoroutineScope {
private val botJob = SupervisorJob(configuration.parentCoroutineContext[Job])
final override val coroutineContext: CoroutineContext =
configuration.parentCoroutineContext + botJob + (configuration.parentCoroutineContext[CoroutineExceptionHandler]
?: CoroutineExceptionHandler { _, e ->
logger.error(
"An exception was thrown under a coroutine of Bot",
e
)
})
configuration.parentCoroutineContext + SupervisorJob(configuration.parentCoroutineContext[Job]) +
(configuration.parentCoroutineContext[CoroutineExceptionHandler]
?: CoroutineExceptionHandler { _, e ->
logger.error(
"An exception was thrown under a coroutine of Bot",
e
)
})
override val context: Context by context.unsafeWeakRef()
@OptIn(LowLevelAPI::class)
@Suppress("CanBePrimaryConstructorProperty", "OverridingDeprecatedMember") // for logger
final override val account: BotAccount = account
@OptIn(RawAccountIdUse::class)
@Deprecated("use id instead", replaceWith = ReplaceWith("id"))
override val uin: Long
get() = this.account.id
get() = this.id
final override val logger: MiraiLogger by lazy { configuration.botLoggerSupplier(this) }
@ -71,7 +67,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
fun getInstance(qq: Long): Bot {
instances.forEach {
it.get()?.let { bot ->
if (bot.uin == qq) {
if (bot.id == qq) {
return bot
}
}
@ -188,20 +184,20 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
groups.delegate.clear() // job is cancelled, so child jobs are to be cancelled
friends.delegate.clear()
instances.removeIf { it.get()?.uin == this.uin }
instances.removeIf { it.get()?.id == this.id }
}
}
@OptIn(MiraiInternalAPI::class)
override fun close(cause: Throwable?) {
if (!this.botJob.isActive) {
if (!this.isActive) {
// already cancelled
return
}
if (cause == null) {
this.botJob.cancel()
this.cancel()
} else {
this.botJob.cancel(CancellationException("bot cancelled", cause))
this.cancel(CancellationException("bot cancelled", cause))
}
}
}

View File

@ -131,7 +131,7 @@ sealed class MessageRecallEvent : BotEvent {
val operator: Long
) : MessageRecallEvent(), Packet {
override val authorId: Long
get() = bot.uin
get() = bot.id
}
data class GroupRecall(
@ -150,9 +150,9 @@ sealed class MessageRecallEvent : BotEvent {
@OptIn(MiraiExperimentalAPI::class)
val MessageRecallEvent.GroupRecall.author: Member
get() = if (authorId == bot.uin) group.botAsMember else group[authorId]
get() = if (authorId == bot.id) group.botAsMember else group[authorId]
val MessageRecallEvent.FriendRecall.isByBot: Boolean get() = this.operator == bot.uin
val MessageRecallEvent.FriendRecall.isByBot: Boolean get() = this.operator == bot.id
val MessageRecallEvent.isByBot: Boolean
get() = when (this) {

View File

@ -15,7 +15,6 @@ import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.data.*
import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.utils.SinceMirai
import net.mamoe.mirai.utils.WeakRef
@ -37,15 +36,6 @@ annotation class LowLevelAPI
@Suppress("FunctionName", "unused")
@LowLevelAPI
interface LowLevelBotAPIAccessor {
/**
* 账号信息
*/
@Deprecated("将来会做修改", level = DeprecationLevel.ERROR)
@MiraiExperimentalAPI
@LowLevelAPI
@MiraiInternalAPI
val account: BotAccount
/**
* 构造一个 [_lowLevelNewQQ] 对象. 它持有对 [Bot] 的弱引用([WeakRef]).
*

View File

@ -25,12 +25,12 @@ open class BotConfiguration {
/**
* 日志记录器
*/
var botLoggerSupplier: ((Bot) -> MiraiLogger) = { DefaultLogger("Bot(${it.uin})") }
var botLoggerSupplier: ((Bot) -> MiraiLogger) = { DefaultLogger("Bot(${it.id})") }
/**
* 网络层日志构造器
*/
var networkLoggerSupplier: ((BotNetworkHandler) -> MiraiLogger) = { DefaultLogger("Network(${it.bot.uin})") }
var networkLoggerSupplier: ((BotNetworkHandler) -> MiraiLogger) = { DefaultLogger("Network(${it.bot.id})") }
/**
* 设备信息覆盖. 默认使用随机的设备信息.

View File

@ -67,10 +67,15 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
*/
actual abstract val context: Context
@PlannedRemoval("1.0.0")
@Deprecated("use id instead", replaceWith = ReplaceWith("id"))
actual abstract val uin: Long
/**
* QQ 号码. 实际类型为 uint
*/
actual abstract val uin: Long
@SinceMirai("0.32.0")
actual abstract val id: Long
/**
* 昵称
@ -97,9 +102,9 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
* 获取一个好友对象. 若没有这个好友, 则会抛出异常 [NoSuchElementException]
*/
actual fun getFriend(id: Long): QQ {
if (id == uin) return selfQQ
if (id == this.id) return selfQQ
return friends.delegate.getOrNull(id)
?: throw NoSuchElementException("No such friend $id for bot ${this.uin}")
?: throw NoSuchElementException("No such friend $id for bot ${this.id}")
}
/**
@ -114,7 +119,7 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
*/
actual fun getGroup(id: Long): Group {
return groups.delegate.getOrNull(id)
?: throw NoSuchElementException("No such group $id for bot ${this.uin}")
?: throw NoSuchElementException("No such group $id for bot ${this.id}")
}
// endregion
@ -207,5 +212,5 @@ actual abstract class Bot actual constructor() : CoroutineScope, LowLevelBotAPIA
actual abstract fun close(cause: Throwable?)
@OptIn(LowLevelAPI::class, MiraiExperimentalAPI::class)
actual final override fun toString(): String = "Bot(${uin})"
actual final override fun toString(): String = "Bot($id)"
}