Search in sources :

Example 1 with AutoLoginObject

use of me.dreamvoid.miraimc.velocity.utils.AutoLoginObject in project MiraiMC by DreamVoid.

the class MiraiAutoLogin method addAutoLoginBot.

public boolean addAutoLoginBot(long Account, String Password, String Protocol) {
    try {
        // 获取现有的机器人列表
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream(AutoLoginFile);
        AutoLoginObject data = yaml.loadAs(inputStream, AutoLoginObject.class);
        if (data.getAccounts() == null) {
            data.setAccounts(new ArrayList<>());
        }
        // 新建用于添加进去的Map
        AutoLoginObject.Accounts account = new AutoLoginObject.Accounts();
        // account 节点
        account.setAccount(Account);
        // password 节点
        AutoLoginObject.Password password = new AutoLoginObject.Password();
        password.setKind("PLAIN");
        password.setValue(Password);
        account.setPassword(password);
        // configuration 节点
        AutoLoginObject.Configuration configuration = new AutoLoginObject.Configuration();
        configuration.setDevice("device.json");
        configuration.setProtocol(Protocol.toUpperCase());
        account.setConfiguration(configuration);
        // 添加
        List<AutoLoginObject.Accounts> accounts = data.getAccounts();
        accounts.add(account);
        data.setAccounts(accounts);
        Yaml yaml1 = new Yaml();
        File writeName = AutoLoginFile;
        try (FileWriter writer = new FileWriter(writeName);
            BufferedWriter out = new BufferedWriter(writer)) {
            out.write(yaml1.dumpAs(data, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
            out.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : BotConfiguration(net.mamoe.mirai.utils.BotConfiguration) Yaml(org.yaml.snakeyaml.Yaml) AutoLoginObject(me.dreamvoid.miraimc.velocity.utils.AutoLoginObject)

Example 2 with AutoLoginObject

use of me.dreamvoid.miraimc.velocity.utils.AutoLoginObject in project MiraiMC by DreamVoid.

the class MiraiAutoLogin method loadAutoLoginList.

public List<AutoLoginObject.Accounts> loadAutoLoginList() throws FileNotFoundException {
    Yaml yaml = new Yaml(new Constructor(AutoLoginObject.class));
    InputStream inputStream = new FileInputStream(AutoLoginFile);
    AutoLoginObject data = yaml.loadAs(inputStream, AutoLoginObject.class);
    if (data.getAccounts() == null) {
        data.setAccounts(new ArrayList<>());
    }
    return data.getAccounts();
}
Also used : AutoLoginObject(me.dreamvoid.miraimc.velocity.utils.AutoLoginObject) Constructor(org.yaml.snakeyaml.constructor.Constructor) Yaml(org.yaml.snakeyaml.Yaml)

Example 3 with AutoLoginObject

use of me.dreamvoid.miraimc.velocity.utils.AutoLoginObject in project MiraiMC by DreamVoid.

the class MiraiAutoLogin method delAutoLoginBot.

public boolean delAutoLoginBot(long Account) {
    try {
        // 获取现有的机器人列表
        Yaml yaml = new Yaml();
        InputStream inputStream = new FileInputStream(AutoLoginFile);
        AutoLoginObject data = yaml.loadAs(inputStream, AutoLoginObject.class);
        if (data.getAccounts() == null) {
            data.setAccounts(new ArrayList<>());
        }
        for (AutoLoginObject.Accounts bots : data.getAccounts()) {
            if (bots.getAccount() == Account) {
                data.getAccounts().remove(bots);
                break;
            }
        }
        Yaml yaml1 = new Yaml();
        File writeName = AutoLoginFile;
        try (FileWriter writer = new FileWriter(writeName);
            BufferedWriter out = new BufferedWriter(writer)) {
            out.write(yaml1.dumpAs(data, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
            out.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : AutoLoginObject(me.dreamvoid.miraimc.velocity.utils.AutoLoginObject) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

AutoLoginObject (me.dreamvoid.miraimc.velocity.utils.AutoLoginObject)3 Yaml (org.yaml.snakeyaml.Yaml)3 BotConfiguration (net.mamoe.mirai.utils.BotConfiguration)1 Constructor (org.yaml.snakeyaml.constructor.Constructor)1