Search in sources :

Example 1 with MinecraftServer

use of me.piggypiglet.gary.core.objects.services.MinecraftServer in project Gary by help-chat.

the class ServiceHandler method execute.

@Override
protected void execute(GenericEvent event) {
    GenericGuildMessageEvent e = (GenericGuildMessageEvent) event;
    TextChannel channel = e.getChannel();
    Message message = channel.retrieveMessageById(e.getMessageId()).complete();
    User author = message.getAuthor();
    if (!author.isBot()) {
        FormatScanner scanner = new FormatScanner(message);
        String[] keys = null;
        switch(e.getChannel().getName()) {
            case "request-free":
                keys = new String[] { "service", "request" };
                break;
            case "request-paid":
                keys = new String[] { "service", "request", "budget" };
                break;
            case "rate-my-server":
                if (!scanner.containsKeys("review") && !scanner.containsKeys("name", "description", "ip")) {
                    message.delete().queue();
                    sendError(author, channel, message.getContentRaw());
                }
                if (scanner.containsKeys("name", "description", "ip")) {
                    EmbedBuilder builder = scanner.toEmbed("name", "Description", "IP", "Website");
                    Map<String, String> values = scanner.getValues();
                    MinecraftServer server = new MinecraftServer(values.get("ip"));
                    if (server.isSuccess()) {
                        builder.addField("Extras:", "Premium: " + server.isPremium() + "\nVersion: " + server.getVersion(), false);
                        builder.setFooter("Posted by " + author.getName() + "#" + author.getDiscriminator(), null);
                        if (!server.getFavicon().equalsIgnoreCase("null")) {
                            builder.setThumbnail("attachment://server.png");
                            InputStream stream = new ByteArrayInputStream(Base64.getDecoder().decode(server.getFavicon().replace("\n", "").split(",")[1]));
                            channel.sendFile(stream, "server.png").embed(builder.build()).queue(s -> Arrays.stream(Constants.RATINGS).forEach(em -> s.addReaction(e.getJDA().getEmoteById(em)).queue()));
                            try {
                                stream.close();
                            } catch (Exception e1) {
                                e1.printStackTrace();
                            }
                        } else {
                            channel.sendMessage(builder.build()).queue(s -> Arrays.stream(Constants.RATINGS).forEach(em -> s.addReaction(e.getJDA().getEmoteById(em)).queue()));
                        }
                    } else {
                        String msg = Lang.getString("formats.rate-my-server.server-error", author.getAsMention());
                        MessageUtils.sendMessage(msg, author, channel, msg);
                    }
                    message.delete().queue();
                }
                return;
        }
        if (keys != null && !scanner.containsKeys(keys)) {
            message.delete().queue();
            sendError(author, channel, message.getContentRaw());
        } else {
            serviceBumpHandler.execute(e, keys);
        }
    }
}
Also used : GEvent(me.piggypiglet.gary.core.handlers.GEvent) Message(net.dv8tion.jda.api.entities.Message) GenericGuildMessageEvent(net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) EventsEnum(me.piggypiglet.gary.core.objects.enums.EventsEnum) FormatScanner(me.piggypiglet.gary.core.objects.services.FormatScanner) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) MinecraftServer(me.piggypiglet.gary.core.objects.services.MinecraftServer) User(net.dv8tion.jda.api.entities.User) MessageUtils(me.piggypiglet.gary.core.utils.discord.MessageUtils) Base64(java.util.Base64) ByteArrayInputStream(java.io.ByteArrayInputStream) TimeAPI(sh.okx.timeapi.TimeAPI) Map(java.util.Map) Constants(me.piggypiglet.gary.core.objects.Constants) Lang(me.piggypiglet.gary.core.storage.file.Lang) GenericEvent(net.dv8tion.jda.api.events.GenericEvent) InputStream(java.io.InputStream) User(net.dv8tion.jda.api.entities.User) Message(net.dv8tion.jda.api.entities.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GenericGuildMessageEvent(net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent) FormatScanner(me.piggypiglet.gary.core.objects.services.FormatScanner) MinecraftServer(me.piggypiglet.gary.core.objects.services.MinecraftServer) TextChannel(net.dv8tion.jda.api.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

Inject (com.google.inject.Inject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 Map (java.util.Map)1 GEvent (me.piggypiglet.gary.core.handlers.GEvent)1 Constants (me.piggypiglet.gary.core.objects.Constants)1 EventsEnum (me.piggypiglet.gary.core.objects.enums.EventsEnum)1 FormatScanner (me.piggypiglet.gary.core.objects.services.FormatScanner)1 MinecraftServer (me.piggypiglet.gary.core.objects.services.MinecraftServer)1 Lang (me.piggypiglet.gary.core.storage.file.Lang)1 MessageUtils (me.piggypiglet.gary.core.utils.discord.MessageUtils)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Message (net.dv8tion.jda.api.entities.Message)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 User (net.dv8tion.jda.api.entities.User)1 GenericEvent (net.dv8tion.jda.api.events.GenericEvent)1 GenericGuildMessageEvent (net.dv8tion.jda.api.events.message.guild.GenericGuildMessageEvent)1 TimeAPI (sh.okx.timeapi.TimeAPI)1