Search in sources :

Example 1 with BotConfiguration

use of net.mamoe.mirai.utils.BotConfiguration in project AngelinaFrame by Strelizia02.

the class MiraiFrameUtil method startMirai.

/**
 * Mirai框架的启动方法,一次性启动Mirai并开启消息监听
 */
public void startMirai() {
    // 判断一下qq数量和密码数量是否匹配
    if (qqList.length == 0 || qqList.length != pwList.length) {
        throw new AngelinaException("当前配置文件中账号密码配置有误,请审视配置文件");
    }
    for (int i = 0; i < qqList.length; i++) {
        // 循环登录所有配置的qq账号,如果有需要滑块验证的,需要单独解决
        Bot bot = BotFactory.INSTANCE.newBot(Long.parseLong(qqList[i]), pwList[i], new BotConfiguration() {

            {
                setProtocol(MiraiProtocol.IPAD);
                setDeviceInfo(bot -> new DeviceInfo("Huawei.856832.001".getBytes(StandardCharsets.UTF_8), "nova75g".getBytes(StandardCharsets.UTF_8), "JEF-AN20".getBytes(StandardCharsets.UTF_8), "Huawei Kirin 985".getBytes(StandardCharsets.UTF_8), "Huawei".getBytes(StandardCharsets.UTF_8), "Nova 7".getBytes(StandardCharsets.UTF_8), "HarmonyOS 2.0".getBytes(StandardCharsets.UTF_8), "Huawei/Nova/nova:7/MIRAI.200122.001/2736748:user/release-keys".getBytes(StandardCharsets.UTF_8), "1BBBCCA8-0B4A-2EFC-BE95-E732C84DA5F0".getBytes(StandardCharsets.UTF_8), "HarmonyOS version 2.0.0.221(C00E208R6P8)".getBytes(StandardCharsets.UTF_8), "unknown".getBytes(StandardCharsets.UTF_8), new DeviceInfo.Version(), "T-Mobile".getBytes(StandardCharsets.UTF_8), "HarmonyOS".getBytes(StandardCharsets.UTF_8), "02:00:00:00:00:00".getBytes(StandardCharsets.UTF_8), "02:00:00:00:00:00".getBytes(StandardCharsets.UTF_8), "Strelitzia".getBytes(StandardCharsets.UTF_8), "6e096dd53aa9062c".getBytes(StandardCharsets.UTF_8), "342086728277870", "wifi".getBytes(StandardCharsets.UTF_8)));
            }
        });
        bot.login();
    }
    reBuildBotGroupMap();
    // 监听全部qq账号所接受的消息
    GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, event -> {
        if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
            MessageInfo messageInfo = new MessageInfo(event, botNames);
            try {
                groupChatController.receive(messageInfo);
            } catch (InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    });
    // 某个Bot被踢出群或退群
    GlobalEventChannel.INSTANCE.subscribeAlways(BotLeaveEvent.class, event -> {
        reBuildBotGroupMap();
    });
    // 某个Bot加入了一个新群
    GlobalEventChannel.INSTANCE.subscribeAlways(BotJoinGroupEvent.class, event -> {
        messageIdMap.put(event.getGroupId(), event.getBot().getId());
        activityMapper.getEventMessage();
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setEvent(EventEnum.MemberJoinEvent);
        messageInfo.setLoginQq(event.getBot().getId());
        messageInfo.setGroupId(event.getGroup().getId());
        try {
            eventsController.receive(messageInfo);
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    });
    // 群撤回消息
    GlobalEventChannel.INSTANCE.subscribeAlways(MessageRecallEvent.GroupRecall.class, event -> {
        if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
            activityMapper.getEventMessage();
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setEvent(EventEnum.GroupRecall);
            messageInfo.setLoginQq(event.getBot().getId());
            messageInfo.setGroupId(event.getGroup().getId());
            messageInfo.setQq(event.getAuthorId());
            try {
                eventsController.receive(messageInfo);
            } catch (InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    });
    // 戳一戳
    GlobalEventChannel.INSTANCE.subscribeAlways(NudgeEvent.class, event -> {
        Contact subject = event.getSubject();
        if (subject instanceof Group) {
            if (messageIdMap.get(subject.getId()) == event.getBot().getId() && event.getTarget() instanceof Bot) {
                activityMapper.getEventMessage();
                MessageInfo messageInfo = new MessageInfo();
                messageInfo.setEvent(EventEnum.NudgeEvent);
                messageInfo.setLoginQq(event.getBot().getId());
                messageInfo.setGroupId(subject.getId());
                messageInfo.setQq(event.getFrom().getId());
                try {
                    eventsController.receive(messageInfo);
                } catch (InvocationTargetException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    // 某人进群
    GlobalEventChannel.INSTANCE.subscribeAlways(MemberJoinEvent.class, event -> {
        if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
            activityMapper.getEventMessage();
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setEvent(EventEnum.MemberJoinEvent);
            messageInfo.setLoginQq(event.getBot().getId());
            messageInfo.setGroupId(event.getGroup().getId());
            messageInfo.setQq(event.getMember().getId());
            messageInfo.setName(event.getMember().getNick());
            try {
                eventsController.receive(messageInfo);
            } catch (InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    });
    // 某人退群
    GlobalEventChannel.INSTANCE.subscribeAlways(MemberLeaveEvent.class, event -> {
        if (messageIdMap.get(event.getGroup().getId()) == event.getBot().getId()) {
            activityMapper.getEventMessage();
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setEvent(EventEnum.MemberLeaveEvent);
            messageInfo.setLoginQq(event.getBot().getId());
            messageInfo.setGroupId(event.getGroup().getId());
            messageInfo.setQq(event.getMember().getId());
            messageInfo.setName(event.getMember().getNick());
            try {
                eventsController.receive(messageInfo);
            } catch (InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    });
    // 账号掉线
    GlobalEventChannel.INSTANCE.subscribeAlways(BotOfflineEvent.class, event -> {
        reBuildBotGroupMap();
    });
    // 账号掉线
    GlobalEventChannel.INSTANCE.subscribeAlways(BotMuteEvent.class, event -> {
        if (event.component1() > 60 * 60 * 24) {
            event.getGroup().quit();
        }
    });
    // 账号上线
    GlobalEventChannel.INSTANCE.subscribeAlways(BotOnlineEvent.class, event -> {
        reBuildBotGroupMap();
    });
    // 好友消息
    GlobalEventChannel.INSTANCE.subscribeAlways(FriendMessageEvent.class, event -> {
        MessageInfo messageInfo = new MessageInfo(event, botNames);
        activityMapper.getFriendMessage();
        try {
            friendChatController.receive(messageInfo);
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    });
}
Also used : DeviceInfo(net.mamoe.mirai.utils.DeviceInfo) GroupChatController(top.angelinaBot.controller.GroupChatController) GlobalEventChannel(net.mamoe.mirai.event.GlobalEventChannel) Bot(net.mamoe.mirai.Bot) Group(net.mamoe.mirai.contact.Group) Autowired(org.springframework.beans.factory.annotation.Autowired) ActivityMapper(top.angelinaBot.dao.ActivityMapper) HashMap(java.util.HashMap) FriendChatController(top.angelinaBot.controller.FriendChatController) MessageInfo(top.angelinaBot.model.MessageInfo) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) Value(org.springframework.beans.factory.annotation.Value) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) BotFactory(net.mamoe.mirai.BotFactory) net.mamoe.mirai.event.events(net.mamoe.mirai.event.events) Map(java.util.Map) AngelinaException(top.angelinaBot.Exception.AngelinaException) EventsController(top.angelinaBot.controller.EventsController) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) Contact(net.mamoe.mirai.contact.Contact) EventEnum(top.angelinaBot.model.EventEnum) AngelinaException(top.angelinaBot.Exception.AngelinaException) Group(net.mamoe.mirai.contact.Group) Bot(net.mamoe.mirai.Bot) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessageInfo(top.angelinaBot.model.MessageInfo) Contact(net.mamoe.mirai.contact.Contact) DeviceInfo(net.mamoe.mirai.utils.DeviceInfo) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration)

Example 2 with BotConfiguration

use of net.mamoe.mirai.utils.BotConfiguration in project su-sunday-cloud by illeagalName.

the class QqBotConfig method bot.

@Bean
@ConditionalOnProperty(prefix = "qq.config", name = "startStatus", havingValue = "1")
public Bot bot(QqProperties qqProperties) {
    Bot bot = BotFactory.INSTANCE.newBot(Long.parseLong(qqProperties.getAccount()), qqProperties.getPassword(), new BotConfiguration() {

        {
            setWorkingDir(new File(qqProperties.getDirectory()));
            // 使用 device.json 存储设备信息
            fileBasedDeviceInfo("device.json");
            // 切换协议
            setProtocol(MiraiProtocol.ANDROID_PHONE);
            redirectNetworkLogToFile();
            redirectNetworkLogToDirectory();
        }
    });
    bot.login();
    // 设置https协议,解决SSL peer shut down incorrectly的异常
    System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
    log.info("======注册监听======");
    GlobalEventChannel.INSTANCE.registerListenerHost(eventListener);
    return bot;
}
Also used : Bot(net.mamoe.mirai.Bot) File(java.io.File) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) Bean(org.springframework.context.annotation.Bean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 3 with BotConfiguration

use of net.mamoe.mirai.utils.BotConfiguration in project FocessQQ by MidCoard.

the class SimpleBotManager method login.

@Override
public boolean login(Bot b) throws BotLoginException {
    if (!b.isOnline() && b instanceof SimpleBot) {
        long id = b.getId();
        BotConfiguration configuration = BotConfiguration.getDefault();
        configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
        File cache = new File("devices/" + id + "/cache");
        if (!cache.exists())
            if (!cache.mkdirs())
                throw new BotLoginException(id, FocessQQ.getLangConfig().get("fatal-create-cache-dir-failed"));
        configuration.fileBasedDeviceInfo("devices/" + id + "/device.json");
        configuration.setCacheDir(cache);
        configuration.setLoginSolver(new LoginSolver() {

            @Nullable
            @Override
            public Object onSolvePicCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull byte[] bytes, @NotNull Continuation<? super String> continuation) {
                try {
                    FileImageOutputStream outputStream = new FileImageOutputStream(new File("captcha.jpg"));
                    outputStream.write(bytes);
                    outputStream.close();
                } catch (IOException e) {
                    FocessQQ.getLogger().thrLang("exception-load-captcha-picture", e);
                }
                FocessQQ.getLogger().infoLang("input-captcha-code");
                try {
                    return IOHandler.getConsoleIoHandler().input();
                } catch (InputTimeoutException e) {
                    return null;
                }
            }

            @Nullable
            @Override
            public Object onSolveSliderCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
                FocessQQ.getLogger().info(s);
                try {
                    IOHandler.getConsoleIoHandler().input();
                } catch (InputTimeoutException ignored) {
                }
                return null;
            }

            @Nullable
            @Override
            public Object onSolveUnsafeDeviceLoginVerify(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
                FocessQQ.getLogger().info(s);
                try {
                    IOHandler.getConsoleIoHandler().input();
                } catch (InputTimeoutException ignored) {
                }
                return null;
            }
        });
        net.mamoe.mirai.Bot bot;
        try {
            bot = BotFactory.INSTANCE.newBot(id, ((SimpleBot) b).getPassword(), configuration);
            bot.login();
            ((SimpleBot) b).setNativeBot(bot);
        } catch (Exception e) {
            throw new BotLoginException(id, e);
        }
        try {
            EventManager.submit(new BotLoginEvent(b));
        } catch (EventSubmitException e) {
            FocessQQ.getLogger().thrLang("exception-submit-bot-login-event", e);
        }
        List<Listener<?>> listeners = Lists.newArrayList();
        listeners.add(bot.getEventChannel().subscribeAlways(GroupMessageEvent.class, event -> {
            GroupChatEvent e = new GroupChatEvent(b, event.getSender(), event.getMessage(), event.getSource());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException eventSubmitException) {
                FocessQQ.getLogger().thrLang("exception-submit-group-chat-event", eventSubmitException);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(FriendMessageEvent.class, event -> {
            FriendChatEvent e = new FriendChatEvent(b, event.getFriend(), event.getMessage(), event.getSource());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException eventSubmitException) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-chat-event", eventSubmitException);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.GroupRecall.class, event -> {
            GroupRecallEvent e = new GroupRecallEvent(b, event.getAuthor(), event.getMessageIds(), event.getOperator());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-group-recall-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.FriendRecall.class, event -> {
            FriendRecallEvent e = new FriendRecallEvent(b, event.getAuthor(), event.getMessageIds());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-recall-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(NewFriendRequestEvent.class, event -> {
            FriendRequestEvent e = new FriendRequestEvent(b, event.getFromId(), event.getFromNick(), event.getFromGroup(), event.getMessage());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-request-event", ex);
            }
            if (e.getAccept() != null)
                if (e.getAccept())
                    event.accept();
                else
                    event.reject(e.isBlackList());
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(BotInvitedJoinGroupRequestEvent.class, event -> {
            GroupRequestEvent e = new GroupRequestEvent(b, event.getGroupId(), event.getGroupName(), event.getInvitor());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-group-request-event", ex);
            }
            if (e.getAccept() != null)
                if (e.getAccept())
                    event.accept();
                else
                    event.ignore();
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(FriendInputStatusChangedEvent.class, event -> {
            FriendInputStatusEvent e = new FriendInputStatusEvent(b, event.getFriend(), event.getInputting());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-input-status-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(StrangerMessageEvent.class, event -> {
            StrangerChatEvent e = new StrangerChatEvent(b, event.getMessage(), event.getSender(), event.getSource());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-stranger-chat-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(MessagePostSendEvent.class, event -> {
            BotSendMessageEvent e = new BotSendMessageEvent(b, event.getMessage(), event.getTarget());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-bot-send-message-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(MessagePreSendEvent.class, event -> {
            BotPreSendMessageEvent e = new BotPreSendMessageEvent(b, event.getMessage(), event.getTarget(), event);
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-bot-pre-send-message-event", ex);
            }
        }));
        BOT_LISTENER_MAP.put(b, listeners);
        return true;
    }
    return false;
}
Also used : top.focess.qq.api.event.bot(top.focess.qq.api.event.bot) GroupRecallEvent(top.focess.qq.api.event.recall.GroupRecallEvent) Continuation(kotlin.coroutines.Continuation) FriendChatEvent(top.focess.qq.api.event.chat.FriendChatEvent) FriendRecallEvent(top.focess.qq.api.event.recall.FriendRecallEvent) StrangerChatEvent(top.focess.qq.api.event.chat.StrangerChatEvent) EventSubmitException(top.focess.qq.api.event.EventSubmitException) InputTimeoutException(top.focess.qq.api.util.InputTimeoutException) Bot(top.focess.qq.api.bot.Bot) EventManager(top.focess.qq.api.event.EventManager) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Scheduler(top.focess.qq.api.schedule.Scheduler) Map(java.util.Map) Schedulers(top.focess.qq.api.schedule.Schedulers) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) BotManager(top.focess.qq.api.bot.BotManager) Listener(net.mamoe.mirai.event.Listener) LoginSolver(net.mamoe.mirai.utils.LoginSolver) IOHandler(top.focess.qq.api.util.IOHandler) FocessQQ(top.focess.qq.FocessQQ) IOException(java.io.IOException) Plugin(top.focess.qq.api.plugin.Plugin) Maps(com.google.common.collect.Maps) GroupChatEvent(top.focess.qq.api.event.chat.GroupChatEvent) File(java.io.File) FriendRequestEvent(top.focess.qq.api.event.request.FriendRequestEvent) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) BotFactory(net.mamoe.mirai.BotFactory) GroupRequestEvent(top.focess.qq.api.event.request.GroupRequestEvent) net.mamoe.mirai.event.events(net.mamoe.mirai.event.events) NotNull(org.jetbrains.annotations.NotNull) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) BotLoginException(top.focess.qq.api.bot.BotLoginException) BotReloginEvent(top.focess.qq.api.event.bot.BotReloginEvent) LoginSolver(net.mamoe.mirai.utils.LoginSolver) Listener(net.mamoe.mirai.event.Listener) GroupRecallEvent(top.focess.qq.api.event.recall.GroupRecallEvent) InputTimeoutException(top.focess.qq.api.util.InputTimeoutException) EventSubmitException(top.focess.qq.api.event.EventSubmitException) BotLoginException(top.focess.qq.api.bot.BotLoginException) StrangerChatEvent(top.focess.qq.api.event.chat.StrangerChatEvent) GroupChatEvent(top.focess.qq.api.event.chat.GroupChatEvent) FriendRequestEvent(top.focess.qq.api.event.request.FriendRequestEvent) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) FriendRecallEvent(top.focess.qq.api.event.recall.FriendRecallEvent) FriendChatEvent(top.focess.qq.api.event.chat.FriendChatEvent) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IOException(java.io.IOException) EventSubmitException(top.focess.qq.api.event.EventSubmitException) InputTimeoutException(top.focess.qq.api.util.InputTimeoutException) IOException(java.io.IOException) BotLoginException(top.focess.qq.api.bot.BotLoginException) GroupRequestEvent(top.focess.qq.api.event.request.GroupRequestEvent) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with BotConfiguration

use of net.mamoe.mirai.utils.BotConfiguration in project ColorMirai by Coloryr.

the class BotStart method start.

public static boolean start() {
    for (QQsObj item : ColorMiraiMain.config.qqList) {
        Bot bot = BotFactory.INSTANCE.newBot(item.qq, item.password, new BotConfiguration() {

            {
                fileBasedDeviceInfo(ColorMiraiMain.runDir + item.info);
                setProtocol(item.loginType);
                setHighwayUploadCoroutineCount(ColorMiraiMain.config.highwayUpload);
                redirectNetworkLogToDirectory(new File(ColorMiraiMain.runDir + "/botnetwork"));
                redirectBotLogToDirectory(new File(ColorMiraiMain.runDir + "/botlog"));
                setAutoReconnectOnForceOffline(ColorMiraiMain.config.autoReconnect);
            }
        });
        try {
            ColorMiraiMain.logger.info("正在登录QQ:" + item.qq);
            ColorMiraiMain.logger.info("如果登录卡住,去看看BotLog文件夹里面的日志有没有验证码");
            bot.login();
            bots.put(item.qq, bot);
            ColorMiraiMain.logger.info("QQ:" + item.qq + "已登录");
        } catch (WrongPasswordException e) {
            ColorMiraiMain.logger.error("机器人密码错误", e);
        } catch (Exception e) {
            ColorMiraiMain.logger.error("机器人错误", e);
            return false;
        }
    }
    if (bots.size() == 0) {
        ColorMiraiMain.logger.error("没有QQ号登录");
        return false;
    }
    BotEvent host = new BotEvent();
    for (Bot item : bots.values()) {
        item.getEventChannel().registerListenerHost(host);
        messageList.put(item.getId(), new ConcurrentHashMap<>());
        break;
    }
    service.scheduleAtFixedRate(() -> {
        if (!reList.isEmpty()) {
            for (ReCallObj item : reList) {
                if (messageList.containsKey(item.bot)) {
                    Map<Integer, MessageSaveObj> list = messageList.get(item.bot);
                    MessageSaveObj obj = list.get(item.mid);
                    if (obj == null) {
                        ColorMiraiMain.logger.warn("不存在消息:" + item.mid);
                        continue;
                    }
                    if (obj.time > 0 || obj.time == -1)
                        try {
                            Mirai.getInstance().recallMessage(bots.get(item.bot), obj.source);
                            list.remove(item.mid);
                            messageList.put(item.bot, list);
                        } catch (Exception e) {
                            ColorMiraiMain.logger.error("消息撤回失败", e);
                        }
                }
            }
            reList.clear();
        }
        for (Map<Integer, MessageSaveObj> item : messageList.values()) {
            for (Map.Entry<Integer, MessageSaveObj> item1 : item.entrySet()) {
                item1.getValue().time -= 1;
            }
            if (item.size() >= ColorMiraiMain.config.maxList) {
                Iterator<Integer> iterator = item.keySet().iterator();
                if (iterator.hasNext()) {
                    iterator.next();
                    iterator.remove();
                }
            }
        }
    }, 0, 1, TimeUnit.SECONDS);
    service1.scheduleAtFixedRate(() -> {
        if (!tasks.isEmpty()) {
            SendPackObj task = tasks.remove(0);
            for (ThePlugin item : PluginUtils.getAll()) {
                item.callEvent(task, task.data, task.index);
            }
        }
    }, 0, 100, TimeUnit.MICROSECONDS);
    ColorMiraiMain.logger.info("机器人已启动");
    return true;
}
Also used : Bot(net.mamoe.mirai.Bot) QQsObj(coloryr.colormirai.config.QQsObj) SendPackObj(coloryr.colormirai.plugin.obj.SendPackObj) WrongPasswordException(net.mamoe.mirai.network.WrongPasswordException) WrongPasswordException(net.mamoe.mirai.network.WrongPasswordException) ThePlugin(coloryr.colormirai.plugin.ThePlugin) File(java.io.File) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration)

Example 5 with BotConfiguration

use of net.mamoe.mirai.utils.BotConfiguration in project MiraiQQ by MidCoard.

the class SimpleBotManager method login.

@Override
public boolean login(Bot b) {
    if (!b.isOnline() && b instanceof SimpleBot) {
        long id = b.getId();
        BotConfiguration configuration = BotConfiguration.getDefault();
        configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
        File cache = new File("devices/" + id + "/cache");
        if (!cache.exists())
            if (!cache.mkdirs())
                throw new BotLoginException(id, FocessQQ.getLangConfig().get("fatal-create-cache-dir-failed"));
        configuration.fileBasedDeviceInfo("devices/" + id + "/device.json");
        configuration.setCacheDir(cache);
        configuration.setLoginSolver(new LoginSolver() {

            @Nullable
            @Override
            public Object onSolvePicCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull byte[] bytes, @NotNull Continuation<? super String> continuation) {
                try {
                    FileImageOutputStream outputStream = new FileImageOutputStream(new File("captcha.jpg"));
                    outputStream.write(bytes);
                    outputStream.close();
                } catch (IOException e) {
                    FocessQQ.getLogger().thrLang("exception-load-captcha-picture", e);
                }
                FocessQQ.getLogger().infoLang("input-captcha-code");
                return IOHandler.getConsoleIoHandler().input();
            }

            @Nullable
            @Override
            public Object onSolveSliderCaptcha(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
                FocessQQ.getLogger().info(s);
                IOHandler.getConsoleIoHandler().input();
                return null;
            }

            @Nullable
            @Override
            public Object onSolveUnsafeDeviceLoginVerify(@NotNull net.mamoe.mirai.Bot bot, @NotNull String s, @NotNull Continuation<? super String> continuation) {
                FocessQQ.getLogger().info(s);
                IOHandler.getConsoleIoHandler().input();
                return null;
            }
        });
        net.mamoe.mirai.Bot bot;
        try {
            bot = BotFactory.INSTANCE.newBot(id, ((SimpleBot) b).getPassword(), configuration);
            bot.login();
            ((SimpleBot) b).setNativeBot(bot);
        } catch (Exception e) {
            throw new BotLoginException(id, e);
        }
        try {
            EventManager.submit(new BotLoginEvent(b));
        } catch (EventSubmitException e) {
            FocessQQ.getLogger().thrLang("exception-submit-bot-login-event", e);
        }
        List<Listener<?>> listeners = Lists.newArrayList();
        listeners.add(bot.getEventChannel().subscribeAlways(GroupMessageEvent.class, event -> {
            GroupChatEvent e = new GroupChatEvent(b, event.getSender(), event.getMessage(), event.getSource());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException eventSubmitException) {
                FocessQQ.getLogger().thrLang("exception-submit-group-chat-event", eventSubmitException);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(FriendMessageEvent.class, event -> {
            FriendChatEvent e = new FriendChatEvent(b, event.getFriend(), event.getMessage(), event.getSource());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException eventSubmitException) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-chat-event", eventSubmitException);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.GroupRecall.class, event -> {
            GroupRecallEvent e = new GroupRecallEvent(b, event.getAuthor(), event.getMessageIds(), event.getOperator());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-group-recall-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(MessageRecallEvent.FriendRecall.class, event -> {
            FriendRecallEvent e = new FriendRecallEvent(b, event.getAuthor(), event.getMessageIds());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-recall-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(NewFriendRequestEvent.class, event -> {
            FriendRequestEvent e = new FriendRequestEvent(b, event.getFromId(), event.getFromNick(), event.getFromGroup(), event.getMessage());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-request-event", ex);
            }
            if (e.getAccept() != null)
                if (e.getAccept())
                    event.accept();
                else
                    event.reject(e.isBlackList());
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(BotInvitedJoinGroupRequestEvent.class, event -> {
            GroupRequestEvent e = new GroupRequestEvent(b, event.getGroupId(), event.getGroupName(), event.getInvitor());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-group-request-event", ex);
            }
            if (e.getAccept() != null)
                if (e.getAccept())
                    event.accept();
                else
                    event.ignore();
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(FriendInputStatusChangedEvent.class, event -> {
            FriendInputStatusEvent e = new FriendInputStatusEvent(b, event.getFriend(), event.getInputting());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-friend-input-status-event", ex);
            }
        }));
        listeners.add(bot.getEventChannel().subscribeAlways(StrangerMessageEvent.class, event -> {
            StrangerChatEvent e = new StrangerChatEvent(b, event.getMessage(), event.getSender(), event.getSource());
            try {
                EventManager.submit(e);
            } catch (EventSubmitException ex) {
                FocessQQ.getLogger().thrLang("exception-submit-stranger-chat-event", ex);
            }
        }));
        BOT_LISTENER_MAP.put(b, listeners);
        return true;
    }
    return false;
}
Also used : GroupRecallEvent(top.focess.qq.api.event.recall.GroupRecallEvent) Continuation(kotlin.coroutines.Continuation) FriendChatEvent(top.focess.qq.api.event.chat.FriendChatEvent) BotLoginEvent(top.focess.qq.api.event.bot.BotLoginEvent) FriendRecallEvent(top.focess.qq.api.event.recall.FriendRecallEvent) StrangerChatEvent(top.focess.qq.api.event.chat.StrangerChatEvent) Bot(top.focess.qq.api.bot.Bot) EventManager(top.focess.qq.api.event.EventManager) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Scheduler(top.focess.qq.api.schedule.Scheduler) Map(java.util.Map) EventSubmitException(top.focess.qq.api.exceptions.EventSubmitException) Schedulers(top.focess.qq.api.schedule.Schedulers) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) BotManager(top.focess.qq.api.bot.BotManager) Listener(net.mamoe.mirai.event.Listener) LoginSolver(net.mamoe.mirai.utils.LoginSolver) FriendInputStatusEvent(top.focess.qq.api.event.bot.FriendInputStatusEvent) IOHandler(top.focess.qq.api.util.IOHandler) FocessQQ(top.focess.qq.FocessQQ) BotLoginException(top.focess.qq.api.exceptions.BotLoginException) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) GroupChatEvent(top.focess.qq.api.event.chat.GroupChatEvent) File(java.io.File) FriendRequestEvent(top.focess.qq.api.event.request.FriendRequestEvent) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) BotFactory(net.mamoe.mirai.BotFactory) GroupRequestEvent(top.focess.qq.api.event.request.GroupRequestEvent) net.mamoe.mirai.event.events(net.mamoe.mirai.event.events) BotLogoutEvent(top.focess.qq.api.event.bot.BotLogoutEvent) NotNull(org.jetbrains.annotations.NotNull) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) BotReloginEvent(top.focess.qq.api.event.bot.BotReloginEvent) LoginSolver(net.mamoe.mirai.utils.LoginSolver) Listener(net.mamoe.mirai.event.Listener) FriendInputStatusEvent(top.focess.qq.api.event.bot.FriendInputStatusEvent) GroupRecallEvent(top.focess.qq.api.event.recall.GroupRecallEvent) BotLoginEvent(top.focess.qq.api.event.bot.BotLoginEvent) EventSubmitException(top.focess.qq.api.exceptions.EventSubmitException) BotLoginException(top.focess.qq.api.exceptions.BotLoginException) StrangerChatEvent(top.focess.qq.api.event.chat.StrangerChatEvent) GroupChatEvent(top.focess.qq.api.event.chat.GroupChatEvent) FriendRequestEvent(top.focess.qq.api.event.request.FriendRequestEvent) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) FriendRecallEvent(top.focess.qq.api.event.recall.FriendRecallEvent) FriendChatEvent(top.focess.qq.api.event.chat.FriendChatEvent) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IOException(java.io.IOException) EventSubmitException(top.focess.qq.api.exceptions.EventSubmitException) BotLoginException(top.focess.qq.api.exceptions.BotLoginException) IOException(java.io.IOException) GroupRequestEvent(top.focess.qq.api.event.request.GroupRequestEvent) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

BotConfiguration (net.mamoe.mirai.utils.BotConfiguration)12 File (java.io.File)9 Bot (net.mamoe.mirai.Bot)7 IOException (java.io.IOException)6 List (java.util.List)5 Map (java.util.Map)5 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)5 BotFactory (net.mamoe.mirai.BotFactory)5 net.mamoe.mirai.event.events (net.mamoe.mirai.event.events)5 LoginSolver (net.mamoe.mirai.utils.LoginSolver)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 Future (java.util.concurrent.Future)4 Continuation (kotlin.coroutines.Continuation)4 Listener (net.mamoe.mirai.event.Listener)4 FocessQQ (top.focess.qq.FocessQQ)4 Bot (top.focess.qq.api.bot.Bot)4 BotManager (top.focess.qq.api.bot.BotManager)4