mirai/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotHelper.kt

122 lines
4.1 KiB
Kotlin
Raw Normal View History

2019-11-23 23:14:17 +08:00
@file:JvmMultifileClass
@file:JvmName("BotHelperKt")
2019-10-13 20:19:54 +08:00
@file:Suppress("unused", "EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai
2019-10-24 15:01:56 +08:00
import net.mamoe.mirai.contact.*
2019-10-26 17:06:40 +08:00
import net.mamoe.mirai.network.BotNetworkHandler
2019-10-26 15:54:02 +08:00
import net.mamoe.mirai.network.BotSession
2019-12-06 23:47:48 +08:00
import net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler
2019-10-22 20:08:08 +08:00
import net.mamoe.mirai.network.protocol.tim.packet.OutgoingPacket
2019-10-13 20:19:54 +08:00
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult
2019-11-16 13:27:29 +08:00
import net.mamoe.mirai.network.protocol.tim.packet.login.requireSuccess
2019-11-07 15:06:38 +08:00
import net.mamoe.mirai.utils.BotConfiguration
2019-10-27 03:36:26 +08:00
import net.mamoe.mirai.utils.internal.PositiveNumbers
import net.mamoe.mirai.utils.internal.coerceAtLeastOrFail
2019-11-16 13:27:29 +08:00
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
2019-11-23 23:14:17 +08:00
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
2019-11-09 21:27:22 +08:00
import kotlin.jvm.JvmOverloads
2019-10-13 20:19:54 +08:00
2019-10-24 15:01:56 +08:00
/*
2019-10-26 17:06:40 +08:00
* [Bot] 中的方法的捷径
2019-10-13 20:19:54 +08:00
*/
//Contacts
suspend inline fun Bot.getQQ(@PositiveNumbers number: Long): QQ = this.contacts.getQQ(number)
2019-11-09 21:27:22 +08:00
2019-10-26 17:06:40 +08:00
suspend inline fun Bot.getQQ(number: UInt): QQ = this.contacts.getQQ(number)
2019-10-13 20:19:54 +08:00
2019-10-27 03:36:26 +08:00
suspend inline fun Bot.getGroup(id: UInt): Group = this.contacts.getGroup(GroupId(id))
2019-12-06 23:47:48 +08:00
suspend inline fun Bot.getGroup(@PositiveNumbers id: Long): Group =
this.contacts.getGroup(GroupId(id.coerceAtLeastOrFail(0).toUInt()))
2019-10-26 17:06:40 +08:00
suspend inline fun Bot.getGroup(id: GroupId): Group = this.contacts.getGroup(id)
suspend inline fun Bot.getGroup(internalId: GroupInternalId): Group = this.contacts.getGroup(internalId)
2019-10-13 20:19:54 +08:00
2019-10-26 17:06:40 +08:00
/**
2019-11-28 12:08:35 +08:00
* 取得群列表
2019-10-26 17:06:40 +08:00
*/
inline val Bot.groups: ContactList<Group> get() = this.contacts.groups
2019-10-13 20:19:54 +08:00
2019-10-26 17:17:46 +08:00
/**
2019-11-28 12:08:35 +08:00
* 取得好友列表
2019-10-26 17:17:46 +08:00
*/
inline val Bot.qqs: ContactList<QQ> get() = this.contacts.qqs
2019-10-26 15:54:02 +08:00
2019-10-26 17:06:40 +08:00
/**
* [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
2019-11-12 14:51:01 +08:00
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, [BotSession.sendAndExpectAsync]
2019-10-26 17:06:40 +08:00
*/
2019-11-16 13:27:29 +08:00
@UseExperimental(ExperimentalContracts::class)
inline fun <R> Bot.withSession(block: BotSession.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return with(this.network.session) { block() }
}
2019-10-13 20:19:54 +08:00
2019-10-26 17:06:40 +08:00
/**
* 发送数据包
* @throws IllegalStateException [BotNetworkHandler.socket] 未开启时
*/
2019-12-06 23:47:48 +08:00
internal suspend inline fun Bot.sendPacket(packet: OutgoingPacket) =
(this.network as TIMBotNetworkHandler).socket.sendPacket(packet)
2019-10-13 20:19:54 +08:00
2019-10-26 17:06:40 +08:00
/**
2019-11-02 01:17:48 +08:00
* 使用在默认配置基础上修改的配置进行登录
2019-10-26 17:06:40 +08:00
*/
2019-11-16 13:27:29 +08:00
@UseExperimental(ExperimentalContracts::class)
2019-12-13 12:16:01 +08:00
suspend inline fun Bot.login(configuration: BotConfiguration.() -> Unit): LoginResult {
2019-11-16 13:27:29 +08:00
contract {
callsInPlace(configuration, InvocationKind.EXACTLY_ONCE)
}
return this.reinitializeNetworkHandler(BotConfiguration().apply(configuration))
2019-11-16 13:27:29 +08:00
}
2019-10-13 20:19:54 +08:00
2019-10-26 17:06:40 +08:00
/**
2019-11-23 22:58:21 +08:00
* 使用默认的配置 ([BotConfiguration.Default]) 登录, 返回登录结果
2019-10-26 17:06:40 +08:00
*/
suspend inline fun Bot.login(): LoginResult = this.reinitializeNetworkHandler(BotConfiguration.Default)
2019-10-13 20:19:54 +08:00
2019-11-16 13:27:29 +08:00
/**
2019-11-23 22:58:21 +08:00
* 使用默认的配置 ([BotConfiguration.Default]) 登录, 返回 [this]
*/
suspend inline fun Bot.alsoLogin(): Bot = apply { login().requireSuccess() }
2019-11-25 14:03:19 +08:00
/**
* 使用在默认配置基础上修改的配置进行登录, 返回 [this]
*/
@UseExperimental(ExperimentalContracts::class)
2019-12-13 12:14:34 +08:00
suspend inline fun Bot.alsoLogin(configuration: BotConfiguration.() -> Unit): Bot {
2019-11-25 14:03:19 +08:00
contract {
callsInPlace(configuration, InvocationKind.EXACTLY_ONCE)
}
this.reinitializeNetworkHandler(BotConfiguration().apply(configuration)).requireSuccess()
2019-11-25 14:03:19 +08:00
return this
}
2019-11-23 22:58:21 +08:00
/**
* 使用默认的配置 ([BotConfiguration.Default]) 登录, 返回 [this]
2019-11-16 13:27:29 +08:00
*/
2019-11-25 14:05:09 +08:00
suspend inline fun Bot.alsoLogin(message: String): Bot {
2019-11-16 13:27:29 +08:00
return this.apply {
2019-11-25 14:05:09 +08:00
login().requireSuccess { message } // requireSuccess is inline, so no performance waste
2019-11-16 13:27:29 +08:00
}
}
2019-11-09 21:27:22 +08:00
/**
* 添加好友
*/
2019-11-16 13:27:29 +08:00
@UseExperimental(ExperimentalContracts::class)
2019-11-09 21:27:22 +08:00
@JvmOverloads
2019-12-06 23:47:48 +08:00
suspend inline fun Bot.addFriend(id: UInt, message: String? = null, remark: String? = null): AddFriendResult =
contacts.addFriend(id, message, remark)
2019-11-09 21:27:22 +08:00
2019-10-26 17:17:46 +08:00
/**
* 取得机器人的 QQ
*/
2019-10-26 17:06:40 +08:00
inline val Bot.qqAccount: UInt get() = this.account.id