Search in sources :

Example 11 with Attachment

use of net.dv8tion.jda.api.entities.Message.Attachment in project Factorio-FBSR by demodude4u.

the class BlueprintBotDiscordService method handleBlueprintMessageCommand.

private void handleBlueprintMessageCommand(MessageCommandEvent event) throws IOException {
    String content = event.getMessage().getContentDisplay();
    for (Attachment attachment : event.getMessage().getAttachments()) {
        content += " " + attachment.getUrl();
    }
    JSONObject options = new JSONObject();
    findDebugOptions(event.getReporting(), content, options);
    List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
    List<EmbedBuilder> embedBuilders = processBlueprints(blueprintStringDatas, event.getReporting(), options);
    embedBuilders.get(0).setAuthor(event.getMessage().getAuthor().getName(), event.getMessage().getJumpUrl(), event.getMessage().getAuthor().getEffectiveAvatarUrl());
    if (blueprintStringDatas.stream().anyMatch(d -> d.getBlueprints().stream().anyMatch(b -> b.isModsDetected()))) {
        embedBuilders.get(embedBuilders.size() - 1).setFooter("(Modded features are shown as question marks)");
    }
    List<MessageEmbed> embeds = embedBuilders.stream().map(EmbedBuilder::build).collect(Collectors.toList());
    for (MessageEmbed embed : embeds) {
        event.replyEmbed(embed);
    }
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) EventReply(com.demod.dcba.EventReply) URL(java.net.URL) FBSR(com.demod.fbsr.FBSR) Permission(net.dv8tion.jda.api.Permission) SlashCommandHandler(com.demod.dcba.SlashCommandHandler) Field(net.dv8tion.jda.api.entities.MessageEmbed.Field) TextChannel(net.dv8tion.jda.api.entities.TextChannel) DataTable(com.demod.factorio.DataTable) RenderUtils(com.demod.fbsr.RenderUtils) JSONException(org.json.JSONException) Matcher(java.util.regex.Matcher) JSONObject(org.json.JSONObject) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) WebUtils(com.demod.fbsr.WebUtils) Message(net.dv8tion.jda.api.entities.Message) BufferedImage(java.awt.image.BufferedImage) DataPrototype(com.demod.factorio.prototype.DataPrototype) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) Collectors(java.util.stream.Collectors) Config(com.demod.factorio.Config) BlueprintEntity(com.demod.fbsr.BlueprintEntity) List(java.util.List) FactorioData(com.demod.factorio.FactorioData) MessageCommandEvent(com.demod.dcba.MessageCommandEvent) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) BlueprintStringData(com.demod.fbsr.BlueprintStringData) ZipOutputStream(java.util.zip.ZipOutputStream) Blueprint(com.demod.fbsr.Blueprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WatchdogReporter(com.demod.fbsr.app.WatchdogService.WatchdogReporter) ImmutableList(com.google.common.collect.ImmutableList) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) SlashCommandEvent(com.demod.dcba.SlashCommandEvent) SimpleEntry(java.util.AbstractMap.SimpleEntry) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) MapVersion(com.demod.fbsr.MapVersion) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandReporting(com.demod.dcba.CommandReporting) DCBA(com.demod.dcba.DCBA) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Utils(com.demod.factorio.Utils) DiscordBot(com.demod.dcba.DiscordBot) LuaValue(org.luaj.vm2.LuaValue) AutoCompleteHandler(com.demod.dcba.AutoCompleteHandler) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayDeque(java.util.ArrayDeque) BlueprintFinder(com.demod.fbsr.BlueprintFinder) ChunkingFilter(net.dv8tion.jda.api.utils.ChunkingFilter) JSONArray(org.json.JSONArray) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) JSONObject(org.json.JSONObject) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) BlueprintStringData(com.demod.fbsr.BlueprintStringData)

Example 12 with Attachment

use of net.dv8tion.jda.api.entities.Message.Attachment in project Factorio-FBSR by demodude4u.

the class BlueprintBotDiscordService method handleBlueprintSlashCommand.

