Suspension functions can be called only within coroutine body
-
我因为要把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,不知怎么解决?
-
必须在另一个协程里调用suspend function
-
@rainchan 那是怎么弄的?
-
runBlocking{}
-