Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
DxsSucuk committed Oct 26, 2022
1 parent 4e8e4df commit c60f821
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 68 deletions.
6 changes: 3 additions & 3 deletions src/main/java/de/presti/ree6/commands/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void addSlashCommand(JDA jda) {
for (DiscordLocale discordLocale : DiscordLocale.values()) {
if (!LanguageService.languageResources.containsKey(discordLocale)) continue;

String description = LanguageService.getByLocale(discordLocale, command.getClass().getAnnotation(Command.class).description() + "_slash");
String description = LanguageService.getByLocale(discordLocale, command.getClass().getAnnotation(Command.class).description());
if (description.equals("Missing language resource!")) {
description = LanguageService.getByLocale(discordLocale, command.getClass().getAnnotation(Command.class).description());
}
Expand All @@ -105,9 +105,9 @@ public void addSlashCommand(JDA jda) {
}
}

String description = LanguageService.getDefault(command.getClass().getAnnotation(Command.class).description() + "_slash");
String description = LanguageService.getDefault(command.getClass().getAnnotation(Command.class).description());
if (description.equals("Missing language resource!")) {
description = LanguageService.getDefault(command.getClass().getAnnotation(Command.class).description());
description = LanguageService.getDefault(description);
}

if (!description.equals("Missing language resource!")) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/presti/ree6/commands/impl/fun/Cringe.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* A command to let the bot response to the last Message with a cringe Image.
*/
@Command(name = "cringe", description = "Let shrek tell them that their message was cringe!", category = Category.FUN)
@Command(name = "cringe", description = "command.description.cringe", category = Category.FUN)
public class Cringe implements ICommand {

/**
Expand Down
49 changes: 6 additions & 43 deletions src/main/java/de/presti/ree6/sql/SQLWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,7 @@ public void addChatLevelData(String guildId, @Nonnull ChatUserLevel userLevel) {
return;
}

// Check if the User is already saved in the Database.
if (existsInChatLevel(guildId, userLevel.getUserId())) {

// If so change the current XP to the new.
updateEntity(userLevel);
} else {
updateEntity(userLevel);
}
updateEntity(userLevel);
}

/**
Expand Down Expand Up @@ -163,14 +156,7 @@ public void addVoiceLevelData(String guildId, @Nonnull VoiceUserLevel voiceUserL
return;
}

// Check if the User is already saved in the Database.
if (existsInVoiceLevel(guildId, voiceUserLevel.getUserId())) {

// If so change the current XP to the new.
updateEntity(voiceUserLevel);
} else {
updateEntity(voiceUserLevel);
}
updateEntity(voiceUserLevel);
}

/**
Expand Down Expand Up @@ -1157,13 +1143,7 @@ public void removeInvite(String guildId, String inviteCode) {
* @param inviteUsage the Usage count of the Invite.
*/
public void setInvite(String guildId, String inviteCreator, String inviteCode, long inviteUsage) {
// Check if there is an entry with the same data.
if (existsInvite(guildId, inviteCreator, inviteCode)) {
// Update entry.
updateEntity(new Invite(guildId, inviteCreator, inviteUsage, inviteCode));
} else {
updateEntity(new Invite(guildId, inviteCreator, inviteUsage, inviteCode));
}
updateEntity(new Invite(guildId, inviteCreator, inviteUsage, inviteCode));
}

/**
Expand Down Expand Up @@ -1245,14 +1225,7 @@ public String getMessage(String guildId) {
* @param content the Join Message.
*/
public void setMessage(String guildId, String content) {

if (isMessageSetup(guildId)) {
// If there is already an entry just replace it.
updateEntity(new Setting(guildId, "message_join", content));
} else {
// Create a new entry, if there was none.
updateEntity(new Setting(guildId, "message_join", content));
}
updateEntity(new Setting(guildId, "message_join", content));
}

/**
Expand Down Expand Up @@ -1384,13 +1357,7 @@ public void setSetting(String guildId, String settingName, Object settingValue)
// Check if it is null.
if (settingValue == null) createSettings(guildId);

// Check if there is an entry.
if (hasSetting(guildId, settingName)) {
// If so update it.
updateEntity(new Setting(guildId, settingName, settingValue));
} else {
updateEntity(new Setting(guildId, settingName, settingValue));
}
updateEntity(new Setting(guildId, settingName, settingValue));
}

/**
Expand Down Expand Up @@ -1718,11 +1685,7 @@ public void optIn(String guildId, String userId) {
public void addBirthday(String guildId, String channelId, String userId, String birthday) {
try {
BirthdayWish newBirthday = new BirthdayWish(guildId, channelId, userId, new SimpleDateFormat("dd.MM.yyyy").parse(birthday));
if (isBirthdaySaved(guildId, userId)) {
updateEntity(newBirthday);
} else {
updateEntity(newBirthday);
}
updateEntity(newBirthday);
} catch (ParseException ignore) {
}
}
Expand Down
45 changes: 24 additions & 21 deletions src/main/java/de/presti/ree6/utils/data/ImageCreationUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,13 @@ public static byte[] createRankImage(UserLevel userLevel) throws IOException {
log.debug("Finished disposing Graphics2D instance. ({}ms)", System.currentTimeMillis() - actionPerformance);
actionPerformance = System.currentTimeMillis();

// Create a ByteArrayOutputStream to convert the BufferedImage to an Array of Bytes.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

ImageIO.write(base, "PNG", outputStream);
log.debug("Finished writing Image into ByteArrayOutputStream. ({}ms)", System.currentTimeMillis() - actionPerformance);
log.debug("Finished creation in {}ms", System.currentTimeMillis() - start);
byte[] bytes = outputStream.toByteArray();
outputStream.close();
return bytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

ImageIO.write(base, "PNG", outputStream);
log.debug("Finished writing Image into ByteArrayOutputStream. ({}ms)", System.currentTimeMillis() - actionPerformance);
log.debug("Finished creation in {}ms", System.currentTimeMillis() - start);
return outputStream.toByteArray();
}
}

/**
Expand All @@ -249,6 +247,13 @@ public static byte[] createJoinImage(User user, String messageImage, String mess
backgroundImage = joinBackgroundBase.get(messageImage);
} else {
backgroundImage = resize(ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(messageImage))), 1920, 1080);
if (backgroundImage == null) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

ImageIO.write(base, "PNG", outputStream);
return outputStream.toByteArray();
}
}
joinBackgroundBase.put(messageImage, backgroundImage);
}

Expand Down Expand Up @@ -308,13 +313,13 @@ public static byte[] createJoinImage(User user, String messageImage, String mess
log.debug("Finished disposing Graphics2D instance. ({}ms)", System.currentTimeMillis() - actionPerformance);
actionPerformance = System.currentTimeMillis();

// Create a ByteArrayOutputStream to convert the BufferedImage to an Array of Bytes.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

ImageIO.write(base, "PNG", outputStream);
log.debug("Finished writing Image into ByteArrayOutputStream. ({}ms)", System.currentTimeMillis() - actionPerformance);
log.debug("Finished creation in {}ms", System.currentTimeMillis() - start);
return outputStream.toByteArray();
ImageIO.write(base, "PNG", outputStream);
log.debug("Finished writing Image into ByteArrayOutputStream. ({}ms)", System.currentTimeMillis() - actionPerformance);
log.debug("Finished creation in {}ms", System.currentTimeMillis() - start);
return outputStream.toByteArray();
}
}

/**
Expand Down Expand Up @@ -362,13 +367,11 @@ public static byte[] createHornyJailImage(User user) throws IOException {
// Close the Graphics2D instance.
graphics2D.dispose();

// Create a ByteArrayOutputStream to convert the BufferedImage to an Array of Bytes.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {

ImageIO.write(base, "PNG", outputStream);
byte[] bytes = outputStream.toByteArray();
outputStream.close();
return bytes;
ImageIO.write(base, "PNG", outputStream);
return outputStream.toByteArray();
}
}

/**
Expand Down

0 comments on commit c60f821

Please sign in to comment.