MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

    Suspension functions can be called only within coroutine body

    开发交流
    4
    5
    342
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 2
      250king last edited by

      我因为要把bot与RabbmitMQ结合,就尝试写一个deom——接受到消息,然后转发到群

      package love.hana.bot.qq.subscribe.twitter
      
      import com.rabbitmq.client.AMQP
      import com.rabbitmq.client.ConnectionFactory
      import com.rabbitmq.client.DefaultConsumer
      import com.rabbitmq.client.Envelope
      import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
      import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
      import net.mamoe.mirai.event.GlobalEventChannel
      import net.mamoe.mirai.event.events.BotOnlineEvent
      import net.mamoe.mirai.message.data.PlainText
      import java.io.IOException
      
      
      object Plugin: KotlinPlugin(JvmPluginDescription("love.hana.bot.qq.subscribe.twitter", "1.0.0", "Twitter订阅推送")) {
          override fun onEnable() {
              val auth = ConnectionFactory()
              auth.host = "127.0.0.1"
              auth.username = ""
              auth.password = ""
              val mq = auth.newConnection()
              val channel = mq.createChannel()
              channel.queueDeclare("love.hana.bot.qq.subscribe.twitter", true, false, true, null)
              channel.queueBind("love.hana.bot.qq.subscribe.twitter", "amq.topic", "subscribe.twitter")
              GlobalEventChannel.subscribeOnce<BotOnlineEvent> {event ->
                  channel.basicConsume("love.hana.bot.qq.subscribe.twitter", false, object: DefaultConsumer(channel) {
                      @Throws(IOException::class)
                      override fun handleDelivery(consumerTag: String?, envelope: Envelope?, properties: AMQP.BasicProperties?, body: ByteArray?) {
                          if (body != null) {
                              bot.getGroup(891766260L)?.sendMessage(PlainText(String(body)))   //提示这边出现问题
                          }
                      }
                  })
              }
          }
      }
      
      

      但打包成插件后就报错:

      e: C:\path\to\user\Documents\project\qq-bot\plugin\subscribe-twitter\src\main\kotlin\love\hana\bot\qq\subscribe\twitter\Plugin.kt: (30, 51): Suspension functions can be called only within coroutine body
      

      刚开始用Kotlin,不知怎么解决?

      1 Reply Last reply Reply Quote 0
      • RainChan
        RainChan 摸鱼 last edited by

        必须在另一个协程里调用suspend function

        2 1 Reply Last reply Reply Quote 0
        • 2
          250king @RainChan last edited by

          @rainchan 那是怎么弄的?

          1 Reply Last reply Reply Quote 0
          • Nambers
            Nambers 摸鱼 last edited by

            runBlocking{}

            1 Reply Last reply Reply Quote 0
            • Karlatemp
              Karlatemp last edited by

              https://www.kotlincn.net/docs/reference/coroutines/coroutines-guide.html

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact