project-mirai / mirai-demos

mirai-console的一些插件与教学
GNU Affero General Public License v3.0
83 stars 75 forks source link

如何才能监听到收到消息的事件 #19

Closed CafeBabe202 closed 4 years ago

CafeBabe202 commented 4 years ago

我测试了那个Demo的代码,已经成功地登录了,也可以发送消息,到时我发现并不能监听到收到消息的事件 bot.login(); bot.getFriends().forEach(friend -> System.out.println(friend.getId() + ":" + friend.getNick())); Events.registerEvents(bot, new SimpleListenerHost() { @EventHandler public ListeningStatus onGroupMessage(GroupMessageEvent event) { System.out.println("你收到了新的群消息!"); return ListeningStatus.LISTENING; } @Override public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) { throw new RuntimeException("在事件处理中发生异常", exception); } });

这个是代码

Him188 commented 4 years ago

这个是 mirai-core 的问题,正在尝试解决

CafeBabe202 commented 4 years ago

好的,谢谢,请问使用以前的版本有这个问题吗?

Him188 commented 4 years ago

可能可以在1.0-EA2或者1.0-EA中正常

CafeBabe202 commented 4 years ago

我在另一个Issues中听说是匿名内部类的问题,好像有人已经调出来了,但是我却发生了新的异常 代码如下:

public static class CustomEventListener extends SimpleListenerHost {
        @EventHandler
        public ListeningStatus onFriendMsg(FriendMessageEvent event) {
            MessageReceipt<Contact> receipt = event.getSender().sendMessage("测试测试");
            receipt.recallIn(10000);
            return ListeningStatus.LISTENING;
        }

        @Override
        public void handleException(@NotNull CoroutineContext context, @javax.validation.constraints.NotNull Throwable exception) {
            super.handleException(context, exception);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final Bot bot = BotFactoryJvm.newBot(qq, password, new BotConfiguration() {
            {
                fileBasedDeviceInfo("deviceInfo.json");
            }
        });
        bot.login();
        Events.registerEvents(bot, new CustomEventListener());
        bot.join();
    }

异常信息:

Exception in thread "main" java.lang.IllegalArgumentException: Kotlin event handlers cannot have nullable parameter type.
    at net.mamoe.mirai.event.Events__JvmMethodListenersKt.registerEvent$Events__JvmMethodListenersKt(JvmMethodListeners.kt:298)
    at net.mamoe.mirai.event.Events__JvmMethodListenersKt.registerEvents(JvmMethodListeners.kt:233)
    at net.mamoe.mirai.event.Events.registerEvents(JvmMethodListeners.kt:1)
    at net.mamoe.mirai.event.Events__JvmMethodListenersKt.registerEvents$default(JvmMethodListeners.kt:230)
    at net.mamoe.mirai.event.Events.registerEvents$default(JvmMethodListeners.kt:1)
    at net.mamoe.mirai.event.Events.registerEvents(JvmMethodListeners.kt)
    at Test.main(Test.java:34)

6BPXI}Y(L8ZHT YU0UU@W91

Him188 commented 4 years ago

@HappyOnion801 请问这个问题发生时使用的 mirai 版本是多少?

CafeBabe202 commented 4 years ago

mirai-core-1.1.-EA2

Him188 commented 4 years ago

请在 mirai core 已有的issue中继续这类问题的讨论