BotFactory.kt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2019-2022 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/dev/LICENSE
  8. */
  9. @file:Suppress("unused", "NOTHING_TO_INLINE")
  10. package net.mamoe.mirai
  11. import net.mamoe.mirai.auth.BotAuthorization
  12. import net.mamoe.mirai.utils.BotConfiguration
  13. import kotlin.jvm.JvmSynthetic
  14. /**
  15. * 构造 [Bot] 的工厂. 这是 [Bot] 唯一的构造方式.
  16. *
  17. * @see IMirai.BotFactory
  18. */
  19. public interface BotFactory {
  20. /**
  21. * 相当于 Kotlin lambda `BotConfiguration.() -> Unit` 和 Java `Consumer<BotConfiguration>`
  22. *
  23. * @see newBot
  24. */
  25. public fun interface BotConfigurationLambda {
  26. public operator fun BotConfiguration.invoke()
  27. }
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Plain Password
  30. ///////////////////////////////////////////////////////////////////////////
  31. /**
  32. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  33. */
  34. public fun newBot(qq: Long, password: String, configuration: BotConfiguration): Bot
  35. /**
  36. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  37. *
  38. * Kotlin:
  39. * ```
  40. * newBot(123, "") {
  41. * // this: BotConfiguration
  42. * fileBasedDeviceInfo()
  43. * }
  44. * ```
  45. *
  46. * Java:
  47. * ```java
  48. * newBot(123, "", configuration -> {
  49. * configuration.fileBasedDeviceInfo()
  50. * })
  51. * ```
  52. */
  53. public fun newBot(
  54. qq: Long,
  55. password: String,
  56. configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
  57. ): Bot = newBot(qq, password, configuration.run { BotConfiguration().apply { invoke() } })
  58. /**
  59. * 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
  60. */
  61. public fun newBot(qq: Long, password: String): Bot = newBot(qq, password, BotConfiguration.Default)
  62. ///////////////////////////////////////////////////////////////////////////
  63. // MD5 Password
  64. ///////////////////////////////////////////////////////////////////////////
  65. /**
  66. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  67. *
  68. * @param passwordMd5 16 bytes
  69. */
  70. public fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot
  71. /**
  72. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  73. *
  74. * Kotlin:
  75. * ```
  76. * newBot(123, password) {
  77. * // this: BotConfiguration
  78. * fileBasedDeviceInfo()
  79. * }
  80. * ```
  81. *
  82. * Java:
  83. * ```java
  84. * newBot(123, password, configuration -> {
  85. * configuration.fileBasedDeviceInfo()
  86. * })
  87. * ```
  88. *
  89. * @param passwordMd5 16 bytes
  90. */
  91. public fun newBot(
  92. qq: Long,
  93. passwordMd5: ByteArray,
  94. configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
  95. ): Bot = newBot(qq, passwordMd5, configuration.run { BotConfiguration().apply { invoke() } })
  96. /**
  97. * 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
  98. *
  99. * @param passwordMd5 16 bytes
  100. */
  101. public fun newBot(qq: Long, passwordMd5: ByteArray): Bot = newBot(qq, passwordMd5, BotConfiguration.Default)
  102. ///////////////////////////////////////////////////////////////////////////
  103. // BotAuthorization
  104. ///////////////////////////////////////////////////////////////////////////
  105. /**
  106. * 使用 [默认配置][BotConfiguration.Default] 构造 [Bot] 实例
  107. *
  108. * @since 2.15
  109. */
  110. public fun newBot(qq: Long, authorization: BotAuthorization): Bot =
  111. newBot(qq, authorization, BotConfiguration.Default)
  112. /**
  113. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  114. *
  115. * @since 2.15
  116. */
  117. public fun newBot(qq: Long, authorization: BotAuthorization, configuration: BotConfiguration): Bot
  118. /**
  119. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  120. *
  121. * Kotlin:
  122. * ```
  123. * newBot(123, password) {
  124. * // this: BotConfiguration
  125. * fileBasedDeviceInfo()
  126. * }
  127. * ```
  128. *
  129. * Java:
  130. * ```java
  131. * newBot(123, password, configuration -> {
  132. * configuration.fileBasedDeviceInfo()
  133. * })
  134. * ```
  135. *
  136. * @since 2.15
  137. */
  138. public fun newBot(
  139. qq: Long,
  140. authorization: BotAuthorization,
  141. configuration: BotConfigurationLambda /* = BotConfiguration.() -> Unit */
  142. ): Bot = newBot(qq, authorization, configuration.run { BotConfiguration().apply { invoke() } })
  143. public companion object INSTANCE : BotFactory {
  144. override fun newBot(qq: Long, password: String, configuration: BotConfiguration): Bot {
  145. return Mirai.BotFactory.newBot(qq, password, configuration)
  146. }
  147. override fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot {
  148. return Mirai.BotFactory.newBot(qq, passwordMd5, configuration)
  149. }
  150. /**
  151. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  152. *
  153. * ```
  154. * newBot(123, "") {
  155. * // this: BotConfiguration
  156. * fileBasedDeviceInfo()
  157. * }
  158. * ```
  159. *
  160. * @since 2.7
  161. */
  162. @JvmSynthetic
  163. public inline fun newBot(
  164. qq: Long,
  165. password: String,
  166. configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
  167. ): Bot = newBot(qq, password, BotConfiguration().apply(configuration))
  168. // implementation notes: this is inline for `inheritCoroutineContext()`
  169. // see https://github.com/mamoe/mirai/commit/0dbb448cad1ed4773d48ccb8c0b497841bc9fa4c#r50249446
  170. /**
  171. * 使用指定的 [配置][configuration] 构造 [Bot] 实例
  172. *
  173. * ```
  174. * newBot(123, password) {
  175. * // this: BotConfiguration
  176. * fileBasedDeviceInfo()
  177. * }
  178. * ```
  179. *
  180. * @since 2.7
  181. */
  182. @JvmSynthetic
  183. public inline fun newBot(
  184. qq: Long,
  185. passwordMd5: ByteArray,
  186. configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
  187. ): Bot = newBot(qq, passwordMd5, BotConfiguration().apply(configuration))
  188. override fun newBot(qq: Long, authorization: BotAuthorization, configuration: BotConfiguration): Bot {
  189. return Mirai.BotFactory.newBot(qq, authorization, configuration)
  190. }
  191. }
  192. }