Search in sources :

Example 1 with DiscordSRV

use of github.scarsz.discordsrv.DiscordSRV in project DiscordSRV by Scarsz.

the class ConfigUtil method migrate.

// When I wrote this, only God and I understood what I was doing. Now, God only knows.
// If this code works, it was written by Scarsz. If not, I don’t know who wrote it.
public static void migrate() {
    Version configVersion = DiscordSRV.config().getString("ConfigVersion").split("\\.").length == 3 ? Version.valueOf(DiscordSRV.config().getString("ConfigVersion")) : Version.valueOf("1." + DiscordSRV.config().getString("ConfigVersion"));
    Version pluginVersion = Version.valueOf(DiscordSRV.getPlugin().getDescription().getVersion());
    // no migration necessary
    if (configVersion.equals(pluginVersion))
        return;
    if (configVersion.greaterThan(pluginVersion)) {
        DiscordSRV.warning("You're attempting to use a higher config version than the plugin. Things might not work correctly.");
        return;
    }
    DiscordSRV.info("Your DiscordSRV config file was outdated; attempting migration...");
    try {
        if (configVersion.greaterThanOrEqualTo(Version.forIntegers(1, 13, 0))) {
            // messages
            File messagesFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "messages.yml-build." + configVersion + ".old");
            File messagesTo = DiscordSRV.getPlugin().getMessagesFile();
            FileUtils.moveFile(messagesTo, messagesFrom);
            LangUtil.saveMessages();
            copyYmlValues(messagesFrom, messagesTo);
            LangUtil.reloadMessages();
            // config
            File configFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "config.yml-build." + configVersion + ".old");
            File configTo = DiscordSRV.getPlugin().getConfigFile();
            FileUtils.moveFile(configTo, configFrom);
            LangUtil.saveConfig();
            copyYmlValues(configFrom, configTo);
            DiscordSRV.getPlugin().reloadConfig();
        } else {
            // messages
            File messagesFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "config.yml");
            File messagesTo = DiscordSRV.getPlugin().getMessagesFile();
            LangUtil.saveMessages();
            copyYmlValues(messagesFrom, messagesTo);
            LangUtil.reloadMessages();
            // config
            File configFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "config.yml-build." + configVersion + ".old");
            File configTo = DiscordSRV.getPlugin().getConfigFile();
            FileUtils.moveFile(configTo, configFrom);
            LangUtil.saveConfig();
            copyYmlValues(configFrom, configTo);
            DiscordSRV.getPlugin().reloadConfig();
            // channels
            File channelsFile = new File(DiscordSRV.getPlugin().getDataFolder(), "channels.json");
            if (channelsFile.exists()) {
                List<Map<String, String>> channels = new ArrayList<>();
                JsonArray jsonElements = DiscordSRV.getPlugin().getGson().fromJson(FileUtils.readFileToString(channelsFile, Charset.forName("UTF-8")), JsonArray.class);
                for (JsonElement jsonElement : jsonElements) {
                    channels.add(new HashMap<String, String>() {

                        {
                            put(jsonElement.getAsJsonObject().get("channelname").getAsString(), jsonElement.getAsJsonObject().get("channelid").getAsString());
                        }
                    });
                }
                String channelsString = "{" + channels.stream().map(stringStringMap -> "\"" + stringStringMap.keySet().iterator().next() + "\": \"" + stringStringMap.values().iterator().next() + "\"").collect(Collectors.joining(", ")) + "}";
                FileUtils.writeStringToFile(channelsFile, "Channels: " + channelsString, Charset.forName("UTF-8"));
                copyYmlValues(channelsFile, configTo);
                channelsFile.delete();
            }
            // colors
            File colorsFile = new File(DiscordSRV.getPlugin().getDataFolder(), "colors.json");
            FileUtils.moveFile(colorsFile, new File(colorsFile.getParent(), "colors.json.old"));
        }
        DiscordSRV.info("Successfully migrated configuration files to version " + DiscordSRV.config().getString("ConfigVersion"));
    } catch (Exception e) {
        DiscordSRV.error("Failed migrating configs: " + e.getMessage());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) Version(com.github.zafarkhaja.semver.Version) JsonElement(com.google.gson.JsonElement) JsonArray(com.google.gson.JsonArray) java.util(java.util) Charset(java.nio.charset.Charset) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) Version(com.github.zafarkhaja.semver.Version) JsonElement(com.google.gson.JsonElement) File(java.io.File)

Example 2 with DiscordSRV

use of github.scarsz.discordsrv.DiscordSRV in project DiscordSRV by Scarsz.

the class WebhookUtil method getWebhookToUseForChannel.

public static Webhook getWebhookToUseForChannel(TextChannel channel, String targetName) {
    synchronized (lastUsedWebhooks) {
        List<Webhook> webhooks = new ArrayList<>();
        channel.getGuild().getWebhooks().complete().stream().filter(webhook -> webhook.getName().startsWith("DiscordSRV #" + channel.getName() + " #")).forEach(webhooks::add);
        if (webhooks.size() != 2) {
            webhooks.forEach(webhook -> webhook.delete().reason("Purging orphaned webhook").queue());
            webhooks.clear();
            if (!channel.getGuild().getMember(channel.getJDA().getSelfUser()).hasPermission(Permission.MANAGE_WEBHOOKS)) {
                DiscordSRV.error("Can't create a webhook to deliver chat message, bot is missing permission \"Manage Webhooks\"");
                return null;
            }
            // create webhooks to use
            Webhook webhook1 = createWebhook(channel.getGuild(), channel, "DiscordSRV " + channel.getId() + " #1");
            Webhook webhook2 = createWebhook(channel.getGuild(), channel, "DiscordSRV " + channel.getId() + " #2");
            if (webhook1 == null || webhook2 == null)
                return null;
            webhooks.add(webhook1);
            webhooks.add(webhook2);
        }
        LastWebhookInfo info = lastUsedWebhooks.getOrDefault(channel, null);
        Webhook target;
        if (info == null) {
            target = webhooks.get(0);
            lastUsedWebhooks.put(channel, new LastWebhookInfo(target.getId(), targetName));
            return target;
        }
        target = info.targetName.equals(targetName) ? webhooks.get(0).getId().equals(info.webhook) ? webhooks.get(0) : webhooks.get(1) : webhooks.get(0).getId().equals(info.webhook) ? webhooks.get(0) : webhooks.get(1);
        lastUsedWebhooks.put(channel, new LastWebhookInfo(target.getId(), targetName));
        return target;
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Webhook(net.dv8tion.jda.core.entities.Webhook) Guild(net.dv8tion.jda.core.entities.Guild) Unirest(com.mashape.unirest.http.Unirest) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) HttpResponse(com.mashape.unirest.http.HttpResponse) Map(java.util.Map) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ArrayList(java.util.ArrayList) Webhook(net.dv8tion.jda.core.entities.Webhook)

Aggregations

DiscordSRV (github.scarsz.discordsrv.DiscordSRV)2 Version (com.github.zafarkhaja.semver.Version)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 HttpResponse (com.mashape.unirest.http.HttpResponse)1 Unirest (com.mashape.unirest.http.Unirest)1 File (java.io.File)1 Charset (java.nio.charset.Charset)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Permission (net.dv8tion.jda.core.Permission)1 Guild (net.dv8tion.jda.core.entities.Guild)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 Webhook (net.dv8tion.jda.core.entities.Webhook)1 FileUtils (org.apache.commons.io.FileUtils)1 ExceptionUtils (org.apache.commons.lang3.exception.ExceptionUtils)1