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());
}
}
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;
}
}
Aggregations