private void handleBlueprintSlashCommand(SlashCommandEvent event) throws IOException {
    String content = event.getCommandString();
    Optional<Attachment> attachment = event.optParamAttachment("file");
    if (attachment.isPresent()) {
        content += " " + attachment.get().getUrl();
    }
    JSONObject options = new JSONObject();
    findDebugOptions(event.getReporting(), content, options);
    event.optParamBoolean("simple").ifPresent(b -> options.put("show-info-panels", !b));
    event.optParamLong("max-width").ifPresent(l -> options.put("max-width", l.intValue()));
    event.optParamLong("max-height").ifPresent(l -> options.put("max-height", l.intValue()));
    List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
    List<EmbedBuilder> embedBuilders = processBlueprints(blueprintStringDatas, event.getReporting(), options);
    if (blueprintStringDatas.stream().anyMatch(d -> d.getBlueprints().stream().anyMatch(b -> b.isModsDetected()))) {
        embedBuilders.get(embedBuilders.size() - 1).setFooter("(Modded features are shown as question marks)");
    }
    List<MessageEmbed> embeds = embedBuilders.stream().map(EmbedBuilder::build).collect(Collectors.toList());
    for (MessageEmbed embed : embeds) {
        event.replyEmbed(embed);
    }
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) EventReply(com.demod.dcba.EventReply) URL(java.net.URL) FBSR(com.demod.fbsr.FBSR) Permission(net.dv8tion.jda.api.Permission) SlashCommandHandler(com.demod.dcba.SlashCommandHandler) Field(net.dv8tion.jda.api.entities.MessageEmbed.Field) TextChannel(net.dv8tion.jda.api.entities.TextChannel) DataTable(com.demod.factorio.DataTable) RenderUtils(com.demod.fbsr.RenderUtils) JSONException(org.json.JSONException) Matcher(java.util.regex.Matcher) JSONObject(org.json.JSONObject) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) WebUtils(com.demod.fbsr.WebUtils) Message(net.dv8tion.jda.api.entities.Message) BufferedImage(java.awt.image.BufferedImage) DataPrototype(com.demod.factorio.prototype.DataPrototype) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) Collectors(java.util.stream.Collectors) Config(com.demod.factorio.Config) BlueprintEntity(com.demod.fbsr.BlueprintEntity) List(java.util.List) FactorioData(com.demod.factorio.FactorioData) MessageCommandEvent(com.demod.dcba.MessageCommandEvent) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) BlueprintStringData(com.demod.fbsr.BlueprintStringData) ZipOutputStream(java.util.zip.ZipOutputStream) Blueprint(com.demod.fbsr.Blueprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WatchdogReporter(com.demod.fbsr.app.WatchdogService.WatchdogReporter) ImmutableList(com.google.common.collect.ImmutableList) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) SlashCommandEvent(com.demod.dcba.SlashCommandEvent) SimpleEntry(java.util.AbstractMap.SimpleEntry) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) MapVersion(com.demod.fbsr.MapVersion) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandReporting(com.demod.dcba.CommandReporting) DCBA(com.demod.dcba.DCBA) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Utils(com.demod.factorio.Utils) DiscordBot(com.demod.dcba.DiscordBot) LuaValue(org.luaj.vm2.LuaValue) AutoCompleteHandler(com.demod.dcba.AutoCompleteHandler) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayDeque(java.util.ArrayDeque) BlueprintFinder(com.demod.fbsr.BlueprintFinder) ChunkingFilter(net.dv8tion.jda.api.utils.ChunkingFilter) JSONArray(org.json.JSONArray) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) JSONObject(org.json.JSONObject) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) BlueprintStringData(com.demod.fbsr.BlueprintStringData)

Example 13 with Attachment

use of net.dv8tion.jda.api.entities.Message.Attachment in project Factorio-FBSR by demodude4u.

the class BlueprintBotDiscordService method handleBlueprintJsonCommand.

