local image fetch

This commit is contained in:
Tyler Murphy 2022-10-05 08:50:42 -04:00
parent 302a6c6841
commit 146d1ee7ff
62 changed files with 39 additions and 21 deletions

View file

@ -147,6 +147,7 @@ public class Responder extends ListenerAdapter {
Response response = (Response)temp; Response response = (Response)temp;
reply(response, event.getHook()); reply(response, event.getHook());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
EmbedBuilder builder = Ken.getInstance().getDefaultEmbed() EmbedBuilder builder = Ken.getInstance().getDefaultEmbed()
.setColor(Color.RED) .setColor(Color.RED)
.setTitle(":x: **Error**") .setTitle(":x: **Error**")

View file

@ -4,7 +4,6 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken; import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.api.Request;
import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.command.Response;
import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Command;
import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Option;
@ -33,8 +32,8 @@ public class CrabRave {
} }
private byte[] generateGif(User u) throws IOException, NullPointerException { private byte[] generateGif(User u) throws IOException, NullPointerException {
BufferedImage crab = Ken.getInstance().getImageFetcher().getImage("https://cdn.tylermurphy.net/ken/crab.png", true); BufferedImage crab = Ken.getInstance().getImageFetcher().getCdnImage("crab.png", true);
BufferedImage avatar = Ken.getInstance().getImageFetcher().getImage(u.getAvatarUrl(), false); BufferedImage avatar = Ken.getInstance().getImageFetcher().getRawImage(u.getAvatarUrl(), false);
BufferedImage[] frames = new BufferedImage[26]; BufferedImage[] frames = new BufferedImage[26];
for(int i = 0; i < frames.length; i++) { for(int i = 0; i < frames.length; i++) {
frames[i] = generateFrame(i, avatar, crab); frames[i] = generateFrame(i, avatar, crab);

View file

@ -4,7 +4,6 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken; import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.api.Request;
import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Command;
import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Option;
import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.command.Response;
@ -28,6 +27,7 @@ public class Eject {
byte[] data = generateGif(ejected.getUser()); byte[] data = generateGif(ejected.getUser());
return Response.success(ejected.getEffectiveName() +" was ejected by "+sender.getEffectiveName()).setFile(data, "eject.gif"); return Response.success(ejected.getEffectiveName() +" was ejected by "+sender.getEffectiveName()).setFile(data, "eject.gif");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
return Response.error("Failed to generate gif"); return Response.error("Failed to generate gif");
} }
} }
@ -39,8 +39,8 @@ public class Eject {
else else
message = u.getName() + " was not An Impostor"; message = u.getName() + " was not An Impostor";
BufferedImage space = Ken.getInstance().getImageFetcher().getImage("https://cdn.tylermurphy.net/ken/space.jpg", true); BufferedImage space = Ken.getInstance().getImageFetcher().getCdnImage("space.jpg", true);
BufferedImage avatar = Ken.getInstance().getImageFetcher().getImage(u.getAvatarUrl(), false); BufferedImage avatar = Ken.getInstance().getImageFetcher().getRawImage(u.getAvatarUrl(), false);
BufferedImage[] frames = new BufferedImage[30]; BufferedImage[] frames = new BufferedImage[30];
for (int i = 0; i < frames.length; i++) { for (int i = 0; i < frames.length; i++) {

View file

@ -4,7 +4,6 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken; import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.api.Request;
import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.command.Response;
import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Command;
import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Option;
@ -12,7 +11,6 @@ import net.tylermurphy.ken.util.GifSequenceWriter;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream; import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -34,9 +32,9 @@ public class GayMeme {
} }
private byte[] generateGif(User u) throws IOException, NullPointerException { private byte[] generateGif(User u) throws IOException, NullPointerException {
BufferedImage avatar = Ken.getInstance().getImageFetcher().getImage(u.getAvatarUrl(), false); BufferedImage avatar = Ken.getInstance().getImageFetcher().getRawImage(u.getAvatarUrl(), false);
BufferedImage background = Ken.getInstance().getImageFetcher().getImage("https://cdn.tylermurphy.net/ken/theMoreYouKnow.png", true); BufferedImage background = Ken.getInstance().getImageFetcher().getCdnImage("theMoreYouKnow.png", true);
BufferedImage gay = Ken.getInstance().getImageFetcher().getImage("https://cdn.tylermurphy.net/ken/gay.png", true); BufferedImage gay = Ken.getInstance().getImageFetcher().getCdnImage("gay.png", true);
BufferedImage[] frames = new BufferedImage[40]; BufferedImage[] frames = new BufferedImage[40];
for(int i = 0; i < frames.length; i++) { for(int i = 0; i < frames.length; i++) {

View file

@ -4,7 +4,6 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.tylermurphy.ken.Ken; import net.tylermurphy.ken.Ken;
import net.tylermurphy.ken.api.Request;
import net.tylermurphy.ken.command.Response; import net.tylermurphy.ken.command.Response;
import net.tylermurphy.ken.command.annotation.Command; import net.tylermurphy.ken.command.annotation.Command;
import net.tylermurphy.ken.command.annotation.Option; import net.tylermurphy.ken.command.annotation.Option;
@ -33,8 +32,8 @@ public class Headpat {
} }
private byte[] generateGif(User u) throws IOException, NullPointerException { private byte[] generateGif(User u) throws IOException, NullPointerException {
BufferedImage hand = Ken.getInstance().getImageFetcher().getImage("https://cdn.tylermurphy.net/ken/headpat.png", true); BufferedImage hand = Ken.getInstance().getImageFetcher().getCdnImage("headpat.png", true);
BufferedImage avatar = Ken.getInstance().getImageFetcher().getImage(u.getAvatarUrl(), false); BufferedImage avatar = Ken.getInstance().getImageFetcher().getRawImage(u.getAvatarUrl(), false);
BufferedImage[] frames = new BufferedImage[5]; BufferedImage[] frames = new BufferedImage[5];
for(int i = 0; i < frames.length; i++) { for(int i = 0; i < frames.length; i++) {

View file

@ -32,8 +32,8 @@ public class Wank {
} }
private byte[] generateGif(User u) throws IOException { private byte[] generateGif(User u) throws IOException {
BufferedImage wank = Ken.getInstance().getImageFetcher().getImage("https://cdn.tylermurphy.net/ken/laptop.png", true); BufferedImage wank = Ken.getInstance().getImageFetcher().getCdnImage("laptop.png", true);
BufferedImage avatar = Ken.getInstance().getImageFetcher().getImage(u.getAvatarUrl(), false); BufferedImage avatar = Ken.getInstance().getImageFetcher().getRawImage(u.getAvatarUrl(), false);
BufferedImage[] frames = new BufferedImage[4]; BufferedImage[] frames = new BufferedImage[4];
for (int i = 0; i < frames.length; i++) { for (int i = 0; i < frames.length; i++) {

View file

@ -1,8 +1,11 @@
package net.tylermurphy.ken.util; package net.tylermurphy.ken.util;
import net.tylermurphy.ken.Ken;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
@ -12,7 +15,21 @@ public class ImageFetcher {
private final Map<String,BufferedImage> CACHE = new HashMap<>(); private final Map<String,BufferedImage> CACHE = new HashMap<>();
public BufferedImage getImage(String url, boolean cache) throws IOException { public BufferedImage getCdnImage(String name, boolean cache) throws IOException {
if(Ken.getInstance().getConfig().getBoolean("fetchLocal")) {
if(CACHE.containsKey(name)) return CACHE.get(name);
InputStream in = Ken.getInstance().getResource("cdn/"+name);
if(in == null) return null;
BufferedImage image = ImageIO.read(in);
if(cache && image != null) CACHE.put(name, image);
return image;
} else {
String url = Ken.getInstance().getConfig().getString("cdn") + name;
return getRawImage(url, cache);
}
}
public BufferedImage getRawImage(String url, boolean cache) throws IOException {
if(CACHE.containsKey(url)) return CACHE.get(url); if(CACHE.containsKey(url)) return CACHE.get(url);
HttpURLConnection connection = getConnection(url); HttpURLConnection connection = getConnection(url);
BufferedImage image = ImageIO.read(connection.getInputStream()); BufferedImage image = ImageIO.read(connection.getInputStream());

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 KiB

View file

@ -45,11 +45,15 @@ botName: "Ken"
# Example: "ChFRPDF" # Example: "ChFRPDF"
supportServer: "" supportServer: ""
# Send welcome messages when a player joins the server # This is the cdn that will contain all KenBoReborn images, I host one myself so there is no
welcomeMessages: true # need for you to use your own. If you want, you can use your own. A cdn is required because discord
# requires a url for embeds instead of just uploading an image.
cdn: "https://cdn.tylermurphy.net/ken/"
# Send level up messages when a player levels up # Fetch image url. For the gif generation commands, instead of sending discord a url to a cdn, Ken has to
levelMessages: true # manually pull images to work on them to create the gifs. For most people, you can just keep it as the cdn, and
# have images automatically update. But if you want, you can set images to locally fetch instead from inside the jar.
fetchLocal: false
# The RGB color value of the discord bot. Used for bot theming. # The RGB color value of the discord bot. Used for bot theming.
colors: colors: