emote add command with resizing

This commit is contained in:
nora 2021-01-24 17:05:13 +01:00
parent d53715f47a
commit 08b91df73b
16 changed files with 176 additions and 64 deletions

BIN
palm.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 244 KiB

View file

@ -2,6 +2,7 @@ package com.github.nilstrieb.cofig;
import com.github.nilstrieb.util.trivia.TriviaQuestionData;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.Event;
@ -36,13 +37,19 @@ public class Config {
private static final String JSON_PATH_INTELLIJ = "trivia_questions.json";
public static final String JSON_PATH = JSON_PATH_JAR;
private static JDA jda;
public static void setJda(JDA jda) {
Config.jda = jda;
}
public static EmbedBuilder getDefaultEmbed(Event event) {
User killua = event.getJDA().getUserById(THIS_ID);
User killua = jda.getUserById(THIS_ID);
if (killua == null) {
event.getJDA().retrieveUserById(THIS_ID).queue(user -> {
System.out.println("[Config 22] " + user.getAsTag() + " successfully retrieved.");
System.out.println("[Config 43] " + user.getAsTag() + " successfully retrieved.");
});
System.err.println("[Config] This bot user not cached. Retrieving user...");
System.err.println("[Config 45] This bot user not cached. Retrieving user...");
EmbedBuilder builder = new EmbedBuilder();
builder.setColor(Config.DEFAULT_COLOR)

View file

@ -12,7 +12,7 @@ public class DepartureCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
final EmbedBuilder latinBuilder = Config.getDefaultEmbed(event);
latinBuilder.setTitle("DEPARTURE - GALNERYUS")
@ -35,6 +35,6 @@ public class DepartureCommand extends Command {
japaneseBuilder.addField("", DepartureSong.LYRICS_JAPANESE[i], false);
}
reply(event, "\uD83C\uDD70", "\uD83C\uDE35", latinBuilder.build(), japaneseBuilder.build());
reply("\uD83C\uDD70", "\uD83C\uDE35", latinBuilder.build(), japaneseBuilder.build());
}
}

View file

@ -16,14 +16,14 @@ public class QuoteCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
if (args.startsWith("all")) {
reply(event, getQuotesEmbed(event));
reply(getQuotesEmbed(event));
} else {
EmbedBuilder builder = Config.getDefaultEmbed(event)
.addField("Killuas Quotes", KilluaQuotes.getRandomQuote(), false);
reply(event, builder.build());
reply(builder.build());
}
}

View file

@ -8,8 +8,8 @@ public class SayCommand extends Command {
super("say", "Let Killua say something", "say hello gon", "<message>");
}
@Override
public void called(MessageReceivedEvent event, String args) {
reply(event, args);
deleteMsg(event);
public void called(String args) {
reply(args);
deleteMsg();
}
}

View file

@ -28,13 +28,13 @@ public class TriviaCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
if (args.equals("dump") && event.getAuthor().getIdLong() == Config.NILS_ID) {
TriviaQuestionData.dump();
reply(event, "dumped");
reply("dumped");
} else if (args.startsWith("add")) {
reply(event, "Enter the Question (Example: \"What is the name of Gons's father?\")");
reply("Enter the Question (Example: \"What is the name of Gons's father?\")");
new AddSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong());
} else {
int arc = 0;
@ -51,7 +51,7 @@ public class TriviaCommand extends Command {
EmbedBuilder builder = Config.getDefaultEmbed(event)
.addField(question.getQuestion(), answers.toString(), false);
reply(event, builder.build());
reply(builder.build());
new TriviaSection(event.getTextChannel().getIdLong(), event.getAuthor().getIdLong(), question);
}
}
@ -69,8 +69,8 @@ public class TriviaCommand extends Command {
}
@Override
public void messageReceived(MessageReceivedEvent event) {
String msg = event.getMessage().getContentRaw().toLowerCase();
public void called(String text) {
String msg = text.toLowerCase();
String answer;
String correctAnswer = question.getAnswers()[question.getCorrectAnswer()];
@ -95,7 +95,7 @@ public class TriviaCommand extends Command {
if (question.getArc() == TriviaQuestion.EXAM) {
builder.setFooter("Tip: Use " + Config.PREFIX + "help trivia for more questions.");
}
reply(event, builder.build());
reply(builder.build());
dispose();
}
}
@ -126,8 +126,8 @@ public class TriviaCommand extends Command {
}
@Override
public void messageReceived(MessageReceivedEvent event) {
if (event.getAuthor().getIdLong() == Config.NILS_ID && event.getMessage().getContentRaw().startsWith("debug")) {
public void called(String text) {
if (event.getAuthor().getIdLong() == Config.NILS_ID && text.startsWith("debug")) {
answers[0] = "question";
answers[1] = "a;b;c;d";
answers[2] = "0";
@ -136,21 +136,21 @@ public class TriviaCommand extends Command {
} else {
System.out.println(ConsoleColors.BLUE_BOLD + "[TriviaCommand.AddSection 139] Received Next Message: "
+ event.getMessage().getContentRaw() + " status: " + status + ConsoleColors.RESET);
+ text + " status: " + status + ConsoleColors.RESET);
answers[status] = event.getMessage().getContentRaw();
if (status >= 3) {
try {
new TriviaApproval(event, new TriviaQuestion(answers));
reply(event, "Question successfully added for approval");
reply("Question successfully added for approval");
} catch (NumberFormatException e) {
reply(event, "Error: " + e.getMessage());
reply("Error: " + e.getMessage());
}
dispose();
} else {
reply(event, messages[status]);
reply(messages[status]);
}
status++;
deleteMsg(event);
deleteMsg();
}
}
}

View file

@ -63,13 +63,6 @@ public abstract class Command extends MessageSender{
CommandHandler.addCommand(name, this, hidden);
}
/**
* The method called by the CommandHandler
* @param event The event
* @param args The arguments (after the command and an optional whitespace)
*/
public abstract void called(MessageReceivedEvent event, String args);
public String getName() {
return name;
}

View file

@ -34,9 +34,9 @@ public class CommandHandler {
String command = split[0];
System.out.println(ConsoleColors.GREEN + "[CHandler 35] cmd: '" + command + "'" + " args: '" + split[1] + "'" + ConsoleColors.RESET);
if (commands.containsKey(command)) {
commands.get(command).called(event, split[1]);
commands.get(command).onMessageReceived(event, split[1]);
} else if (hiddenCommands.containsKey(command)) {
hiddenCommands.get(command).called(event, split[1]);
hiddenCommands.get(command).onMessageReceived(event, split[1]);
}
}
}

View file

@ -9,31 +9,46 @@ import java.util.TimerTask;
public abstract class MessageSender {
protected void reply(MessageReceivedEvent event, String message) {
protected MessageReceivedEvent event;
public void onMessageReceived(MessageReceivedEvent event, String args){
this.event = event;
called(args);
}
/**
* The method called by the CommandHandler
* @param args The arguments (after the command and an optional whitespace)
*/
public abstract void called(String args);
protected void reply(String message) {
if (!message.equals("")) {
event.getTextChannel().sendMessage(message).queue();
}
}
protected void reply(MessageReceivedEvent event, MessageEmbed embed) {
protected void reply(MessageEmbed embed) {
if (!embed.isEmpty()) {
event.getTextChannel().sendMessage(embed).queue();
}
}
protected void reply(MessageReceivedEvent event, MessageEmbed... embeds) {
protected void reply(MessageEmbed... embeds) {
if (!embeds[0].isEmpty()) {
new MultiPageEmbed(event, embeds);
}
}
protected void reply(MessageReceivedEvent event, String emote1, String emote2, MessageEmbed... embeds) {
protected void reply(String emote1, String emote2, MessageEmbed... embeds) {
if (!embeds[0].isEmpty()) {
new MultiPageEmbed(event, emote1, emote2, embeds);
}
}
protected void deleteMsg(MessageReceivedEvent event, long delay) {
protected void deleteMsg(long delay) {
new Timer().schedule(
new TimerTask() {
@Override
@ -44,7 +59,7 @@ public abstract class MessageSender {
);
}
protected void deleteMsg(MessageReceivedEvent event) {
protected void deleteMsg() {
event.getMessage().delete().queue();
}
}

View file

@ -18,9 +18,9 @@ public class EvalCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
if (args.startsWith("event.getJDA().getToken()")) {
reply(event, "ODAxDKE1MLU0UDOzNzk4ODI1.YAaYOg.u.MEQ_2bzQkVVZ5y1J5Q23Se5CU");
reply("ODAxDKE1MLU0UDOzNzk4ODI1.YAaYOg.u.MEQ_2bzQkVVZ5y1J5Q23Se5CU");
} else if (event.getAuthor().getIdLong() == Config.NILS_ID || event.getAuthor().getIdLong() == Config.YUKI_ID) {
if (args.startsWith("help")) {
@ -31,9 +31,9 @@ public class EvalCommand extends Command {
.addField("triviaset", "Set JSON. Make sure to backup the JSON beforehand with `triviadump`", false)
.addField("reloadtrivia", "Reload the new trivia File", false)
.addField("jar", "Upload a new jar file", false);
reply(event, builder.build());
reply(builder.build());
} else if (args.startsWith("shutdown")) {
reply(event, "Shutting down KilluaBot...");
reply("Shutting down KilluaBot...");
System.exit(0);
} else if (args.startsWith("triviadump")) {
File f = TriviaQuestionData.getFile();
@ -43,11 +43,11 @@ public class EvalCommand extends Command {
if (attachments.size() > 0) {
attachments.get(0).downloadToFile(TriviaQuestionData.getFile());
} else {
reply(event, "JSON File not found");
reply("JSON File not found");
}
} else if (args.startsWith("reloadtrivia")) {
TriviaQuestionData.reload();
reply(event, "Reloaded Trivia Questions");
reply("Reloaded Trivia Questions");
} else if (args.startsWith("jar")) {
List<Message.Attachment> attachments = event.getMessage().getAttachments();
if (attachments.size() > 0) {
@ -55,16 +55,16 @@ public class EvalCommand extends Command {
attachments.get(0).downloadToFile(
new File(EvalCommand.class.getProtectionDomain().getCodeSource()
.getLocation().toURI()));
reply(event, "Downloaded jar file");
reply("Downloaded jar file");
} catch (URISyntaxException e) {
reply(event, "Error: " + e.getMessage());
reply("Error: " + e.getMessage());
}
} else {
reply(event, "JSON File not found");
reply("JSON File not found");
}
}
} else {
reply(event, "no eval for you");
reply("no eval for you");
}
}
}

View file

@ -2,12 +2,7 @@ package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.commands.handler.Command;
import com.github.nilstrieb.commands.handler.CommandHandler;
import com.github.nilstrieb.util.MultiPageEmbed;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.List;
public class HelpCommand extends Command {
public HelpCommand() {
@ -15,18 +10,18 @@ public class HelpCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
if(args.length() == 0) {
if (CommandHandler.commandAmount() > CommandHandler.MAX_PAGE_LENGTH) {
reply(event, CommandHandler.getHelpLists(event));
reply(CommandHandler.getHelpLists(event));
} else {
reply(event, CommandHandler.getHelpList(event).build());
reply(CommandHandler.getHelpList(event).build());
}
} else {
MessageEmbed help = CommandHandler.getCommandHelp(event, args.split(" ")[0]);
if (help != null) {
reply(event, help);
reply(help);
}
}
}

View file

@ -15,14 +15,14 @@ public class InviteCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
event.getJDA().retrieveUserById(Config.NILS_ID).queue(nils -> {
EmbedBuilder builder = Config.getDefaultEmbed(event)
.setTitle("Invite Killua to your server!")
.addField("Invite link", "[Invite]" + INVITE_LINK, true)
.setFooter("This bot was made by " + nils.getAsTag(), nils.getAvatarUrl());
reply(event, builder.build());
reply(builder.build());
});

View file

@ -3,7 +3,6 @@ package com.github.nilstrieb.commands.info;
import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.commands.handler.Command;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class ToukaCommand extends Command {
@ -16,7 +15,7 @@ public class ToukaCommand extends Command {
}
@Override
public void called(MessageReceivedEvent event, String args) {
public void called(String args) {
event.getJDA().retrieveUserById(Config.YUKI_ID).queue(yuki -> {
event.getJDA().retrieveUserById(783720725848129566L).queue(touka -> {
EmbedBuilder builder = Config.getDefaultEmbed(event)
@ -24,7 +23,7 @@ public class ToukaCommand extends Command {
.setFooter("The Touka bot was made by " + yuki.getAsTag(), yuki.getAvatarUrl())
.setThumbnail(touka.getAvatarUrl())
.addField("Invite link", "[Invite]" + TOUKA_INVITE, false);
event.getTextChannel().sendMessage(builder.build()).queue();
reply(builder.build());
});
});

View file

@ -0,0 +1,91 @@
package com.github.nilstrieb.commands.util;
import com.github.nilstrieb.commands.handler.Command;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.List;
public class EmoteAddCommand extends Command {
private static final long MAX_EMOTE_SIZE = 258000;
private static final int DEFAULT_SIZE = 400;
public EmoteAddCommand() {
super("emote", "Add a new image as an emote, gets rescaled automatically.",
"emote Killua (with an attached image of killua)", "<emote name>",
"Add a new emote to your server using this command. \nIf the image is too big for discord, Killua will automatically resize it for you!");
}
@Override
public void called(String args) {
List<Message.Attachment> attachments = event.getMessage().getAttachments();
Member author = event.getGuild().getMember(event.getAuthor());
if (author == null || !author.getPermissions().contains(Permission.MANAGE_EMOTES)) {
reply("You don't have the permissions to do that.");
} else if (attachments.size() == 0 || !attachments.get(0).isImage()) {
reply("No image attached");
} else if (args.length() < 3) {
reply("Name must be at least 3 characters: " + args);
} else {
try {
Message.Attachment image = attachments.get(0);
byte[] bytes = readImage(image);
if (bytes.length > MAX_EMOTE_SIZE) {
bytes = resizeImage(bytes, image.getFileExtension(), DEFAULT_SIZE);
}
Icon icon = Icon.from(bytes);
event.getGuild().createEmote(args, icon).queue(emote -> reply("Successfully added emote: " + emote.getAsMention()));
} catch (IOException e) {
reply("Error while reading image. Please try again.");
}
}
}
private byte[] readImage(Message.Attachment image) throws IOException {
String urlString = image.getUrl();
URL url = new URL(urlString);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] chunk = new byte[4096];
int bytesRead;
InputStream stream = url.openStream();
while ((bytesRead = stream.read(chunk)) > 0) {
out.write(chunk, 0, bytesRead);
}
return out.toByteArray();
}
private byte[] resizeImage(byte[] bytes, String format, int size) throws IOException {
reply("Image size too big (" + bytes.length / 1000 + "kB). Resizing image...");
Image image = ImageIO.read(new ByteArrayInputStream(bytes));
image = image.getScaledInstance(size, size, Image.SCALE_SMOOTH);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage bufferedImg = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bufferedImg.createGraphics();
bGr.drawImage(image, 0, 0, null);
bGr.dispose();
ImageIO.write(bufferedImg, format, out);
bytes = out.toByteArray();
if(bytes.length > MAX_EMOTE_SIZE){
return resizeImage(bytes, format, size - 100);
} else {
return bytes;
}
}
}

View file

@ -1,5 +1,6 @@
package com.github.nilstrieb.core;
import com.github.nilstrieb.cofig.Config;
import com.github.nilstrieb.cofig.Secrets;
//import com.github.nilstrieb.commands.fun.DepartureCommand;
import com.github.nilstrieb.commands.fun.DepartureCommand;
@ -10,10 +11,12 @@ import com.github.nilstrieb.commands.info.InviteCommand;
import com.github.nilstrieb.commands.info.EvalCommand;
import com.github.nilstrieb.commands.info.HelpCommand;
import com.github.nilstrieb.commands.info.ToukaCommand;
import com.github.nilstrieb.commands.util.EmoteAddCommand;
import com.github.nilstrieb.listener.ChannelMessageListener;
import com.github.nilstrieb.listener.CommandListener;
import com.github.nilstrieb.listener.ReactionEventListener;
import com.github.nilstrieb.listener.StartUpListener;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
@ -35,6 +38,7 @@ public class Main {
JDA jda = builder.build();
setupCommands();
Config.setJda(jda);
Thread t = new Thread(() -> {
Scanner scanner = new Scanner(System.in);
@ -73,5 +77,6 @@ public class Main {
new QuoteCommand();
new DepartureCommand();
new TriviaCommand();
new EmoteAddCommand();
}
}

View file

@ -1,6 +1,7 @@
package com.github.nilstrieb.sections;
import com.github.nilstrieb.commands.handler.MessageSender;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public abstract class Section extends MessageSender implements ChannelListener{
private final long textChannelID;
@ -13,6 +14,12 @@ public abstract class Section extends MessageSender implements ChannelListener{
ChannelMessageEventManager.addListener(this, textChannelID);
}
@Override
public void messageReceived(MessageReceivedEvent event) {
this.event = event;
called(event.getMessage().getContentRaw());
}
public Section(long textChannelID) {
this.textChannelID = textChannelID;
this.userID = 0;