BotConfiguration.kt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright 2019-2021 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/master/LICENSE
  8. */
  9. @file:Suppress("unused", "DEPRECATION_ERROR", "EXPOSED_SUPER_CLASS", "MemberVisibilityCanBePrivate")
  10. @file:JvmMultifileClass
  11. @file:JvmName("Utils")
  12. package net.mamoe.mirai.utils
  13. import kotlinx.coroutines.Job
  14. import kotlinx.coroutines.SupervisorJob
  15. import kotlinx.serialization.json.Json
  16. import net.mamoe.mirai.Bot
  17. import net.mamoe.mirai.BotFactory
  18. import net.mamoe.mirai.event.events.BotOfflineEvent
  19. import java.io.File
  20. import java.io.InputStream
  21. import kotlin.coroutines.CoroutineContext
  22. import kotlin.coroutines.EmptyCoroutineContext
  23. import kotlin.coroutines.coroutineContext
  24. import kotlin.time.Duration
  25. import kotlin.time.ExperimentalTime
  26. /**
  27. * [Bot] 配置. 用于 [BotFactory.newBot]
  28. *
  29. * Kotlin 使用方法:
  30. * ```
  31. * val bot = BotFactory.newBot(...) {
  32. * // 在这里配置 Bot
  33. *
  34. * bogLoggerSupplier = { bot -> ... }
  35. * fileBasedDeviceInfo()
  36. * inheritCoroutineContext() // 使用 `coroutineScope` 的 Job 作为父 Job
  37. * }
  38. * ```
  39. *
  40. * Java 使用方法:
  41. * ```java
  42. * Bot bot = BotFactory.newBot(..., new BotConfiguration() {{
  43. * setBogLoggerSupplier((Bot bot) -> { ... })
  44. * fileBasedDeviceInfo()
  45. * ...
  46. * }})
  47. * ```
  48. */
  49. @Suppress("PropertyName")
  50. public open class BotConfiguration { // open for Java
  51. /**
  52. * 工作目录. 默认为 "."
  53. */
  54. public var workingDir: File = File(".")
  55. /**
  56. * Json 序列化器, 使用 'kotlinx.serialization'
  57. */
  58. @MiraiExperimentalApi
  59. public var json: Json = kotlin.runCatching {
  60. Json {
  61. isLenient = true
  62. ignoreUnknownKeys = true
  63. prettyPrint = true
  64. }
  65. }.getOrElse { Json {} }
  66. ///////////////////////////////////////////////////////////////////////////
  67. // Coroutines
  68. ///////////////////////////////////////////////////////////////////////////
  69. /** 父 [CoroutineContext]. [Bot] 创建后会使用 [SupervisorJob] 覆盖其 [Job], 但会将这个 [Job] 作为父 [Job] */
  70. public var parentCoroutineContext: CoroutineContext = EmptyCoroutineContext
  71. /**
  72. * 使用当前协程的 [coroutineContext] 作为 [parentCoroutineContext].
  73. *
  74. * Bot 将会使用一个 [SupervisorJob] 覆盖 [coroutineContext] 当前协程的 [Job], 并使用当前协程的 [Job] 作为父 [Job]
  75. *
  76. * 用例:
  77. * ```
  78. * coroutineScope {
  79. * val bot = Bot(...) {
  80. * inheritCoroutineContext()
  81. * }
  82. * bot.login()
  83. * } // coroutineScope 会等待 Bot 退出
  84. * ```
  85. *
  86. *
  87. * **注意**: `bot.cancel` 时将会让父 [Job] 也被 cancel.
  88. * ```
  89. * coroutineScope { // this: CoroutineScope
  90. * launch {
  91. * while(isActive) {
  92. * delay(500)
  93. * println("I'm alive")
  94. * }
  95. * }
  96. *
  97. * val bot = Bot(...) {
  98. * inheritCoroutineContext() // 使用 `coroutineScope` 的 Job 作为父 Job
  99. * }
  100. * bot.login()
  101. * bot.cancel() // 取消了整个 `coroutineScope`, 因此上文不断打印 `"I'm alive"` 的协程也会被取消.
  102. * }
  103. * ```
  104. *
  105. * 因此, 此函数尤为适合在 `suspend fun main()` 中使用, 它能阻止主线程退出:
  106. * ```
  107. * suspend fun main() {
  108. * val bot = Bot() {
  109. * inheritCoroutineContext()
  110. * }
  111. * bot.eventChannel.subscribe { ... }
  112. *
  113. * // 主线程不会退出, 直到 Bot 离线.
  114. * }
  115. * ```
  116. *
  117. * 简言之,
  118. * - 若想让 [Bot] 作为 '守护进程' 运行, 则无需调用 [inheritCoroutineContext].
  119. * - 若想让 [Bot] 依赖于当前协程, 让当前协程等待 [Bot] 运行, 则使用 [inheritCoroutineContext]
  120. *
  121. * @see parentCoroutineContext
  122. */
  123. @JvmSynthetic
  124. @ConfigurationDsl
  125. public suspend inline fun inheritCoroutineContext() {
  126. parentCoroutineContext = coroutineContext
  127. }
  128. ///////////////////////////////////////////////////////////////////////////
  129. // Connection
  130. ///////////////////////////////////////////////////////////////////////////
  131. /** 连接心跳包周期. 过长会导致被服务器断开连接. */
  132. public var heartbeatPeriodMillis: Long = 60.secondsToMillis
  133. /**
  134. * 状态心跳包周期. 过长会导致掉线.
  135. * 该值会在登录时根据服务器下发的配置自动进行更新.
  136. * @since 2.6
  137. * @see heartbeatStrategy
  138. */
  139. public var statHeartbeatPeriodMillis: Long = 300.secondsToMillis
  140. /**
  141. * 心跳策略.
  142. * @since 2.6.3
  143. */
  144. public var heartbeatStrategy: HeartbeatStrategy = HeartbeatStrategy.STAT_HB
  145. /**
  146. * 心跳策略.
  147. * @since 2.6.3
  148. */
  149. public enum class HeartbeatStrategy {
  150. /**
  151. * 使用 2.6.0 增加的*状态心跳* (Stat Heartbeat). 通常推荐这个模式.
  152. *
  153. * 该模式大多数情况下更稳定. 但有些账号使用这个模式时会遇到一段时间后发送消息成功但客户端不可见的问题.
  154. */
  155. STAT_HB,
  156. /**
  157. * 不发送状态心跳, 而是发送*切换在线状态* (可能会导致频繁的好友或客户端上线提示, 也可能产生短暂 (几秒) 发送消息不可见的问题).
  158. *
  159. * 建议在 [STAT_HB] 不可用时使用 [REGISTER].
  160. */
  161. REGISTER,
  162. /**
  163. * 不主动维护会话. 多数账号会每 16 分钟掉线然后重连. 则会有短暂的不可用时间.
  164. *
  165. * 仅当 [STAT_HB] 和 [REGISTER] 都造成无法接收等问题时使用.
  166. * 同时请在 [https://github.com/mamoe/mirai/issues/1209] 提交问题.
  167. */
  168. NONE;
  169. }
  170. /**
  171. * 每次心跳时等待结果的时间.
  172. * 一旦心跳超时, 整个网络服务将会重启 (将消耗约 1s). 除正在进行的任务 (如图片上传) 会被中断外, 事件和插件均不受影响.
  173. */
  174. public var heartbeatTimeoutMillis: Long = 5.secondsToMillis
  175. /** 心跳失败后的第一次重连前的等待时间. */
  176. @Deprecated(
  177. "Useless since new network. Please just remove this.",
  178. level = DeprecationLevel.ERROR
  179. ) // deprecated since 2.7, error since 2.8
  180. public var firstReconnectDelayMillis: Long = 5.secondsToMillis
  181. /** 重连失败后, 继续尝试的每次等待时间 */
  182. @Deprecated(
  183. "Useless since new network. Please just remove this.",
  184. level = DeprecationLevel.ERROR
  185. ) // deprecated since 2.7, error since 2.8
  186. public var reconnectPeriodMillis: Long = 5.secondsToMillis
  187. /** 最多尝试多少次重连 */
  188. public var reconnectionRetryTimes: Int = Int.MAX_VALUE
  189. /**
  190. * 在被挤下线时 ([BotOfflineEvent.Force]) 自动重连. 默认为 `false`.
  191. *
  192. * 其他情况掉线都默认会自动重连, 详见 [BotOfflineEvent.reconnect]
  193. *
  194. * @since 2.1
  195. */
  196. public var autoReconnectOnForceOffline: Boolean = false
  197. /**
  198. * 验证码处理器
  199. *
  200. * - 在 Android 需要手动提供 [LoginSolver]
  201. * - 在 JVM, Mirai 会根据环境支持情况选择 Swing/CLI 实现
  202. *
  203. * 详见 [LoginSolver.Default]
  204. *
  205. * @see LoginSolver
  206. */
  207. public var loginSolver: LoginSolver? = LoginSolver.Default
  208. /** 使用协议类型 */
  209. public var protocol: MiraiProtocol = MiraiProtocol.ANDROID_PHONE
  210. public enum class MiraiProtocol {
  211. /**
  212. * Android 手机. 所有功能都支持.
  213. */
  214. ANDROID_PHONE,
  215. /**
  216. * Android 平板.
  217. *
  218. * 注意: 不支持戳一戳事件解析
  219. */
  220. ANDROID_PAD,
  221. /**
  222. * Android 手表.
  223. */
  224. ANDROID_WATCH,
  225. /**
  226. * iPad - 来自MiraiGo
  227. *
  228. * @since 2.8
  229. */
  230. IPAD,
  231. /**
  232. * MacOS - 来自MiraiGo
  233. *
  234. * @since 2.8
  235. */
  236. MACOS,
  237. }
  238. /**
  239. * Highway 通道上传图片, 语音, 文件等资源时的协程数量.
  240. *
  241. * 每个协程的速度约为 200KB/s. 协程数量越多越快, 同时也更要求性能.
  242. * 默认 [CPU 核心数][Runtime.availableProcessors].
  243. *
  244. * @since 2.2
  245. */
  246. public var highwayUploadCoroutineCount: Int = Runtime.getRuntime().availableProcessors()
  247. /**
  248. * 设置 [autoReconnectOnForceOffline] 为 `true`, 即在被挤下线时自动重连.
  249. * @since 2.1
  250. */
  251. @ConfigurationDsl
  252. public fun autoReconnectOnForceOffline() {
  253. autoReconnectOnForceOffline = true
  254. }
  255. ///////////////////////////////////////////////////////////////////////////
  256. // Device
  257. ///////////////////////////////////////////////////////////////////////////
  258. /**
  259. * 设备信息覆盖. 在没有手动指定时将会通过日志警告, 并使用随机设备信息.
  260. * @see fileBasedDeviceInfo 使用指定文件存储设备信息
  261. * @see randomDeviceInfo 使用随机设备信息
  262. */
  263. public var deviceInfo: ((Bot) -> DeviceInfo)? = deviceInfoStub // allows user to set `null` manually.
  264. /**
  265. * 使用随机设备信息.
  266. *
  267. * @see deviceInfo
  268. */
  269. @ConfigurationDsl
  270. public fun randomDeviceInfo() {
  271. deviceInfo = null
  272. }
  273. /**
  274. * 使用特定由 [DeviceInfo] 序列化产生的 JSON 的设备信息
  275. *
  276. * @see deviceInfo
  277. */
  278. @ConfigurationDsl
  279. public fun loadDeviceInfoJson(json: String) {
  280. deviceInfo = {
  281. this.json.decodeFromString(DeviceInfo.serializer(), json)
  282. }
  283. }
  284. /**
  285. * 使用文件存储设备信息.
  286. *
  287. * 此函数只在 JVM 和 Android 有效. 在其他平台将会抛出异常.
  288. * @param filepath 文件路径. 默认是相对于 [workingDir] 的文件 "device.json".
  289. * @see deviceInfo
  290. */
  291. @JvmOverloads
  292. @ConfigurationDsl
  293. public fun fileBasedDeviceInfo(filepath: String = "device.json") {
  294. deviceInfo = getFileBasedDeviceInfoSupplier { workingDir.resolve(filepath) }
  295. }
  296. ///////////////////////////////////////////////////////////////////////////
  297. // Logging
  298. ///////////////////////////////////////////////////////////////////////////
  299. /**
  300. * 日志记录器
  301. *
  302. * - 默认打印到标准输出, 通过 [MiraiLogger.create]
  303. * - 忽略所有日志: [noBotLog]
  304. * - 重定向到一个目录: `botLoggerSupplier = { DirectoryLogger("Bot ${it.id}") }`
  305. * - 重定向到一个文件: `botLoggerSupplier = { SingleFileLogger("Bot ${it.id}") }`
  306. *
  307. * @see MiraiLogger
  308. */
  309. public var botLoggerSupplier: ((Bot) -> MiraiLogger) = {
  310. MiraiLogger.Factory.create(Bot::class, "Bot ${it.id}")
  311. }
  312. /**
  313. * 网络层日志构造器
  314. *
  315. * - 默认打印到标准输出, 通过 [MiraiLogger.create]
  316. * - 忽略所有日志: [noNetworkLog]
  317. * - 重定向到一个目录: `networkLoggerSupplier = { DirectoryLogger("Net ${it.id}") }`
  318. * - 重定向到一个文件: `networkLoggerSupplier = { SingleFileLogger("Net ${it.id}") }`
  319. *
  320. * @see MiraiLogger
  321. */
  322. public var networkLoggerSupplier: ((Bot) -> MiraiLogger) = {
  323. MiraiLogger.Factory.create(Bot::class, "Net ${it.id}")
  324. }
  325. /**
  326. * 重定向 [网络日志][networkLoggerSupplier] 到指定目录. 若目录不存在将会自动创建 ([File.mkdirs])
  327. * 默认目录路径为 "$workingDir/logs/".
  328. * @see DirectoryLogger
  329. * @see redirectNetworkLogToDirectory
  330. */
  331. @JvmOverloads
  332. @ConfigurationDsl
  333. public fun redirectNetworkLogToDirectory(
  334. dir: File = File("logs"),
  335. retain: Long = 1.weeksToMillis,
  336. identity: (bot: Bot) -> String = { "Net ${it.id}" }
  337. ) {
  338. require(!dir.isFile) { "dir must not be a file" }
  339. networkLoggerSupplier = { DirectoryLogger(identity(it), workingDir.resolve(dir), retain) }
  340. }
  341. /**
  342. * 重定向 [网络日志][networkLoggerSupplier] 到指定文件. 默认文件路径为 "$workingDir/mirai.log".
  343. * 日志将会逐行追加到此文件. 若文件不存在将会自动创建 ([File.createNewFile])
  344. * @see SingleFileLogger
  345. * @see redirectNetworkLogToDirectory
  346. */
  347. @JvmOverloads
  348. @ConfigurationDsl
  349. public fun redirectNetworkLogToFile(
  350. file: File = File("mirai.log"),
  351. identity: (bot: Bot) -> String = { "Net ${it.id}" }
  352. ) {
  353. require(!file.isDirectory) { "file must not be a dir" }
  354. networkLoggerSupplier = { SingleFileLogger(identity(it), workingDir.resolve(file)) }
  355. }
  356. /**
  357. * 重定向 [Bot 日志][botLoggerSupplier] 到指定文件.
  358. * 日志将会逐行追加到此文件. 若文件不存在将会自动创建 ([File.createNewFile])
  359. * @see SingleFileLogger
  360. * @see redirectBotLogToDirectory
  361. */
  362. @JvmOverloads
  363. @ConfigurationDsl
  364. public fun redirectBotLogToFile(
  365. file: File = File("mirai.log"),
  366. identity: (bot: Bot) -> String = { "Net ${it.id}" }
  367. ) {
  368. require(!file.isDirectory) { "file must not be a dir" }
  369. botLoggerSupplier = { SingleFileLogger(identity(it), workingDir.resolve(file)) }
  370. }
  371. /**
  372. * 重定向 [Bot 日志][botLoggerSupplier] 到指定目录. 若目录不存在将会自动创建 ([File.mkdirs])
  373. * @see DirectoryLogger
  374. * @see redirectBotLogToFile
  375. */
  376. @JvmOverloads
  377. @ConfigurationDsl
  378. public fun redirectBotLogToDirectory(
  379. dir: File = File("logs"),
  380. retain: Long = 1.weeksToMillis,
  381. identity: (bot: Bot) -> String = { "Net ${it.id}" }
  382. ) {
  383. require(!dir.isFile) { "dir must not be a file" }
  384. botLoggerSupplier = { DirectoryLogger(identity(it), workingDir.resolve(dir), retain) }
  385. }
  386. /**
  387. * 不显示网络日志. 不推荐.
  388. * @see networkLoggerSupplier 更多日志处理方式
  389. */
  390. @ConfigurationDsl
  391. public fun noNetworkLog() {
  392. networkLoggerSupplier = { _ -> SilentLogger }
  393. }
  394. /**
  395. * 不显示 [Bot] 日志. 不推荐.
  396. * @see botLoggerSupplier 更多日志处理方式
  397. */
  398. @ConfigurationDsl
  399. public fun noBotLog() {
  400. botLoggerSupplier = { _ -> SilentLogger }
  401. }
  402. /**
  403. * 是否显示过于冗长的事件日志
  404. *
  405. * 默认为 `false`
  406. *
  407. * @since 2.8
  408. */
  409. public var isShowingVerboseEventLog: Boolean = false
  410. ///////////////////////////////////////////////////////////////////////////
  411. // Cache
  412. //////////////////////////////////////////////////////////////////////////
  413. /**
  414. * 缓存数据目录, 相对于 [workingDir].
  415. *
  416. * 缓存目录保存的内容均属于不稳定的 Mirai 内部数据, 请不要手动修改它们. 清空缓存不会影响功能. 只会导致一些操作如读取全部群列表要重新进行.
  417. * 默认启用的缓存可以加快登录过程.
  418. *
  419. * 注意: 这个目录只存储能在 [BotConfiguration] 配置的内容, 即包含:
  420. * - 联系人列表
  421. * - 登录服务器列表
  422. * - 资源服务秘钥
  423. *
  424. * 其他内容如通过 [InputStream] 发送图片时的缓存使用 [FileCacheStrategy], 默认使用系统临时文件且会在关闭时删除文件.
  425. *
  426. * @since 2.4
  427. */
  428. public var cacheDir: File = File("cache")
  429. /**
  430. * 联系人信息缓存配置. 将会保存在 [cacheDir] 中 `contacts` 目录
  431. * @since 2.4
  432. */
  433. public var contactListCache: ContactListCache = ContactListCache()
  434. /**
  435. * 联系人信息缓存配置
  436. * @see contactListCache
  437. * @see enableContactCache
  438. * @see disableContactCache
  439. * @since 2.4
  440. */
  441. public class ContactListCache {
  442. /**
  443. * 在有修改时自动保存间隔. 默认 60 秒. 在每次登录完成后有修改时都会立即保存一次.
  444. */
  445. public var saveIntervalMillis: Long = 60_000
  446. /**
  447. * 在有修改时自动保存间隔. 默认 60 秒. 在每次登录完成后有修改时都会立即保存一次.
  448. */
  449. @ExperimentalTime
  450. public inline var saveInterval: Duration
  451. @JvmSynthetic inline get() = Duration.milliseconds(saveIntervalMillis)
  452. @JvmSynthetic inline set(v) {
  453. saveIntervalMillis = v.inWholeMilliseconds
  454. }
  455. /**
  456. * 开启好友列表缓存.
  457. */
  458. public var friendListCacheEnabled: Boolean = false
  459. /**
  460. * 开启群成员列表缓存.
  461. */
  462. public var groupMemberListCacheEnabled: Boolean = false
  463. }
  464. /**
  465. * 配置 [ContactListCache]
  466. * ```
  467. * contactListCache {
  468. * saveIntervalMillis = 30_000
  469. * friendListCacheEnabled = true
  470. * }
  471. * ```
  472. * @since 2.4
  473. */
  474. @JvmSynthetic
  475. public inline fun contactListCache(action: ContactListCache.() -> Unit) {
  476. action.invoke(this.contactListCache)
  477. }
  478. /**
  479. * 禁用好友列表和群成员列表的缓存.
  480. * @since 2.4
  481. */
  482. @ConfigurationDsl
  483. public fun disableContactCache() {
  484. contactListCache.friendListCacheEnabled = false
  485. contactListCache.groupMemberListCacheEnabled = false
  486. }
  487. /**
  488. * 启用好友列表和群成员列表的缓存.
  489. * @since 2.4
  490. */
  491. @ConfigurationDsl
  492. public fun enableContactCache() {
  493. contactListCache.friendListCacheEnabled = true
  494. contactListCache.groupMemberListCacheEnabled = true
  495. }
  496. /**
  497. * 登录缓存.
  498. *
  499. * 开始后在密码登录成功时会保存秘钥等信息, 在下次启动时通过这些信息登录, 而不提交密码.
  500. * 可以减少验证码出现的频率.
  501. *
  502. * 秘钥信息会由密码加密保存. 如果秘钥过期, 则会进行普通密码登录.
  503. *
  504. * 默认 `true` (开启).
  505. *
  506. * @since 2.6
  507. */
  508. public var loginCacheEnabled: Boolean = true
  509. ///////////////////////////////////////////////////////////////////////////
  510. // Misc
  511. ///////////////////////////////////////////////////////////////////////////
  512. @Suppress("DuplicatedCode")
  513. public fun copy(): BotConfiguration {
  514. return BotConfiguration().also { new ->
  515. // To structural order
  516. new.workingDir = workingDir
  517. new.json = json
  518. new.parentCoroutineContext = parentCoroutineContext
  519. new.heartbeatPeriodMillis = heartbeatPeriodMillis
  520. new.heartbeatTimeoutMillis = heartbeatTimeoutMillis
  521. new.statHeartbeatPeriodMillis = statHeartbeatPeriodMillis
  522. new.heartbeatStrategy = heartbeatStrategy
  523. @Suppress("DEPRECATION")
  524. new.firstReconnectDelayMillis = firstReconnectDelayMillis
  525. @Suppress("DEPRECATION")
  526. new.reconnectPeriodMillis = reconnectPeriodMillis
  527. new.reconnectionRetryTimes = reconnectionRetryTimes
  528. new.autoReconnectOnForceOffline = autoReconnectOnForceOffline
  529. new.loginSolver = loginSolver
  530. new.protocol = protocol
  531. new.highwayUploadCoroutineCount = highwayUploadCoroutineCount
  532. new.deviceInfo = deviceInfo
  533. new.botLoggerSupplier = botLoggerSupplier
  534. new.networkLoggerSupplier = networkLoggerSupplier
  535. new.cacheDir = cacheDir
  536. new.contactListCache = contactListCache
  537. new.convertLineSeparator = convertLineSeparator
  538. new.isShowingVerboseEventLog = isShowingVerboseEventLog
  539. }
  540. }
  541. /**
  542. * 是否处理接受到的特殊换行符, 默认为 `true`
  543. *
  544. * - 若为 `true`, 会将收到的 `CRLF(\r\n)` 和 `CR(\r)` 替换为 `LF(\n)`
  545. * - 若为 `false`, 则不做处理
  546. *
  547. * @since 2.4
  548. */
  549. @get:JvmName("isConvertLineSeparator")
  550. public var convertLineSeparator: Boolean = true
  551. /** 标注一个配置 DSL 函数 */
  552. @Target(AnnotationTarget.FUNCTION)
  553. @DslMarker
  554. public annotation class ConfigurationDsl
  555. public companion object {
  556. /** 默认的配置实例. 可以进行修改 */
  557. @JvmStatic
  558. public val Default: BotConfiguration = BotConfiguration()
  559. }
  560. }
  561. /**
  562. * 构建一个 [BotConfiguration].
  563. *
  564. * @see BotConfiguration
  565. * @since 2.3
  566. */
  567. @JvmSynthetic
  568. public inline fun BotConfiguration(block: BotConfiguration.() -> Unit): BotConfiguration {
  569. return BotConfiguration().apply(block)
  570. }
  571. internal val deviceInfoStub: (Bot) -> DeviceInfo = {
  572. logger.warning("未指定设备信息, 已使用随机设备信息. 请查看 BotConfiguration.deviceInfo 以获取更多信息.")
  573. logger.warning("Device info isn't specified. Please refer to BotConfiguration.deviceInfo for more information")
  574. DeviceInfo.random()
  575. }
  576. private val logger by lazy {
  577. MiraiLogger.Factory.create(BotConfiguration::class)
  578. }