forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotFactory.kt
164 lines (147 loc) · 5.04 KB
/
BotFactory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("unused", "NOTHING_TO_INLINE")
package net.mamoe.mirai
import net.mamoe.mirai.utils.BotConfiguration
import kotlin.jvm.JvmSynthetic
/**
* 构造 [Bot] 的工厂. 这是 [Bot] 唯一的构造方式.
*
* @see IMirai.BotFactory
*/
public interface BotFactory {
/**
* 相当于 Kotlin lambda `BotConfiguration.() -> Unit` 和 Java `Consumer<BotConfiguration>`
*
* @see newBot
*/
public fun interface BotConfigurationLambda {
public operator fun BotConfiguration.invoke()
}
///////////////////////////////////////////////////////////////////////////
// Plain Password
///////////////////////////////////////////////////////////////////////////
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
public fun newBot(qq: Long, password: String, configuration: BotConfiguration): Bot
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*
* Kotlin:
* ```
* newBot(123, "") {
* // this: BotConfiguration
* fileBasedDeviceInfo()
* }
* ```
*
* Java:
* ```java
* newBot(123, "", configuration -> {
* configuration.fileBasedDeviceInfo()
* })
* ```
*/
public fun newBot(
qq: Long,
password: String,
configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
): Bot = newBot(qq, password, configuration.run { BotConfiguration().apply { invoke() } })
/**
* 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
*/
public fun newBot(qq: Long, password: String): Bot = newBot(qq, password, BotConfiguration.Default)
///////////////////////////////////////////////////////////////////////////
// MD5 Password
///////////////////////////////////////////////////////////////////////////
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*
* @param passwordMd5 16 bytes
*/
public fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*
* Kotlin:
* ```
* newBot(123, password) {
* // this: BotConfiguration
* fileBasedDeviceInfo()
* }
* ```
*
* Java:
* ```java
* newBot(123, password, configuration -> {
* configuration.fileBasedDeviceInfo()
* })
* ```
*
* @param passwordMd5 16 bytes
*/
public fun newBot(
qq: Long,
passwordMd5: ByteArray,
configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
): Bot = newBot(qq, passwordMd5, configuration.run { BotConfiguration().apply { invoke() } })
/**
* 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
*
* @param passwordMd5 16 bytes
*/
public fun newBot(qq: Long, passwordMd5: ByteArray): Bot = newBot(qq, passwordMd5, BotConfiguration.Default)
public companion object INSTANCE : BotFactory {
override fun newBot(qq: Long, password: String, configuration: BotConfiguration): Bot {
return Mirai.BotFactory.newBot(qq, password, configuration)
}
override fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot {
return Mirai.BotFactory.newBot(qq, passwordMd5, configuration)
}
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*
* ```
* newBot(123, "") {
* // this: BotConfiguration
* fileBasedDeviceInfo()
* }
* ```
*
* @since 2.7
*/
@JvmSynthetic
public inline fun newBot(
qq: Long,
password: String,
configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
): Bot = newBot(qq, password, BotConfiguration().apply(configuration))
// implementation notes: this is inline for `inheritCoroutineContext()`
// see https://github.com/mamoe/mirai/commit/0dbb448cad1ed4773d48ccb8c0b497841bc9fa4c#r50249446
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*
* ```
* newBot(123, password) {
* // this: BotConfiguration
* fileBasedDeviceInfo()
* }
* ```
*
* @since 2.7
*/
@JvmSynthetic
public inline fun newBot(
qq: Long,
passwordMd5: ByteArray,
configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
): Bot = newBot(qq, passwordMd5, BotConfiguration().apply(configuration))
}
}