BotFactory.kt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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", "NOTHING_TO_INLINE")
  10. package net.mamoe.mirai
  11. import net.mamoe.mirai.utils.BotConfiguration
  12. /**
  13. * 构造 [Bot] 的工厂. 这是 [Bot] 唯一的构造方式.
  14. *
  15. * @see IMirai.BotFactory
  16. */
  17. public interface BotFactory {
  18. /**
  19. * 相当于 Kotlin lambda `BotConfiguration.() -> Unit` 和 Java `Consumer<BotConfiguration>`
  20. *
  21. * @see newBot
  22. */
  23. public fun interface BotConfigurationLambda {
  24. public operator fun BotConfiguration.invoke()
  25. }
  26. ///////////////////////////////////////////////////////////////////////////
  27. // Plain Password
  28. ///////////////////////////////////////////////////////////////////////////
  29. /**
  30. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  31. */
  32. public fun newBot(qq: Long, password: String, configuration: BotConfiguration): Bot
  33. /**
  34. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  35. *
  36. * Kotlin:
  37. * ```
  38. * newBot(123, "") {
  39. * // this: BotConfiguration
  40. * fileBasedDeviceInfo()
  41. * }
  42. * ```
  43. *
  44. * Java:
  45. * ```java
  46. * newBot(123, "", configuration -> {
  47. * configuration.fileBasedDeviceInfo()
  48. * })
  49. * ```
  50. */
  51. public fun newBot(
  52. qq: Long,
  53. password: String,
  54. configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
  55. ): Bot = newBot(qq, password, configuration.run { BotConfiguration().apply { invoke() } })
  56. /**
  57. * 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
  58. */
  59. public fun newBot(qq: Long, password: String): Bot = newBot(qq, password, BotConfiguration.Default)
  60. ///////////////////////////////////////////////////////////////////////////
  61. // MD5 Password
  62. ///////////////////////////////////////////////////////////////////////////
  63. /**
  64. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  65. *
  66. * @param passwordMd5 16 bytes
  67. */
  68. public fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot
  69. /**
  70. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  71. *
  72. * Kotlin:
  73. * ```
  74. * newBot(123, password) {
  75. * // this: BotConfiguration
  76. * fileBasedDeviceInfo()
  77. * }
  78. * ```
  79. *
  80. * Java:
  81. * ```java
  82. * newBot(123, password, configuration -> {
  83. * configuration.fileBasedDeviceInfo()
  84. * })
  85. * ```
  86. *
  87. * @param passwordMd5 16 bytes
  88. */
  89. public fun newBot(
  90. qq: Long,
  91. passwordMd5: ByteArray,
  92. configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
  93. ): Bot = newBot(qq, passwordMd5, configuration.run { BotConfiguration().apply { invoke() } })
  94. /**
  95. * 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
  96. *
  97. * @param passwordMd5 16 bytes
  98. */
  99. public fun newBot(qq: Long, passwordMd5: ByteArray): Bot = newBot(qq, passwordMd5, BotConfiguration.Default)
  100. public companion object INSTANCE : BotFactory {
  101. override fun newBot(qq: Long, password: String, configuration: BotConfiguration): Bot {
  102. return Mirai.BotFactory.newBot(qq, password, configuration)
  103. }
  104. override fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot {
  105. return Mirai.BotFactory.newBot(qq, passwordMd5, configuration)
  106. }
  107. /**
  108. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  109. *
  110. * ```
  111. * newBot(123, "") {
  112. * // this: BotConfiguration
  113. * fileBasedDeviceInfo()
  114. * }
  115. * ```
  116. *
  117. * @since 2.7
  118. */
  119. @JvmSynthetic
  120. public inline fun newBot(
  121. qq: Long,
  122. password: String,
  123. configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
  124. ): Bot = newBot(qq, password, configuration.run { BotConfiguration().apply(configuration) })
  125. // implementation notes: this is inline for `inheritCoroutineContext()`
  126. // see https://github.com/mamoe/mirai/commit/0dbb448cad1ed4773d48ccb8c0b497841bc9fa4c#r50249446
  127. /**
  128. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  129. *
  130. * ```
  131. * newBot(123, password) {
  132. * // this: BotConfiguration
  133. * fileBasedDeviceInfo()
  134. * }
  135. * ```
  136. *
  137. * @since 2.7
  138. */
  139. @JvmSynthetic
  140. public inline fun newBot(
  141. qq: Long,
  142. passwordMd5: ByteArray,
  143. configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
  144. ): Bot = newBot(qq, passwordMd5, configuration.run { BotConfiguration().apply(configuration) })
  145. }
  146. }