private void handleBlueprintJsonCommand(SlashCommandEvent event) throws JSONException, IOException {
    String content = event.getCommandString();
    Optional<Attachment> attachment = event.optParamAttachment("file");
    if (attachment.isPresent()) {
        content += " " + attachment.get().getUrl();
    }
    List<String> results = BlueprintFinder.searchRaw(content, event.getReporting());
    if (!results.isEmpty()) {
        byte[] bytes = BlueprintStringData.decode(results.get(0)).toString(2).getBytes();
        if (results.size() == 1) {
            event.replyFile(bytes, "blueprint.json");
        } else {
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ZipOutputStream zos = new ZipOutputStream(baos)) {
                for (int i = 0; i < results.size(); i++) {
                    try {
                        String blueprintString = results.get(i);
                        zos.putNextEntry(new ZipEntry("blueprint " + (i + 1) + ".json"));
                        zos.write(BlueprintStringData.decode(blueprintString).toString(2).getBytes());
                    } catch (Exception e) {
                        event.getReporting().addException(e);
                    }
                }
                zos.close();
                byte[] zipData = baos.toByteArray();
                event.replyFile(zipData, "blueprint JSON files.zip");
            }
        }
    } else {
        event.replyIfNoException("No blueprint found!");
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Blueprint(com.demod.fbsr.Blueprint) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 14 with Attachment

use of net.dv8tion.jda.api.entities.Message.Attachment in project Factorio-FBSR by demodude4u.

the class BlueprintBotDiscordService method startUp.

@Override
protected void startUp() throws JSONException, IOException {
    configJson = Config.get().getJSONObject("discord");
    DataTable table = FactorioData.getTable();
    System.out.println("Factorio " + FBSR.getVersion() + " Data Loaded.");
    bot = // 
    DCBA.builder().setInfo(// 
    "Blueprint Bot").withSupport(// 
    "Find Demod and complain to him!\nYou can find him over in the [Factorio Discord.](https://discord.gg/factorio)").withTechnology("[FBSR](https://github.com/demodude4u/Factorio-FBSR)", // 
    "Factorio Blueprint String Renderer").withTechnology("[FactorioDataWrapper](https://github.com/demodude4u/Java-Factorio-Data-Wrapper)", // 
    "Factorio Data Scraper").withCredits("Attribution", // 
    "[Factorio](https://www.factorio.com/) - Made by Wube Software").withCredits("Contributors", // 
    "Demod").withCredits("Contributors", // 
    "Bilka").withCredits("Contributors", // 
    "FactorioBlueprints").withCredits("Contributors", // 
    "acid").withCredits("Contributors", // 
    "Vilsol").withInvite(new Permission[] { // 
    Permission.VIEW_CHANNEL, // 
    Permission.MESSAGE_SEND, // 
    Permission.MESSAGE_ATTACH_FILES, // 
    Permission.MESSAGE_EXT_EMOJI, // 
    Permission.MESSAGE_EMBED_LINKS, // 
    Permission.MESSAGE_HISTORY, // 
    Permission.MESSAGE_ADD_REACTION }).withCustomField("Need old !blueprint commands?", // 
    "[BlueprintBot Legacy Invite Link](https://discord.com/oauth2/authorize?scope=bot&client_id=958469202824552498&permissions=379968)").addMessageCommand("Blueprint Image", event -> handleBlueprintMessageCommand(event)).addSlashCommand("blueprint/string", "Renders an image of the blueprint string.", // 
    event -> handleBlueprintSlashCommand(event)).withParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.BOOLEAN, "simple", "Set True to show just the image, no side panels.").withOptionalParam(OptionType.INTEGER, "max-width", "Maximum width of image, in pixels.").withOptionalParam(OptionType.INTEGER, "max-height", "Maximum height of image, in pixels.").withLegacyWarning("blueprint", // 
    "bp").addSlashCommand("blueprint/url", "Renders an image of the blueprint url.", // 
    event -> handleBlueprintSlashCommand(event)).withParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.BOOLEAN, "simple", "Set True to show just the image, no side panels.").withOptionalParam(OptionType.INTEGER, "max-width", "Maximum width of image, in pixels.").withOptionalParam(OptionType.INTEGER, "max-height", "Maximum height of image, in pixels.").addSlashCommand("blueprint/file", "Renders an image of the blueprint attachment.", // 
    event -> handleBlueprintSlashCommand(event)).withParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withOptionalParam(OptionType.BOOLEAN, "simple", "Set True to show just the image, no side panels.").withOptionalParam(OptionType.INTEGER, "max-width", "Maximum width of image, in pixels.").withOptionalParam(OptionType.INTEGER, "max-height", "Maximum height of image, in pixels.").addSlashCommand("json", "Provides a dump of the json data in the specified blueprint string.", // 
    event -> handleBlueprintJsonCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning(// 
    "blueprintJSON").addSlashCommand("upgrade/belts", "Converts all yellow belts into red belts, and all red belts into blue belts.", // 
    event -> handleBlueprintUpgradeBeltsCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning(// 
    "blueprintUpgradeBelts").addSlashCommand("items", "Prints out all of the items needed by the blueprint.", // 
    event -> handleBlueprintItemsCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning("blueprintItems", // 
    "bpItems").addSlashCommand("raw/items", "Prints out all of the raw items needed by the blueprint.", // 
    event -> handleBlueprintItemsRawCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning("blueprintRawItems", // 
    "bpRawItems").addSlashCommand("counts", "Prints out the total counts of entities, items and tiles needed by the blueprint.", event -> handleBlueprintTotalsCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning("blueprintCounts", // 
    "bpCounts").addSlashCommand("book/extract", "Provides an collection of blueprint strings contained within the specified blueprint book.", // 
    event -> handleBlueprintBookExtractCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning(// 
    "blueprintBookExtract").addSlashCommand("book/assemble", "Combines all blueprints (including from other books) from multiple strings into a single book.", // 
    event -> handleBlueprintBookAssembleCommand(event)).withOptionalParam(OptionType.STRING, "string", // 
    "Blueprint string.").withOptionalParam(OptionType.STRING, "url", // 
    "Url containing blueprint string.").withOptionalParam(OptionType.ATTACHMENT, "file", // 
    "File containing blueprint string.").withLegacyWarning(// 
    "blueprintBookAssemble").addSlashCommand("prototype/entity", "Lua data for the specified entity prototype.", createPrototypeCommandHandler("entity", table.getEntities()), // 
    createPrototypeAutoCompleteHandler(table.getEntities())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the entity.").withLegacyWarning(// 
    "prototypeEntity").addSlashCommand("prototype/recipe", "Lua data for the specified recipe prototype.", createPrototypeCommandHandler("recipe", table.getRecipes()), // 
    createPrototypeAutoCompleteHandler(table.getRecipes())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the recipe.").withLegacyWarning(// 
    "prototypeRecipe").addSlashCommand("prototype/fluid", "Lua data for the specified fluid prototype.", createPrototypeCommandHandler("fluid", table.getFluids()), // 
    createPrototypeAutoCompleteHandler(table.getFluids())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the fluid.").withLegacyWarning(// 
    "prototypeFluid").addSlashCommand("prototype/item", "Lua data for the specified item prototype.", createPrototypeCommandHandler("item", table.getItems()), // 
    createPrototypeAutoCompleteHandler(table.getItems())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the item.").withLegacyWarning(// 
    "prototypeItem").addSlashCommand("prototype/technology", "Lua data for the specified technology prototype.", createPrototypeCommandHandler("technology", table.getTechnologies()), // 
    createPrototypeAutoCompleteHandler(table.getTechnologies())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the technology.").withLegacyWarning(// 
    "prototypeTechnology").addSlashCommand("prototype/equipment", "Lua data for the specified equipment prototype.", createPrototypeCommandHandler("equipment", table.getEquipments()), // 
    createPrototypeAutoCompleteHandler(table.getEquipments())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the equipment.").withLegacyWarning(// 
    "prototypeEquipment").addSlashCommand("prototype/tile", "Lua data for the specified tile prototype.", createPrototypeCommandHandler("tile", table.getTiles()), // 
    createPrototypeAutoCompleteHandler(table.getTiles())).withAutoParam(OptionType.STRING, "name", // 
    "Prototype name of the tile.").withLegacyWarning(// 
    "prototypeTile").addSlashCommand("data/raw", "Lua from `data.raw` for the specified key.", // 
    createDataRawCommandHandler(table::getRaw)).withParam(OptionType.STRING, "path", // 
    "Path to identify which key.").withLegacyWarning(// 
    "dataRaw").withCustomSetup(builder -> {
        return // 
        builder.setChunkingFilter(// 
        ChunkingFilter.NONE);
    }).create();
    bot.startAsync().awaitRunning();
    hostingChannelID = configJson.getString("hosting_channel_id");
    ServiceFinder.addService(this);
    ServiceFinder.addService(WatchdogReporter.class, new WatchdogReporter() {

        @Override
        public void notifyInactive(String label) {
            CommandReporting reporting = new CommandReporting("Watchdog Reporter", null, null);
            reporting.addWarning(label + " has gone inactive!");
            bot.submitReport(reporting);
        }

        @Override
        public void notifyReactive(String label) {
            CommandReporting reporting = new CommandReporting("Watchdog Reporter", null, null);
            reporting.addWarning(label + " is now active again!");
            bot.submitReport(reporting);
        }
    });
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) EventReply(com.demod.dcba.EventReply) URL(java.net.URL) FBSR(com.demod.fbsr.FBSR) Permission(net.dv8tion.jda.api.Permission) SlashCommandHandler(com.demod.dcba.SlashCommandHandler) Field(net.dv8tion.jda.api.entities.MessageEmbed.Field) TextChannel(net.dv8tion.jda.api.entities.TextChannel) DataTable(com.demod.factorio.DataTable) RenderUtils(com.demod.fbsr.RenderUtils) JSONException(org.json.JSONException) Matcher(java.util.regex.Matcher) JSONObject(org.json.JSONObject) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) WebUtils(com.demod.fbsr.WebUtils) Message(net.dv8tion.jda.api.entities.Message) BufferedImage(java.awt.image.BufferedImage) DataPrototype(com.demod.factorio.prototype.DataPrototype) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) Collectors(java.util.stream.Collectors) Config(com.demod.factorio.Config) BlueprintEntity(com.demod.fbsr.BlueprintEntity) List(java.util.List) FactorioData(com.demod.factorio.FactorioData) MessageCommandEvent(com.demod.dcba.MessageCommandEvent) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) BlueprintStringData(com.demod.fbsr.BlueprintStringData) ZipOutputStream(java.util.zip.ZipOutputStream) Blueprint(com.demod.fbsr.Blueprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WatchdogReporter(com.demod.fbsr.app.WatchdogService.WatchdogReporter) ImmutableList(com.google.common.collect.ImmutableList) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) SlashCommandEvent(com.demod.dcba.SlashCommandEvent) SimpleEntry(java.util.AbstractMap.SimpleEntry) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) MapVersion(com.demod.fbsr.MapVersion) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandReporting(com.demod.dcba.CommandReporting) DCBA(com.demod.dcba.DCBA) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Utils(com.demod.factorio.Utils) DiscordBot(com.demod.dcba.DiscordBot) LuaValue(org.luaj.vm2.LuaValue) AutoCompleteHandler(com.demod.dcba.AutoCompleteHandler) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayDeque(java.util.ArrayDeque) BlueprintFinder(com.demod.fbsr.BlueprintFinder) ChunkingFilter(net.dv8tion.jda.api.utils.ChunkingFilter) JSONArray(org.json.JSONArray) DataTable(com.demod.factorio.DataTable) CommandReporting(com.demod.dcba.CommandReporting) WatchdogReporter(com.demod.fbsr.app.WatchdogService.WatchdogReporter)

Example 15 with Attachment

use of net.dv8tion.jda.api.entities.Message.Attachment in project Factorio-FBSR by demodude4u.

the class BlueprintBotDiscordService method handleBlueprintItemsRawCommand.

private void handleBlueprintItemsRawCommand(SlashCommandEvent event) throws IOException {
    DataTable table;
    try {
        table = FactorioData.getTable();
    } catch (JSONException | IOException e1) {
        throw new InternalError(e1);
    }
    String content = event.getCommandString();
    Optional<Attachment> attachment = event.optParamAttachment("file");
    if (attachment.isPresent()) {
        content += " " + attachment.get().getUrl();
    }
    List<BlueprintStringData> blueprintStringDatas = BlueprintFinder.search(content, event.getReporting());
    Map<String, Double> totalItems = new LinkedHashMap<>();
    for (BlueprintStringData blueprintStringData : blueprintStringDatas) {
        for (Blueprint blueprint : blueprintStringData.getBlueprints()) {
            Map<String, Double> items = FBSR.generateTotalItems(table, blueprint);
            items.forEach((k, v) -> {
                totalItems.compute(k, ($, old) -> old == null ? v : old + v);
            });
        }
    }
    Map<String, Double> rawItems = FBSR.generateTotalRawItems(table, table.getRecipes(), totalItems);
    if (!rawItems.isEmpty()) {
        String responseContent = rawItems.entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).map(e -> table.getWikiItemName(e.getKey()) + ": " + RenderUtils.fmtDouble2(e.getValue())).collect(Collectors.joining("\n"));
        String response = "```ldif\n" + responseContent + "```";
        if (response.length() < 2000) {
            event.reply(response);
        } else {
            event.replyFile(responseContent.getBytes(), "raw-items.txt");
        }
    } else if (blueprintStringDatas.stream().anyMatch(d -> !d.getBlueprints().isEmpty())) {
        event.replyIfNoException("I couldn't find any items!");
    } else {
        event.replyIfNoException("No blueprint found!");
    }
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) EventReply(com.demod.dcba.EventReply) URL(java.net.URL) FBSR(com.demod.fbsr.FBSR) Permission(net.dv8tion.jda.api.Permission) SlashCommandHandler(com.demod.dcba.SlashCommandHandler) Field(net.dv8tion.jda.api.entities.MessageEmbed.Field) TextChannel(net.dv8tion.jda.api.entities.TextChannel) DataTable(com.demod.factorio.DataTable) RenderUtils(com.demod.fbsr.RenderUtils) JSONException(org.json.JSONException) Matcher(java.util.regex.Matcher) JSONObject(org.json.JSONObject) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) WebUtils(com.demod.fbsr.WebUtils) Message(net.dv8tion.jda.api.entities.Message) BufferedImage(java.awt.image.BufferedImage) DataPrototype(com.demod.factorio.prototype.DataPrototype) LevenshteinDistance(org.apache.commons.text.similarity.LevenshteinDistance) Collectors(java.util.stream.Collectors) Config(com.demod.factorio.Config) BlueprintEntity(com.demod.fbsr.BlueprintEntity) List(java.util.List) FactorioData(com.demod.factorio.FactorioData) MessageCommandEvent(com.demod.dcba.MessageCommandEvent) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) BlueprintStringData(com.demod.fbsr.BlueprintStringData) ZipOutputStream(java.util.zip.ZipOutputStream) Blueprint(com.demod.fbsr.Blueprint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WatchdogReporter(com.demod.fbsr.app.WatchdogService.WatchdogReporter) ImmutableList(com.google.common.collect.ImmutableList) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) SlashCommandEvent(com.demod.dcba.SlashCommandEvent) SimpleEntry(java.util.AbstractMap.SimpleEntry) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) MapVersion(com.demod.fbsr.MapVersion) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandReporting(com.demod.dcba.CommandReporting) DCBA(com.demod.dcba.DCBA) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Utils(com.demod.factorio.Utils) DiscordBot(com.demod.dcba.DiscordBot) LuaValue(org.luaj.vm2.LuaValue) AutoCompleteHandler(com.demod.dcba.AutoCompleteHandler) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) ArrayDeque(java.util.ArrayDeque) BlueprintFinder(com.demod.fbsr.BlueprintFinder) ChunkingFilter(net.dv8tion.jda.api.utils.ChunkingFilter) JSONArray(org.json.JSONArray) DataTable(com.demod.factorio.DataTable) Blueprint(com.demod.fbsr.Blueprint) JSONException(org.json.JSONException) Attachment(net.dv8tion.jda.api.entities.Message.Attachment) BlueprintStringData(com.demod.fbsr.BlueprintStringData) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Attachment (net.dv8tion.jda.api.entities.Message.Attachment)17 IOException (java.io.IOException)14 Message (net.dv8tion.jda.api.entities.Message)13 TextChannel (net.dv8tion.jda.api.entities.TextChannel)12 ArrayList (java.util.ArrayList)11 List (java.util.List)11 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)11 Permission (net.dv8tion.jda.api.Permission)11 Blueprint (com.demod.fbsr.Blueprint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 AutoCompleteHandler (com.demod.dcba.AutoCompleteHandler)9 CommandReporting (com.demod.dcba.CommandReporting)9 DCBA (com.demod.dcba.DCBA)9 DiscordBot (com.demod.dcba.DiscordBot)9 EventReply (com.demod.dcba.EventReply)9 MessageCommandEvent (com.demod.dcba.MessageCommandEvent)9 SlashCommandEvent (com.demod.dcba.SlashCommandEvent)9 SlashCommandHandler (com.demod.dcba.SlashCommandHandler)9 Config (com.demod.factorio.Config)9 DataTable (com.demod.factorio.DataTable)9