Search in sources :

Example 1 with MiraiHttpAPI

use of me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI in project MiraiMC by DreamVoid.

the class JavaMain method main.

public static void main(String[] args) {
    try {
        MiraiHttpAPI api = new MiraiHttpAPI("http://localhost:8080");
        System.out.println("Verify");
        Verify verify = api.verify("INITKEYwaYvbCS3");
        System.out.println("code: " + verify.code);
        System.out.println("session:" + verify.session);
        System.out.println("msg: " + verify.msg);
        System.out.println("Bind");
        Bind bind = api.bind(verify.session, Long.parseLong(args[0]));
        System.out.println("code: " + bind.code);
        System.out.println("msg: " + bind.msg);
        System.out.println("SendFriendMessage");
        SendMessage sendFriendMessage = api.sendFriendMessage(verify.session, Long.parseLong(args[1]), "123456");
        System.out.println("code: " + sendFriendMessage.code);
        System.out.println("msg: " + sendFriendMessage.msg);
        System.out.println("messageId: " + sendFriendMessage.messageId);
        System.out.println("SendGroupMessage");
        SendMessage sendGroupMessage = api.sendGroupMessage(verify.session, Long.parseLong(args[2]), "123456");
        System.out.println("code: " + sendGroupMessage.code);
        System.out.println("msg: " + sendGroupMessage.msg);
        System.out.println("messageId: " + sendGroupMessage.messageId);
        /*System.out.println("FetchMessage");
            FetchMessage fetchMessage = api.fetchMessage("mKjSL1Ic", 10);
            System.out.println("code: " + fetchMessage.code);
            System.out.println("msg: " + fetchMessage.msg);
            for(FetchMessage.Data data : fetchMessage.data) {
                for(Message msg : data.messageChain){
                    System.out.println("Type: " + msg.type);
                    System.out.println("Text: " + msg.text);
                    System.out.println("url: " + msg.url);
                }
            }*/
        System.out.println("Release");
    // Release release = api.release(verify.session, Long.parseLong(args[0]));
    // System.out.println("code: " + release.code);
    // System.out.println("msg: " + release.msg);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Bind(me.dreamvoid.miraimc.internal.httpapi.response.Bind) MiraiHttpAPI(me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI) Verify(me.dreamvoid.miraimc.internal.httpapi.response.Verify) SendMessage(me.dreamvoid.miraimc.internal.httpapi.response.SendMessage)

Example 2 with MiraiHttpAPI

use of me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI in project MiraiMC by DreamVoid.

the class MiraiCommand method execute.

@Override
public void execute(CommandSender sender, String[] args) {
    if (!(args.length == 0)) {
        switch(args[0].toLowerCase()) {
            case "login":
                {
                    if (sender.hasPermission("miraimc.command.mirai.login")) {
                        if (args.length >= 3) {
                            bungee.getProxy().getScheduler().runAsync(bungee, () -> {
                                BotConfiguration.MiraiProtocol Protocol = null;
                                boolean useHttpApi = false;
                                if (args.length == 3) {
                                    Protocol = BotConfiguration.MiraiProtocol.ANDROID_PHONE;
                                } else if (args[3].equalsIgnoreCase("httpapi")) {
                                    useHttpApi = true;
                                } else
                                    try {
                                        Protocol = BotConfiguration.MiraiProtocol.valueOf(args[3].toUpperCase());
                                    } catch (IllegalArgumentException ignored) {
                                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&e无效的协议类型,已自动选择 ANDROID_PHONE.")));
                                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&e可用的协议类型: " + Arrays.toString(BotConfiguration.MiraiProtocol.values()).replace("[", "").replace("]", "") + ", HTTPAPI")));
                                        Protocol = BotConfiguration.MiraiProtocol.ANDROID_PHONE;
                                    }
                                try {
                                    if (!useHttpApi) {
                                        MiraiBot.doBotLogin(Long.parseLong(args[1]), args[2], Protocol);
                                    } else {
                                        if (Config.Gen_EnableHttpApi) {
                                            MiraiHttpAPI httpAPI = new MiraiHttpAPI(Config.HTTPAPI_Url);
                                            httpAPI.bind(httpAPI.verify(args[2]).session, Long.parseLong(args[1]));
                                            sender.sendMessage(new TextComponent(ChatColor.GREEN + args[1] + " HTTP-API登录成功!"));
                                        } else
                                            sender.sendMessage(new TextComponent(ChatColor.RED + "此服务器没有启用HTTP-API模式,请检查配置文件!"));
                                    }
                                } catch (InterruptedException e) {
                                    Utils.logger.warning("登录机器人时出现异常,原因: " + e.getLocalizedMessage());
                                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c登录机器人时出现异常,请检查控制台输出!")));
                                } catch (IOException e) {
                                    Utils.logger.warning("登录机器人时出现异常,原因: " + e);
                                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c登录机器人时出现异常,请检查控制台输出!")));
                                } catch (AbnormalStatusException e) {
                                    Utils.logger.warning("使用HTTPAPI登录机器人时出现异常,状态码:" + e.getCode() + ",原因: " + e.getMessage());
                                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c登录机器人时出现异常,状态码:" + e.getCode() + ",原因: " + e.getMessage())));
                                }
                            });
                        } else {
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai login <账号> <密码> [协议]")));
                        }
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "logout":
                {
                    if (sender.hasPermission("miraimc.command.mirai.logout")) {
                        if (args.length >= 2) {
                            try {
                                MiraiBot.getBot(Long.parseLong(args[1])).doLogout();
                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a已退出指定机器人!")));
                            } catch (NoSuchElementException e) {
                                if (Config.Gen_EnableHttpApi && MiraiHttpAPI.Bots.containsKey(Long.parseLong(args[1]))) {
                                    try {
                                        new MiraiHttpAPI(Config.HTTPAPI_Url).release(MiraiHttpAPI.Bots.get(Long.parseLong(args[1])), Long.parseLong(args[1]));
                                        sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&a已退出指定机器人!")));
                                    } catch (IOException ex) {
                                        Utils.logger.warning("退出机器人时出现异常,原因: " + ex);
                                        sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c退出机器人时出现异常,请检查控制台输出!")));
                                    } catch (AbnormalStatusException ex) {
                                        if (ex.getCode() == 2) {
                                            sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c指定的机器人不存在!")));
                                        } else {
                                            Utils.logger.warning("退出机器人时出现异常,状态码:" + ex.getCode() + ",原因: " + ex.getMessage());
                                            sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c退出机器人时出现异常,状态码:" + ex.getCode() + ",原因: " + ex.getMessage())));
                                        }
                                    }
                                } else
                                    sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c指定的机器人不存在!")));
                            }
                        } else {
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai logout <账号>")));
                        }
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "sendgroupmessage":
                {
                    if (sender.hasPermission("miraimc.command.mirai.sendgroupmessage")) {
                        if (args.length >= 4) {
                            StringBuilder message = new StringBuilder();
                            for (int i = 0; i < args.length; i++) {
                                // list.size()就是循环的次数
                                if (i >= 3) {
                                    message.append(args[i]).append(" ");
                                }
                            }
                            String text = message.toString().replace("\\n", System.lineSeparator());
                            try {
                                MiraiBot.getBot(Long.parseLong(args[1])).getGroup(Long.parseLong(args[2])).sendMessageMirai(text);
                            } catch (NoSuchElementException e) {
                                if (Config.Gen_EnableHttpApi && MiraiHttpAPI.Bots.containsKey(Long.parseLong(args[1]))) {
                                    try {
                                        MiraiHttpAPI.INSTANCE.sendGroupMessage(MiraiHttpAPI.Bots.get(Long.parseLong(args[1])), Long.parseLong(args[2]), text);
                                    } catch (IOException ex) {
                                        Utils.logger.warning("发送群消息时出现异常,原因: " + e);
                                        sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c发送群消息时出现异常,请检查控制台了解更多信息!")));
                                    } catch (AbnormalStatusException ex) {
                                        sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c发送群消息时出现异常,状态码: " + ex.getCode() + ",原因: " + ex.getMessage())));
                                    }
                                }
                            }
                        } else {
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai sendgroupmessage <账号> <群号> <消息>")));
                        }
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "sendfriendmessage":
                {
                    if (sender.hasPermission("miraimc.command.mirai.sendfriendmessage")) {
                        if (args.length >= 4) {
                            StringBuilder message = new StringBuilder();
                            for (int i = 0; i < args.length; i++) {
                                // list.size()就是循环的次数
                                if (i >= 3) {
                                    message.append(args[i]).append(" ");
                                }
                            }
                            String text = message.toString().replace("\\n", System.lineSeparator());
                            try {
                                MiraiBot.getBot(Long.parseLong(args[1])).getFriend(Long.parseLong(args[2])).sendMessageMirai(text);
                            } catch (NoSuchElementException e) {
                                if (Config.Gen_EnableHttpApi && MiraiHttpAPI.Bots.containsKey(Long.parseLong(args[1]))) {
                                    try {
                                        MiraiHttpAPI.INSTANCE.sendGroupMessage(MiraiHttpAPI.Bots.get(Long.parseLong(args[1])), Long.parseLong(args[2]), text);
                                    } catch (IOException ex) {
                                        Utils.logger.warning("发送好友消息时出现异常,原因: " + e);
                                        sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c发送好友消息时出现异常,请检查控制台了解更多信息!")));
                                    } catch (AbnormalStatusException ex) {
                                        sender.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&c发送好友消息时出现异常,状态码: " + ex.getCode() + ",原因: " + ex.getMessage())));
                                    }
                                }
                            }
                        } else {
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai sendfriendmessage <账号> <好友> <消息>")));
                        }
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "sendfriendnudge":
                {
                    if (sender.hasPermission("miraimc.command.mirai.sendfriendnudge")) {
                        if (args.length >= 3) {
                            MiraiBot.getBot(Long.parseLong(args[1])).getFriend(Long.parseLong(args[2])).sendNudge();
                        } else {
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai sendfriendnudge <账号> <好友>")));
                        }
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "list":
                {
                    if (sender.hasPermission("miraimc.command.mirai.list")) {
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a存在的机器人: ")));
                        List<Long> BotList = MiraiBot.getOnlineBots();
                        // Core
                        for (long bots : BotList) {
                            Bot bot = Bot.getInstance(bots);
                            if (bot.isOnline()) {
                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&b" + bot.getId() + "&r &7-&r &6" + Bot.getInstance(bots).getNick())));
                            } else {
                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&b" + bot.getId() + "&r &7-&r &c离线")));
                            }
                        }
                        // HTTP API
                        for (long botWithHttp : MiraiHttpAPI.Bots.keySet()) {
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&b" + botWithHttp + "&r &7-&r &eHTTP API")));
                        }
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "checkonline":
                {
                    if (sender.hasPermission("miraimc.command.mirai.checkonline")) {
                        if (args.length >= 2) {
                            if (MiraiBot.getBot(Long.parseLong(args[1])).isOnline()) {
                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a当前机器人在线")));
                            } else
                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&e当前机器人不在线")));
                        } else
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai checkonline <账号>")));
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "autologin":
                {
                    if (sender.hasPermission("miraimc.command.mirai.autologin")) {
                        if (args.length >= 2) {
                            switch(args[1]) {
                                case "add":
                                    {
                                        boolean result;
                                        if (args.length >= 4) {
                                            if (args.length == 5) {
                                                result = MiraiAutoLogin.addAutoLoginBot(Long.parseLong(args[2]), args[3], args[4]);
                                            } else
                                                result = MiraiAutoLogin.addAutoLoginBot(Long.parseLong(args[2]), args[3], "ANDROID_PHONE");
                                            if (result) {
                                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a新的自动登录机器人添加成功!")));
                                            } else
                                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c新的自动登录机器人添加失败,请检查控制台错误输出!")));
                                        } else
                                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai autologin add <账号> <密码> [协议]")));
                                        break;
                                    }
                                case "remove":
                                    {
                                        boolean result;
                                        if (args.length >= 3) {
                                            result = MiraiAutoLogin.delAutoLoginBot(Long.parseLong(args[2]));
                                            if (result) {
                                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a删除自动登录机器人成功!")));
                                            } else
                                                sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c删除自动登录机器人失败,请检查控制台错误输出!")));
                                        } else
                                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai autologin remove <账号>")));
                                        break;
                                    }
                                case "list":
                                    {
                                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&a存在的自动登录机器人: ")));
                                        try {
                                            for (Object list : MiraiAutoLogin.loadAutoLoginList()) {
                                                Configuration data = ConfigurationProvider.getProvider(net.md_5.bungee.config.JsonConfiguration.class).load(list.toString());
                                                long Account = data.getLong("account");
                                                if (Account != 123456) {
                                                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&b" + Account)));
                                                }
                                            }
                                        } catch (IOException e) {
                                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c执行自动登录时出现异常,原因: " + e)));
                                        }
                                        break;
                                    }
                                default:
                                    {
                                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c未知或不完整的命令,请输入 /mirai help 查看帮助!")));
                                        break;
                                    }
                            }
                        } else
                            sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c未知或不完整的命令,请输入 /mirai help 查看帮助!")));
                    } else
                        sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!")));
                    break;
                }
            case "help":
                {
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6&lMiraiMC&r &b机器人帮助菜单")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai login <账号> <密码> [协议]:&r 登录一个机器人")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai logout <账号>:&r 退出一个机器人")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai list:&r 查看当前存在的机器人")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai sendfriendmessage <账号> <好友> <消息>:&r 向指定好友发送私聊消息")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai sendgroupmessage <账号> <群号> <消息>:&r 向指定群发送群聊消息")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai sendfriendnudge <账号> <好友>:&r 向指定好友发送戳一戳")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai checkonline <账号>:&r 检查指定的机器人是否在线")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai autologin add <账号> <密码> [协议]:&r 添加一个自动登录账号")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai autologin list:&r 查看自动登录账号列表")));
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&6/mirai autologin remove <账号>:&r 删除一个自动登录账号")));
                    break;
                }
            default:
                {
                    sender.sendMessage(new TextComponent(ChatColor.translateAlternateColorCodes('&', "&c未知或不完整的命令,请输入 /mirai help 查看帮助!")));
                    break;
                }
        }
    } else {
        sender.sendMessage(new TextComponent("This server is running " + bungee.getDescription().getName() + " version " + bungee.getDescription().getVersion() + " by " + bungee.getDescription().getAuthor()));
    }
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) Configuration(net.md_5.bungee.config.Configuration) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) Bot(net.mamoe.mirai.Bot) MiraiBot(me.dreamvoid.miraimc.api.MiraiBot) IOException(java.io.IOException) AbnormalStatusException(me.dreamvoid.miraimc.internal.httpapi.exception.AbnormalStatusException) MiraiHttpAPI(me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI) List(java.util.List) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with MiraiHttpAPI

use of me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI in project MiraiMC by DreamVoid.

the class MiraiHttpAPIResolver method run.

@Override
public void run() {
    MiraiHttpAPI api = new MiraiHttpAPI(Config.HTTPAPI_Url);
    for (long account : Bots.keySet()) {
        try {
            String session = Bots.get(account);
            FetchMessage fetchMessage = api.fetchMessage(session, Config.HTTPAPI_MessageFetch_Count);
            if (fetchMessage.code == 0) {
                for (FetchMessage.Data data : fetchMessage.data) {
                    // 这里搞循环是因为mirai http api会返回一堆消息,需要挨个处理
                    // 发送者QQ
                    long id = data.sender.id;
                    // 发送者昵称
                    String nickname = data.sender.nickname;
                    // 发送者备注
                    String remark = data.sender.remark;
                    // 消息类型,用于区分私聊群聊等
                    String type = data.type;
                    // 发送时间
                    int sendTime = 0;
                    // 消息ID
                    int messageId = 0;
                    // 消息内容
                    StringBuilder message = new StringBuilder();
                    for (Message msg : data.messageChain) {
                        // 这里搞循环是用来组合消息链
                        switch(msg.type) {
                            case "Source":
                                {
                                    // 消息源
                                    sendTime = msg.time;
                                    messageId = msg.id;
                                    break;
                                }
                            case "Plain":
                                {
                                    // 普通文本消息
                                    message.append(msg.text);
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                    // 准备广播事件
                    switch(type) {
                        case "FriendMessage":
                            {
                                Bukkit.getPluginManager().callEvent(new MiraiFriendMessageEvent(account, new Message().setSenderId(id).setSenderNickname(nickname).setSenderRemark(remark).setSource(messageId, sendTime).setPlain(message.toString())));
                                break;
                            }
                        case "GroupMessage":
                            {
                                Bukkit.getPluginManager().callEvent(new MiraiGroupMessageEvent(account, data.sender, new Message().setSenderId(id).setSenderNickname(nickname).setSenderRemark(remark).setSource(messageId, sendTime).setPlain(message.toString())));
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                }
            } else
                Utils.logger.warning("Unable to fetch " + account + "'s message, reason: " + fetchMessage.msg);
        } catch (Exception e) {
            Utils.logger.warning("An error occurred while fetching message for " + account + ": " + e);
        }
    }
}
Also used : MiraiGroupMessageEvent(me.dreamvoid.miraimc.bukkit.event.MiraiGroupMessageEvent) FetchMessage(me.dreamvoid.miraimc.internal.httpapi.response.FetchMessage) FetchMessage(me.dreamvoid.miraimc.internal.httpapi.response.FetchMessage) Message(me.dreamvoid.miraimc.internal.httpapi.type.Message) MiraiHttpAPI(me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI) MiraiFriendMessageEvent(me.dreamvoid.miraimc.bukkit.event.MiraiFriendMessageEvent)

Example 4 with MiraiHttpAPI

use of me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI in project MiraiMC by DreamVoid.

the class MiraiCommand method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!(args.length == 0)) {
        switch(args[0].toLowerCase()) {
            case "login":
                {
                    if (sender.hasPermission("miraimc.command.mirai.login")) {
                        if (args.length >= 3) {
                            new BukkitRunnable() {

                                @Override
                                public void run() {
                                    BotConfiguration.MiraiProtocol Protocol = null;
                                    boolean useHttpApi = false;
                                    if (args.length == 3) {
                                        Protocol = BotConfiguration.MiraiProtocol.ANDROID_PHONE;
                                    } else if (args[3].equalsIgnoreCase("httpapi")) {
                                        useHttpApi = true;
                                    } else
                                        try {
                                            Protocol = BotConfiguration.MiraiProtocol.valueOf(args[3].toUpperCase());
                                        } catch (IllegalArgumentException ignored) {
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&e无效的协议类型,已自动选择 ANDROID_PHONE."));
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&e可用的协议类型: " + Arrays.toString(BotConfiguration.MiraiProtocol.values()).replace("[", "").replace("]", "") + ", HTTPAPI"));
                                            Protocol = BotConfiguration.MiraiProtocol.ANDROID_PHONE;
                                        }
                                    try {
                                        if (!useHttpApi) {
                                            MiraiBot.doBotLogin(Long.parseLong(args[1]), args[2], Protocol);
                                        } else {
                                            if (Config.Gen_EnableHttpApi) {
                                                MiraiHttpAPI httpAPI = new MiraiHttpAPI(Config.HTTPAPI_Url);
                                                httpAPI.bind(httpAPI.verify(args[2]).session, Long.parseLong(args[1]));
                                                sender.sendMessage(ChatColor.GREEN + args[1] + " HTTP-API登录成功!");
                                            } else
                                                sender.sendMessage(ChatColor.RED + "此服务器没有启用HTTP-API模式,请检查配置文件!");
                                        }
                                    } catch (InterruptedException e) {
                                        Utils.logger.warning("登录机器人时出现异常,原因: " + e.getLocalizedMessage());
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c登录机器人时出现异常,请检查控制台输出!"));
                                    } catch (IOException e) {
                                        Utils.logger.warning("登录机器人时出现异常,原因: " + e);
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c登录机器人时出现异常,请检查控制台输出!"));
                                    } catch (AbnormalStatusException e) {
                                        Utils.logger.warning("使用HTTPAPI登录机器人时出现异常,状态码:" + e.getCode() + ",原因: " + e.getMessage());
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c登录机器人时出现异常,状态码:" + e.getCode() + ",原因: " + e.getMessage()));
                                    }
                                }
                            }.runTaskAsynchronously(plugin);
                        } else {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai login <账号> <密码> [协议]"));
                        }
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "logout":
                {
                    if (sender.hasPermission("miraimc.command.mirai.logout")) {
                        if (args.length >= 2) {
                            try {
                                MiraiBot.getBot(Long.parseLong(args[1])).doLogout();
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a已退出指定机器人!"));
                            } catch (NoSuchElementException e) {
                                if (Config.Gen_EnableHttpApi && MiraiHttpAPI.Bots.containsKey(Long.parseLong(args[1]))) {
                                    try {
                                        new MiraiHttpAPI(Config.HTTPAPI_Url).release(MiraiHttpAPI.Bots.get(Long.parseLong(args[1])), Long.parseLong(args[1]));
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a已退出指定机器人!"));
                                    } catch (IOException ex) {
                                        Utils.logger.warning("退出机器人时出现异常,原因: " + ex);
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c退出机器人时出现异常,请检查控制台输出!"));
                                    } catch (AbnormalStatusException ex) {
                                        if (ex.getCode() == 2) {
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c指定的机器人不存在!"));
                                        } else {
                                            Utils.logger.warning("退出机器人时出现异常,状态码:" + ex.getCode() + ",原因: " + ex.getMessage());
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c退出机器人时出现异常,状态码:" + ex.getCode() + ",原因: " + ex.getMessage()));
                                        }
                                    }
                                } else
                                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c指定的机器人不存在!"));
                            }
                        } else {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai logout <账号>"));
                        }
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "sendgroupmessage":
                {
                    if (sender.hasPermission("miraimc.command.mirai.sendgroupmessage")) {
                        if (args.length >= 4) {
                            StringBuilder message = new StringBuilder();
                            for (int i = 0; i < args.length; i++) {
                                // list.size()就是循环的次数
                                if (i >= 3) {
                                    message.append(args[i]).append(" ");
                                }
                            }
                            String text = message.toString().replace("\\n", System.lineSeparator());
                            try {
                                MiraiBot.getBot(Long.parseLong(args[1])).getGroup(Long.parseLong(args[2])).sendMessageMirai(text);
                            } catch (NoSuchElementException e) {
                                if (Config.Gen_EnableHttpApi && MiraiHttpAPI.Bots.containsKey(Long.parseLong(args[1]))) {
                                    try {
                                        MiraiHttpAPI.INSTANCE.sendGroupMessage(MiraiHttpAPI.Bots.get(Long.parseLong(args[1])), Long.parseLong(args[2]), text);
                                    } catch (IOException ex) {
                                        Utils.logger.warning("发送群消息时出现异常,原因: " + e);
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c发送群消息时出现异常,请检查控制台了解更多信息!"));
                                    } catch (AbnormalStatusException ex) {
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c发送群消息时出现异常,状态码: " + ex.getCode() + ",原因: " + ex.getMessage()));
                                    }
                                }
                            }
                        } else {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai sendgroupmessage <账号> <群号> <消息>"));
                        }
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "sendfriendmessage":
                {
                    if (sender.hasPermission("miraimc.command.mirai.sendfriendmessage")) {
                        if (args.length >= 4) {
                            StringBuilder message = new StringBuilder();
                            for (int i = 0; i < args.length; i++) {
                                // list.size()就是循环的次数
                                if (i >= 3) {
                                    message.append(args[i]).append(" ");
                                }
                            }
                            String text = message.toString().replace("\\n", System.lineSeparator());
                            try {
                                MiraiBot.getBot(Long.parseLong(args[1])).getFriend(Long.parseLong(args[2])).sendMessageMirai(text);
                            } catch (NoSuchElementException e) {
                                if (Config.Gen_EnableHttpApi && MiraiHttpAPI.Bots.containsKey(Long.parseLong(args[1]))) {
                                    try {
                                        MiraiHttpAPI.INSTANCE.sendGroupMessage(MiraiHttpAPI.Bots.get(Long.parseLong(args[1])), Long.parseLong(args[2]), text);
                                    } catch (IOException ex) {
                                        Utils.logger.warning("发送好友消息时出现异常,原因: " + e);
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c发送好友消息时出现异常,请检查控制台了解更多信息!"));
                                    } catch (AbnormalStatusException ex) {
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c发送好友消息时出现异常,状态码: " + ex.getCode() + ",原因: " + ex.getMessage()));
                                    }
                                }
                            }
                        } else {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai sendfriendmessage <账号> <好友> <消息>"));
                        }
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "sendfriendnudge":
                {
                    if (sender.hasPermission("miraimc.command.mirai.sendfriendnudge")) {
                        if (args.length >= 3) {
                            MiraiBot.getBot(Long.parseLong(args[1])).getFriend(Long.parseLong(args[2])).sendNudge();
                        } else {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai sendfriendnudge <账号> <好友>"));
                        }
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "list":
                {
                    if (sender.hasPermission("miraimc.command.mirai.list")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a存在的机器人: "));
                        List<Long> BotList = MiraiBot.getOnlineBots();
                        // Core
                        for (Long bots : BotList) {
                            Bot bot = Bot.getInstance(bots);
                            if (bot.isOnline()) {
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&b" + bot.getId() + "&r &7-&r &6" + Bot.getInstance(bots).getNick()));
                            } else {
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&b" + bot.getId() + "&r &7-&r &c离线"));
                            }
                        }
                        // HTTP API
                        for (long botWithHttp : MiraiHttpAPI.Bots.keySet()) {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&b" + botWithHttp + "&r &7-&r &eHTTP API"));
                        }
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "checkonline":
                {
                    if (sender.hasPermission("miraimc.command.mirai.checkonline")) {
                        if (args.length >= 2) {
                            if (MiraiBot.getBot(Long.parseLong(args[1])).isOnline()) {
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a当前机器人在线"));
                            } else
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&e当前机器人不在线"));
                        } else
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai checkonline <账号>"));
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "autologin":
                {
                    if (sender.hasPermission("miraimc.command.mirai.autologin")) {
                        if (args.length >= 2) {
                            switch(args[1]) {
                                case "add":
                                    {
                                        boolean result;
                                        if (args.length >= 4) {
                                            if (args.length == 5) {
                                                result = MiraiAutoLogin.addAutoLoginBot(Long.parseLong(args[2]), args[3], args[4]);
                                            } else
                                                result = MiraiAutoLogin.addAutoLoginBot(Long.parseLong(args[2]), args[3], "ANDROID_PHONE");
                                            if (result) {
                                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a新的自动登录机器人添加成功!"));
                                            } else
                                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c新的自动登录机器人添加失败,请检查控制台错误输出!"));
                                        } else
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai autologin add <账号> <密码> [协议]"));
                                        break;
                                    }
                                case "remove":
                                    {
                                        boolean result;
                                        if (args.length >= 3) {
                                            result = MiraiAutoLogin.delAutoLoginBot(Long.parseLong(args[2]));
                                            if (result) {
                                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a删除自动登录机器人成功!"));
                                            } else
                                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c删除自动登录机器人失败,请检查控制台错误输出!"));
                                        } else
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c无效的参数!用法: /mirai autologin remove <账号>"));
                                        break;
                                    }
                                case "list":
                                    {
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a存在的自动登录机器人: "));
                                        List<Map<?, ?>> AutoLoginBotList = MiraiAutoLogin.loadAutoLoginList();
                                        for (Map<?, ?> bots : AutoLoginBotList) {
                                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&b" + bots.get("account")));
                                        }
                                        break;
                                    }
                                default:
                                    {
                                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c未知或不完整的命令,请输入 /mirai help 查看帮助!"));
                                        break;
                                    }
                            }
                        } else
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c未知或不完整的命令,请输入 /mirai help 查看帮助!"));
                    } else
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c你没有足够的权限执行此命令!"));
                    break;
                }
            case "help":
                {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6&lMiraiMC&r &b机器人帮助菜单"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai login <账号> <密码> [协议]:&r 登录一个机器人"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai logout <账号>:&r 退出一个机器人"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai list:&r 查看当前存在的机器人"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai sendfriendmessage <账号> <好友> <消息>:&r 向指定好友发送私聊消息"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai sendgroupmessage <账号> <群号> <消息>:&r 向指定群发送群聊消息"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai sendfriendnudge <账号> <好友>:&r 向指定好友发送戳一戳"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai checkonline <账号>:&r 检查指定的机器人是否在线"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai autologin add <账号> <密码> [协议]:&r 添加一个自动登录账号"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai autologin list:&r 查看自动登录账号列表"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/mirai autologin remove <账号>:&r 删除一个自动登录账号"));
                    break;
                }
            default:
                {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c未知或不完整的命令,请输入 /mirai help 查看帮助!"));
                    break;
                }
        }
        return true;
    } else {
        sender.sendMessage("This server is running " + plugin.getDescription().getName() + " version " + plugin.getDescription().getVersion() + " by " + plugin.getDescription().getAuthors().toString().replace("[", "").replace("]", ""));
        return false;
    }
}
Also used : Bot(net.mamoe.mirai.Bot) MiraiBot(me.dreamvoid.miraimc.api.MiraiBot) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) IOException(java.io.IOException) AbnormalStatusException(me.dreamvoid.miraimc.internal.httpapi.exception.AbnormalStatusException) MiraiHttpAPI(me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI) List(java.util.List) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) BotConfiguration(net.mamoe.mirai.utils.BotConfiguration)

Example 5 with MiraiHttpAPI

use of me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI in project MiraiMC by DreamVoid.

the class MiraiHttpAPIResolver method run.

@Override
public void run() {
    MiraiHttpAPI api = new MiraiHttpAPI(Config.HTTPAPI_Url);
    for (long account : Bots.keySet()) {
        try {
            String session = Bots.get(account);
            FetchMessage fetchMessage = api.fetchMessage(session, Config.HTTPAPI_MessageFetch_Count);
            if (fetchMessage.code == 0) {
                for (FetchMessage.Data data : fetchMessage.data) {
                    // 这里搞循环是因为mirai http api会返回一堆消息,需要挨个处理
                    // 发送者QQ
                    long id = data.sender.id;
                    // 发送者昵称
                    String nickname = data.sender.nickname;
                    // 发送者备注
                    String remark = data.sender.remark;
                    // 消息类型,用于区分私聊群聊等
                    String type = data.type;
                    // 发送时间
                    int sendTime = 0;
                    // 消息ID
                    int messageId = 0;
                    // 消息内容
                    StringBuilder message = new StringBuilder();
                    for (Message msg : data.messageChain) {
                        // 这里搞循环是用来组合消息链
                        switch(msg.type) {
                            case "Source":
                                {
                                    // 消息源
                                    sendTime = msg.time;
                                    messageId = msg.id;
                                    break;
                                }
                            case "Plain":
                                {
                                    // 普通文本消息
                                    message.append(msg.text);
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                    // 准备广播事件
                    switch(type) {
                        case "FriendMessage":
                            {
                                BungeePlugin.INSTANCE.getProxy().getPluginManager().callEvent(new MiraiFriendMessageEvent(account, new Message().setSenderId(id).setSenderNickname(nickname).setSenderRemark(remark).setSource(messageId, sendTime).setPlain(message.toString())));
                                break;
                            }
                        case "GroupMessage":
                            {
                                BungeePlugin.INSTANCE.getProxy().getPluginManager().callEvent(new MiraiGroupMessageEvent(account, data.sender, new Message().setSenderId(id).setSenderNickname(nickname).setSenderRemark(remark).setSource(messageId, sendTime).setPlain(message.toString())));
                                break;
                            }
                        default:
                            break;
                    }
                }
            } else
                Utils.logger.warning("Unable to fetch " + account + "'s message, reason: " + fetchMessage.msg);
        } catch (Exception e) {
            Utils.logger.warning("An error occurred while fetching message for " + account + ": " + e);
        }
    }
}
Also used : MiraiGroupMessageEvent(me.dreamvoid.miraimc.bungee.event.MiraiGroupMessageEvent) FetchMessage(me.dreamvoid.miraimc.internal.httpapi.response.FetchMessage) FetchMessage(me.dreamvoid.miraimc.internal.httpapi.response.FetchMessage) Message(me.dreamvoid.miraimc.internal.httpapi.type.Message) MiraiHttpAPI(me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI) MiraiFriendMessageEvent(me.dreamvoid.miraimc.bungee.event.MiraiFriendMessageEvent)

Aggregations

MiraiHttpAPI (me.dreamvoid.miraimc.internal.httpapi.MiraiHttpAPI)8 IOException (java.io.IOException)5 AbnormalStatusException (me.dreamvoid.miraimc.internal.httpapi.exception.AbnormalStatusException)5 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)4 MiraiBot (me.dreamvoid.miraimc.api.MiraiBot)4 Bot (net.mamoe.mirai.Bot)4 BotConfiguration (net.mamoe.mirai.utils.BotConfiguration)4 FileNotFoundException (java.io.FileNotFoundException)2 FetchMessage (me.dreamvoid.miraimc.internal.httpapi.response.FetchMessage)2 Message (me.dreamvoid.miraimc.internal.httpapi.type.Message)2 AsyncTask (cn.nukkit.scheduler.AsyncTask)1 CommandSource (com.velocitypowered.api.command.CommandSource)1 Map (java.util.Map)1 MiraiFriendMessageEvent (me.dreamvoid.miraimc.bukkit.event.MiraiFriendMessageEvent)1 MiraiGroupMessageEvent (me.dreamvoid.miraimc.bukkit.event.MiraiGroupMessageEvent)1 MiraiFriendMessageEvent (me.dreamvoid.miraimc.bungee.event.MiraiFriendMessageEvent)1 MiraiGroupMessageEvent (me.dreamvoid.miraimc.bungee.event.MiraiGroupMessageEvent)1 Bind (me.dreamvoid.miraimc.internal.httpapi.response.Bind)1 SendMessage (me.dreamvoid.miraimc.internal.httpapi.response.SendMessage)1