Created
September 9, 2018 18:29
-
-
Save karelmikie3/0b8e101222d1eeef247a6b209aedbccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.karelmikie3.wendybot.command; | |
import com.karelmikie3.wendybot.command.api.IGeneralCommand; | |
import net.dv8tion.jda.core.EmbedBuilder; | |
import net.dv8tion.jda.core.entities.Message; | |
import net.dv8tion.jda.core.entities.TextChannel; | |
import net.dv8tion.jda.core.entities.User; | |
import net.dv8tion.jda.core.requests.restaction.MessageAction; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import java.awt.*; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class CommandImage implements IGeneralCommand { | |
private final ImageType type; | |
private final Logger logger; | |
public CommandImage(ImageType type) { | |
this.type = type; | |
this.logger = LogManager.getLogger(this.type.type + "_command"); | |
} | |
@Override | |
public MessageAction execute(TextChannel textChannel, User user, Message message) { | |
try { | |
EmbedBuilder builder = new EmbedBuilder(); | |
builder.setColor(new Color(54, 57, 63)); | |
if (!type.single) { | |
final URL url = new URL("https://images.karelmikie3.com?type=" + type.type); | |
final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setConnectTimeout(5000); | |
connection.getInputStream(); | |
builder.setImage(connection.getURL().toString()); | |
} else { | |
builder.setImage("https://images.karelmikie3.com/images/" + type.type); | |
} | |
return textChannel.sendMessage(builder.build()); | |
} catch (IOException e) { | |
logger.error("Exception in image command", e); | |
} | |
return textChannel.sendMessage("Unable to grab image."); | |
} | |
public enum ImageType { | |
ASTOLFO("astolfo"), | |
BAN("ban"), | |
KAPPA("kappa"), | |
PINCH("pinch"), | |
PLATELET("platelet"), | |
RAIN("rain"), | |
RORY("rory"), | |
TACHIBANA("tachibana"), | |
WENDY("wendy"), | |
ZORO("zoro"), | |
MINE("mine"), | |
LH_CLASSROOM("lh_classroom.jpg", true), | |
PERHAPS("perhaps_desu.jpg", true); | |
final String type; | |
final boolean single; | |
ImageType(String type, boolean single) { | |
this.type = type; | |
this.single = single; | |
} | |
ImageType(String type) { | |
this.type = type; | |
this.single = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment