diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/SamQuotesCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/SamQuotesCommand.java index 2a43c35f..696cd329 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/SamQuotesCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/SamQuotesCommand.java @@ -1,16 +1,26 @@ package com.diamondfire.helpbot.bot.command.impl.other; import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; +import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.SingleArgumentContainer; +import com.diamondfire.helpbot.bot.command.argument.impl.types.DefinedObjectArgument; import com.diamondfire.helpbot.bot.command.help.*; import com.diamondfire.helpbot.bot.command.impl.Command; import com.diamondfire.helpbot.bot.command.permissions.Permission; +import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; +import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; import com.diamondfire.helpbot.bot.events.CommandEvent; import com.diamondfire.helpbot.sys.externalfile.ExternalFiles; +import com.diamondfire.helpbot.util.IOUtil; import net.dv8tion.jda.api.EmbedBuilder; +import javax.imageio.ImageIO; import java.awt.*; -import java.io.File; -import java.util.Random; +import java.awt.geom.Ellipse2D; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.*; + +import static com.diamondfire.helpbot.util.textgen.MarkovManipulation.getNextWord; public class SamQuotesCommand extends Command { @@ -28,11 +38,6 @@ public HelpContext getHelpContext() { .category(CommandCategory.OTHER); } - @Override - public ArgumentSet compileArguments() { - return new ArgumentSet(); - } - @Override public Permission getPermission() { return Permission.USER; @@ -40,16 +45,379 @@ public Permission getPermission() { @Override public void run(CommandEvent event) { - String[] strings = ExternalFiles.SAM_DIR.list(); - File file = new File(ExternalFiles.SAM_DIR, strings[random.nextInt(strings.length)]); - EmbedBuilder builder = new EmbedBuilder(); - builder.setTitle("Sam Quote"); - builder.setImage("attachment://quote.png"); - builder.setColor(new Color(87, 177, 71)); - event.getChannel().sendMessage(builder.build()).addFile(file, "quote.png").queue(); + if (event.getArgument("action") == "submit") { + + String[] message = event.getMessage().getContentRaw().split("/"); + + System.out.println(message.length); + + if(message.length != 7 || message[5] == null || message[6] == null || message[5].length() != 18 || message[6].length() != 18) { + + PresetBuilder error = new PresetBuilder(); + + error.withPreset( + new InformativeReply(InformativeReplyType.ERROR, "This is not a samquote!") + ); + + return; + } + + long channelID = Long.parseLong(message[5]); + long messageID = Long.parseLong(message[6]); + + event.getGuild().getTextChannelById(channelID).retrieveMessageById(messageID).queue((messageText) -> { + + if(messageText.getAuthor().getIdLong() == 132092551782989824L) { + + String samProfilePic = messageText.getAuthor().getAvatarUrl(); + + try { + + BufferedImage samPfp = ImageIO.read(IOUtil.getFileFromSite(samProfilePic, "sampfp.png")); + + String text = " " + messageText.getContentRaw().replaceAll("[^a-zA-Z0-9 ]", ""); + + int pfpWidth = samPfp.getWidth() * 10; + + //crop pfp into a circle + + BufferedImage circleBuffer = new BufferedImage(pfpWidth, pfpWidth, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2 = circleBuffer.createGraphics(); + + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + g2.setClip(new Ellipse2D.Float(0, 0, pfpWidth, pfpWidth)); + g2.drawImage(samPfp, 0, 0, pfpWidth, pfpWidth, null); + + //convert text into image + + BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2d = img.createGraphics(); + + Font font = new Font("Whitney", Font.PLAIN, 140); + g2d.setFont(font); + FontMetrics fm = g2d.getFontMetrics(); + + int width = fm.stringWidth(text); + int height = fm.getHeight() * 2; + + g2d.dispose(); + + img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + + g2d = img.createGraphics(); + + g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); + g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); + g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); + + g2d.setFont(font); + g2d.setColor(Color.WHITE); + g2d.drawString(text, 0, fm.getAscent() * 2.2f); + + g2d.dispose(); + + //convert SamMan_ into image + + BufferedImage img2 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2d2 = img2.createGraphics(); + + Font font2 = new Font("Whitney", Font.BOLD, 140); + g2d2.setFont(font2); + FontMetrics fm2 = g2d2.getFontMetrics(); + + int width2 = fm2.stringWidth(" SamMan_"); + int height2 = fm2.getHeight(); + + g2d2.dispose(); + + img2 = new BufferedImage(width2, height2, BufferedImage.TYPE_INT_ARGB); + + g2d2 = img2.createGraphics(); + + g2d2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); + g2d2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); + g2d2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); + g2d2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); + g2d2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2d2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2d2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); + + g2d2.setFont(font); + Color nameColor = new Color(45, 102, 212); + g2d2.setColor(nameColor); + g2d2.drawString(" SamMan_", 0, fm2.getAscent()); + + g2d2.dispose(); + + //create background image + + BufferedImage combined = new BufferedImage(Math.max(img.getWidth(), img2.getWidth()) + 10, img.getHeight() + 10, BufferedImage.TYPE_INT_ARGB); + + Graphics g = combined.getGraphics(); + + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + Color bgColor = new Color(54,57,63); + + BufferedImage bg = new BufferedImage(Math.max(img.getWidth(), img2.getWidth()) + 30, height + 30, BufferedImage.TYPE_INT_ARGB); + Graphics2D bgg = bg.createGraphics(); + bgg.setPaint (bgColor); + bgg.fillRect (0, 0, bg.getWidth(), bg.getHeight()); + bgg.dispose(); + + //combine images + + g.drawImage(bg, 0, 0, bgColor, null); + g.drawImage(img, 5, 5, bgColor, null); + g.drawImage(img2, 5, 5, bgColor, null); + g.drawImage(circleBuffer, 5, 5, height, height, bgColor, null); + + //save image + + File imageFile = new File(ExternalFiles.SAM_DIR, messageText.getContentRaw().replaceAll("[^a-zA-Z0-9]", "") + ".png"); + ImageIO.write(combined, "PNG", imageFile); + + PresetBuilder success = new PresetBuilder(); + + success.withPreset( + new InformativeReply(InformativeReplyType.SUCCESS, "Your SamQuote has been added!") + ); + + } catch (IOException e) { + + e.printStackTrace(); + + } + + } else { + + PresetBuilder error = new PresetBuilder(); + + error.withPreset( + new InformativeReply(InformativeReplyType.ERROR, "This is not a samquote!") + ); + + } + + }); + } else if (event.getArgument("action") == "generate") { + + File file = new File("samquotes.txt"); + + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + ArrayList startingTexts = new ArrayList<>(); + + String line = null; + while (true) { + try { + if ((line = br.readLine()) == null) break; + } catch (IOException e) { + e.printStackTrace(); + } + + startingTexts.add(line.split(" ")[0]); + } + + String word = startingTexts.get((int) Math.rint(Math.random() * startingTexts.size())); + + String string = ""; + for (int i = 0; i < 50; i++) { + + if (word == null || word.equals(".")) break; + string += word + " "; + try { + word = getNextWord(word); + } catch (IOException e) { + e.printStackTrace(); + } + } + + BufferedImage samPfp = null; + try { + samPfp = ImageIO.read(IOUtil.getFileFromSite("https://cdn.discordapp.com/avatars/132092551782989824/14368bddad31d5fb5501acc8338a8ab4.png?size=1024", "sampfp.png")); + } catch (IOException e) { + e.printStackTrace(); + } + + String text = " " + string; + + int pfpWidth = samPfp.getWidth() * 10; + + //crop pfp into a circle + + BufferedImage circleBuffer = new BufferedImage(pfpWidth, pfpWidth, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2 = circleBuffer.createGraphics(); + + g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + g2.setClip(new Ellipse2D.Float(0, 0, pfpWidth, pfpWidth)); + g2.drawImage(samPfp, 0, 0, pfpWidth, pfpWidth, null); + + //convert text into image + + BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2d = img.createGraphics(); + + Font font = new Font("Whitney", Font.PLAIN, 140); + g2d.setFont(font); + FontMetrics fm = g2d.getFontMetrics(); + + int width = fm.stringWidth(text); + int height = fm.getHeight() * 2; + + g2d.dispose(); + + img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + + g2d = img.createGraphics(); + + g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); + g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); + g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); + + g2d.setFont(font); + g2d.setColor(Color.WHITE); + g2d.drawString(text, 0, fm.getAscent() * 2.2f); + + g2d.dispose(); + + //convert SamMan_ into image + + BufferedImage img2 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + + Graphics2D g2d2 = img2.createGraphics(); + + Font font2 = new Font("Whitney", Font.BOLD, 140); + g2d2.setFont(font2); + FontMetrics fm2 = g2d2.getFontMetrics(); + + int width2 = fm2.stringWidth(" SamMan_"); + int height2 = fm2.getHeight(); + + g2d2.dispose(); + + img2 = new BufferedImage(width2, height2, BufferedImage.TYPE_INT_ARGB); + + g2d2 = img2.createGraphics(); + + g2d2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); + g2d2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); + g2d2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); + g2d2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); + g2d2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2d2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2d2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); + + g2d2.setFont(font); + + double colorChance = Math.random(); + + + Color nameColor; + + if (colorChance < 0.1) { + + nameColor = new Color(255, 153, 227); + + } else if (colorChance < 0.3) { + + nameColor = new Color(255, 225, 76); + + } else if (colorChance < 0.6) { + + nameColor = new Color(35, 255, 38); + + } else { + + nameColor = new Color(45, 102, 212); + } + + g2d2.setColor(nameColor); + g2d2.drawString(" SamMan_", 0, fm2.getAscent()); + + g2d2.dispose(); + + //create background image + + BufferedImage combined = new BufferedImage(Math.max(img.getWidth(), img2.getWidth()) + 10, img.getHeight() + 10, BufferedImage.TYPE_INT_ARGB); + + Graphics g = combined.getGraphics(); + + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + Color bgColor = new Color(54,57,63); + + BufferedImage bg = new BufferedImage(Math.max(img.getWidth(), img2.getWidth()) + 30, height + 30, BufferedImage.TYPE_INT_ARGB); + Graphics2D bgg = bg.createGraphics(); + bgg.setPaint (bgColor); + bgg.fillRect (0, 0, bg.getWidth(), bg.getHeight()); + bgg.dispose(); + + //combine images + + g.drawImage(bg, 0, 0, bgColor, null); + g.drawImage(img, 5, 5, bgColor, null); + g.drawImage(img2, 5, 5, bgColor, null); + g.drawImage(circleBuffer, 5, 5, height, height, bgColor, null); + + File samQuote = new File(ExternalFiles.IMAGES_DIR, "quote.png"); + try { + ImageIO.write(combined, "PNG", samQuote); + } catch (IOException e) { + e.printStackTrace(); + } + EmbedBuilder builder = new EmbedBuilder(); + builder.setTitle("Sam Quote"); + builder.setImage("attachment://quote.png"); + builder.setColor(new Color(87, 177, 71)); + + event.getChannel().sendMessage(builder.build()).addFile(samQuote, "quote.png").queue(); + + } else { + + String[] strings = ExternalFiles.SAM_DIR.list(); + File file = new File(ExternalFiles.SAM_DIR, strings[random.nextInt(strings.length)]); + EmbedBuilder builder = new EmbedBuilder(); + builder.setTitle("Sam Quote"); + builder.setImage("attachment://quote.png"); + builder.setColor(new Color(87, 177, 71)); + + event.getChannel().sendMessage(builder.build()).addFile(file, "quote.png").queue(); + + } + } -} - - + @Override + public ArgumentSet compileArguments() { + return new ArgumentSet() + .addArgument("action", + new SingleArgumentContainer<>(new DefinedObjectArgument<>("submit", "generate")).optional(null)); + } + +} \ No newline at end of file diff --git a/src/main/java/com/diamondfire/helpbot/util/textgen/CacheData.java b/src/main/java/com/diamondfire/helpbot/util/textgen/CacheData.java new file mode 100644 index 00000000..c016036e --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/textgen/CacheData.java @@ -0,0 +1,39 @@ +package com.diamondfire.helpbot.util.textgen; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; + +import static com.diamondfire.helpbot.util.textgen.MarkovManipulation.addWord; + +public class CacheData { + + public static void CacheData() throws IOException { + + File file = new File("samquotes.txt"); + BufferedReader br = new BufferedReader(new FileReader(file)); + + FileWriter fileWriter = new FileWriter("markov.txt"); + fileWriter.write(""); + fileWriter.close(); + + String line; + while ((line = br.readLine()) != null) { + + ArrayList splitLine = new ArrayList<>(); + splitLine.addAll(Arrays.asList(line.split(" "))); + + for (int i = 0; i < splitLine.size(); i++) { + + if (i == splitLine.size() - 1) { + + addWord(splitLine.get(i), "."); + + } else { + + addWord(splitLine.get(i), splitLine.get(i + 1)); + } + } + } + } +} diff --git a/src/main/java/com/diamondfire/helpbot/util/textgen/MarkovManipulation.java b/src/main/java/com/diamondfire/helpbot/util/textgen/MarkovManipulation.java new file mode 100644 index 00000000..0c8100a5 --- /dev/null +++ b/src/main/java/com/diamondfire/helpbot/util/textgen/MarkovManipulation.java @@ -0,0 +1,113 @@ +package com.diamondfire.helpbot.util.textgen; + +import java.io.*; +import java.util.ArrayList; + +public class MarkovManipulation { + + public static String getNextWord(String word) throws IOException { + + File file = new File("markov.txt"); + BufferedReader br = new BufferedReader(new FileReader(file)); + + float num = (float) Math.random(); + float currentNum = 0f; + + String line; + String[] split; + String[] split2; + while ((line = br.readLine()) != null) { + + split = line.split("//"); + if (split[0].split("/")[0].equals(word)) { + + for (int i = 1; i < split.length; i++) { + + split2 = split[i].split("/"); + currentNum += Float.parseFloat(split2[1]); + + if (currentNum >= num) { + + return split2[0]; + } + } + } + } + + return null; + } + + public static void addWord(String word, String word2) throws IOException { + + File file = new File("markov.txt"); + BufferedReader br = new BufferedReader(new FileReader(file)); + + ArrayList newFile = new ArrayList<>(); + String newLine; + float newChance; + int newAmount; + + String line; + String[] split; + String[] split2; + + boolean addNewWord = true; + boolean addNewLine = true; + + while ((line = br.readLine()) != null) { + + split = line.split("//"); + + String wordTest = split[0].split("/")[0]; + + newAmount = Integer.parseInt(split[0].split("/")[1]) + 1; + + if (wordTest.equals(word)) { + + newLine = split[0].split("/")[0] + "/" + newAmount; + + for (int i = 1; i < split.length; i++) { + + split2 = split[i].split("/"); + + if (split2[0].equals(word2)) { + + newChance = (Float.parseFloat(split[0].split("/")[1]) * Float.parseFloat(split2[1]) + 1) / newAmount; + newLine += "//" + split2[0] + "/" + newChance; + + addNewWord = false; + + } else { + + newChance = (Float.parseFloat(split[0].split("/")[1]) * Float.parseFloat(split2[1])) / newAmount; + newLine += "//" + split2[0] + "/" + newChance; + } + } + + if (addNewWord) { + + newLine += "//" + word2 + "/" + (1f / newAmount); + } + + newFile.add(newLine); + + addNewLine = false; + + } else { + + newFile.add(line); + } + } + + if (addNewLine) { + + newFile.add(word + "/1//" + word2 + "/1"); + } + + String newFileString = String.join("\n", newFile); + + FileWriter fileWriter = new FileWriter("markov.txt"); + fileWriter.write(newFileString); + fileWriter.close(); + } +}