MiraiForum

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

    Solved 如何实现监听器在一定时间内或接收指令一定次数后停止监听

    开发交流
    3
    5
    148
    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.
    • 珂朵莉-诺塔-瑟尼欧里斯
      珂朵莉-诺塔-瑟尼欧里斯 last edited by

      问题背景:想在群内实现2-5的小游戏,开局需要匹配参与人。群友需要对bot私聊(非好友为群临时会话消息,好友为好友消息?)发送指令参与。
      开始时,一个人@bot表示开始,这时开始监听,群友私聊发送指令参与,参与人达到5人或者10秒后停止监听。
      大佬们,这该如何实现,欧内该

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

        使用:
        EventChannel.subscribe:监听事件并自行觉得何时停止
        EventChannel.subscribeAlways:一直监听事件
        EventChannel.subscribeOnce:只监听一次事件

        参考https://docs.mirai.mamoe.net/Events.html#限制作用域

        珂朵莉-诺塔-瑟尼欧里斯 1 Reply Last reply Reply Quote 0
        • 珂朵莉-诺塔-瑟尼欧里斯
          珂朵莉-诺塔-瑟尼欧里斯 @RainChan last edited by

          @rainchan 行吧,我去试试看能不能解决

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

            以下是几种结束监听的方式

            var counter = 0
            val listener = [EventChannel].subscribe<***Event> {
                if () {
                    counter ++
                    return if (counter > 5) ListeningStatus.STOPPED else ListeningStatus.LISTENING
                }
                return ListeningStatus.LISTENING
            }
            delay(10000) // 10s
            listener.complete() // Cancel listener
            
            珂朵莉-诺塔-瑟尼欧里斯 1 Reply Last reply Reply Quote 0
            • 珂朵莉-诺塔-瑟尼欧里斯
              珂朵莉-诺塔-瑟尼欧里斯 @Karlatemp last edited by

              @karlatemp 谢谢大佬!

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