Main.kt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. @file:Suppress("EXPERIMENTAL_UNSIGNED_LITERALS", "EXPERIMENTAL_API_USAGE")
  2. package demo.gentleman
  3. import kotlinx.coroutines.Dispatchers.IO
  4. import kotlinx.coroutines.GlobalScope
  5. import kotlinx.coroutines.delay
  6. import kotlinx.coroutines.launch
  7. import kotlinx.coroutines.withContext
  8. import net.mamoe.mirai.BotAccount
  9. import net.mamoe.mirai.alsoLogin
  10. import net.mamoe.mirai.contact.MemberPermission
  11. import net.mamoe.mirai.event.Subscribable
  12. import net.mamoe.mirai.event.events.ReceiveFriendAddRequestEvent
  13. import net.mamoe.mirai.event.subscribeAlways
  14. import net.mamoe.mirai.event.subscribeGroupMessages
  15. import net.mamoe.mirai.event.subscribeMessages
  16. import net.mamoe.mirai.message.FriendMessage
  17. import net.mamoe.mirai.message.GroupMessage
  18. import net.mamoe.mirai.message.data.At
  19. import net.mamoe.mirai.message.data.Image
  20. import net.mamoe.mirai.message.data.buildXMLMessage
  21. import net.mamoe.mirai.message.data.getValue
  22. import net.mamoe.mirai.message.sendAsImageTo
  23. import net.mamoe.mirai.timpc.TIMPC
  24. import java.io.File
  25. import java.util.*
  26. import javax.swing.filechooser.FileSystemView
  27. import kotlin.random.Random
  28. private fun readTestAccount(): BotAccount? {
  29. val file = File("testAccount.txt")
  30. if (!file.exists() || !file.canRead()) {
  31. return null
  32. }
  33. val lines = file.readLines()
  34. return try {
  35. BotAccount(lines[0].toLong(), lines[1])
  36. } catch (e: Exception) {
  37. null
  38. }
  39. }
  40. @Suppress("UNUSED_VARIABLE")
  41. suspend fun main() {
  42. val bot = TIMPC.Bot(
  43. readTestAccount() ?: BotAccount(
  44. id = 913366033,
  45. password = "a18260132383"
  46. )
  47. ).alsoLogin()
  48. /**
  49. * 监听所有事件
  50. */
  51. subscribeAlways<Subscribable> {
  52. //bot.logger.verbose("收到了一个事件: ${it::class.simpleName}")
  53. }
  54. subscribeAlways<ReceiveFriendAddRequestEvent> {
  55. it.approve()
  56. }
  57. bot.subscribeGroupMessages {
  58. "群资料" reply {
  59. group.updateGroupInfo().toString().reply()
  60. }
  61. startsWith("mt2months") {
  62. val at: At by message
  63. at.member().mute(1)
  64. }
  65. startsWith("mute") {
  66. val at: At by message
  67. at.member().mute(30)
  68. }
  69. startsWith("unmute") {
  70. val at: At by message
  71. at.member().unmute()
  72. }
  73. }
  74. bot.subscribeMessages {
  75. case("at me") { At(sender).reply() }
  76. "你好" reply "你好!"
  77. startsWith("profile", removePrefix = true) {
  78. val account = it.trim()
  79. if (account.isNotEmpty()) {
  80. account.toLong().qq()
  81. } else {
  82. sender
  83. }.queryProfile().toString().reply()
  84. }
  85. "grouplist" reply {
  86. //"https://ssl.ptlogin2.qq.com/jump?pt_clientver=5509&pt_src=1&keyindex=9&clientuin=" + bot.qqAccount + "&clientkey=" + com.tick_tock.pctim.utils.Util.byte2HexString(
  87. // user.txprotocol.serviceTicketHttp
  88. //).replace(" ", "").toString() + "&u1=http%3A%2F%2Fqun.qq.com%2Fmember.html%23gid%3D168209441"
  89. }
  90. "xml" reply {
  91. val template =
  92. """
  93. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
  94. <msg templateID='1' serviceID='1' action='plugin' actionData='ACTION_LINK' brief='BRIEF' flag='3' url=''>
  95. <item bg='0' layout='4'>
  96. <picture cover='TITLE_PICTURE_LINK'/>
  97. <title size='30' color='#fc7299'>TITLE</title>
  98. </item>
  99. <item>
  100. <summary color='#fc7299'>CONTENT</summary>
  101. <picture cover='CONTENT_PICTURE_LINK'/>
  102. </item>
  103. <source name='ExHentai' icon='ExHentai'/>
  104. </msg>
  105. """.trimIndent()
  106. buildXMLMessage {
  107. item {
  108. picture("http://img.mamoe.net/2019/12/03/be35ccb489ecb.jpg")
  109. title("This is title")
  110. }
  111. item {
  112. summary("This is a summary colored #66CCFF", color = "#66CCFF")
  113. picture("http://img.mamoe.net/2019/12/03/74c8614c4a161.jpg")
  114. }
  115. source("Mirai", "http://img.mamoe.net/2019/12/03/02eea0f6e826a.png")
  116. }.reply()
  117. }
  118. has<Image> {
  119. if (this is FriendMessage || (this is GroupMessage && this.permission == MemberPermission.ADMINISTRATOR)) withContext(IO) {
  120. val image: Image by message
  121. // 等同于 val image = message[Image]
  122. try {
  123. image.downloadTo(newTestTempFile(suffix = ".png").also { reply("Temp file: ${it.absolutePath}") })
  124. reply(image.id.value + " downloaded")
  125. } catch (e: Exception) {
  126. e.printStackTrace()
  127. reply(e.message ?: e::class.java.simpleName)
  128. }
  129. }
  130. }
  131. startsWith("上传图片", removePrefix = true) handler@{
  132. val file = File(FileSystemView.getFileSystemView().homeDirectory, it)
  133. if (!file.exists()) {
  134. reply("图片不存在")
  135. return@handler
  136. }
  137. reply("sent")
  138. file.sendAsImageTo(subject)
  139. }
  140. startsWith("随机图片", removePrefix = true) {
  141. repeat(it.toIntOrNull() ?: 1) {
  142. GlobalScope.launch {
  143. delay(Random.Default.nextLong(100, 1000))
  144. Gentlemen.provide(subject).receive().image.await().send()
  145. }
  146. }
  147. }
  148. startsWith("添加好友", removePrefix = true) {
  149. reply(bot.addFriend(it.toLong()).toString())
  150. }
  151. }
  152. bot.network.awaitDisconnection()//等到直到断开连接
  153. }
  154. private fun newTestTempFile(filename: String = "${UUID.randomUUID()}", suffix: String = ".tmp"): File =
  155. File(System.getProperty("user.dir"), filename + suffix).also { it.createNewFile(); it.deleteOnExit() }