Rename service classes to util

java-rewrite
Hammy 3 years ago
parent b83daa7748
commit bef7a2050e

@ -9,8 +9,8 @@ import javax.imageio.ImageIO;
import me.goudham.command.annotation.Option; import me.goudham.command.annotation.Option;
import me.goudham.command.annotation.SlashCommand; import me.goudham.command.annotation.SlashCommand;
import me.goudham.command.annotation.SubCommand; import me.goudham.command.annotation.SubCommand;
import me.goudham.service.EmbedService; import me.goudham.util.EmbedUtil;
import me.goudham.service.ImageService; import me.goudham.util.ImageUtil;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
@ -19,13 +19,13 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
@SlashCommand(name = "avatar") @SlashCommand(name = "avatar")
public class Avatar { public class Avatar {
private final EmbedService embedService; private final EmbedUtil embedUtil;
private final ImageService imageService; private final ImageUtil imageUtil;
@Inject @Inject
public Avatar(EmbedService embedService, ImageService imageService) { public Avatar(EmbedUtil embedUtil, ImageUtil imageUtil) {
this.embedService = embedService; this.embedUtil = embedUtil;
this.imageService = imageService; this.imageUtil = imageUtil;
} }
@Executable @Executable
@ -46,14 +46,14 @@ public class Avatar {
User user = optionalUser == null ? slashCommandEvent.getUser() : optionalUser.getAsUser(); User user = optionalUser == null ? slashCommandEvent.getUser() : optionalUser.getAsUser();
BufferedImage inputImage = ImageIO.read(new URL(user.getEffectiveAvatarUrl())); BufferedImage inputImage = ImageIO.read(new URL(user.getEffectiveAvatarUrl()));
imageService.invertImage(inputImage); imageUtil.invertImage(inputImage);
MessageEmbed avatarEmbed = embedService.getBaseEmbed() MessageEmbed avatarEmbed = embedUtil.getBaseEmbed()
.setAuthor(user.getName() + "'s Avatar\nFilter Applied -> Negative") .setAuthor(user.getName() + "'s Avatar\nFilter Applied -> Negative")
.setImage("attachment://invert.png") .setImage("attachment://invert.png")
.build(); .build();
byte[] imageByteArray = imageService.toByteArray(inputImage, "png"); byte[] imageByteArray = imageUtil.toByteArray(inputImage, "png");
slashCommandEvent.replyEmbeds(avatarEmbed).addFile(imageByteArray, "invert.png").queue(); slashCommandEvent.replyEmbeds(avatarEmbed).addFile(imageByteArray, "invert.png").queue();
} }
@ -75,14 +75,14 @@ public class Avatar {
User user = optionalUser == null ? slashCommandEvent.getUser() : optionalUser.getAsUser(); User user = optionalUser == null ? slashCommandEvent.getUser() : optionalUser.getAsUser();
BufferedImage inputImage = ImageIO.read(new URL(user.getEffectiveAvatarUrl())); BufferedImage inputImage = ImageIO.read(new URL(user.getEffectiveAvatarUrl()));
BufferedImage grayscaleImage = imageService.toGrayscaleImage(inputImage); BufferedImage grayscaleImage = imageUtil.toGrayscaleImage(inputImage);
MessageEmbed avatarEmbed = embedService.getBaseEmbed() MessageEmbed avatarEmbed = embedUtil.getBaseEmbed()
.setAuthor(user.getName() + "'s Avatar\nFilter Applied -> Grayscale") .setAuthor(user.getName() + "'s Avatar\nFilter Applied -> Grayscale")
.setImage("attachment://grayscale.png") .setImage("attachment://grayscale.png")
.build(); .build();
byte[] imageByteArray = imageService.toByteArray(grayscaleImage, "png"); byte[] imageByteArray = imageUtil.toByteArray(grayscaleImage, "png");
slashCommandEvent.replyEmbeds(avatarEmbed).addFile(imageByteArray, "grayscale.png").queue(); slashCommandEvent.replyEmbeds(avatarEmbed).addFile(imageByteArray, "grayscale.png").queue();
} }
@ -103,7 +103,7 @@ public class Avatar {
OptionMapping optionalUser = slashCommandEvent.getOption("member"); OptionMapping optionalUser = slashCommandEvent.getOption("member");
User user = optionalUser == null ? slashCommandEvent.getUser() : optionalUser.getAsUser(); User user = optionalUser == null ? slashCommandEvent.getUser() : optionalUser.getAsUser();
MessageEmbed avatarEmbed = embedService.getBaseEmbed() MessageEmbed avatarEmbed = embedUtil.getBaseEmbed()
.setAuthor(user.getName() + "'s Avatar") .setAuthor(user.getName() + "'s Avatar")
.setImage(user.getEffectiveAvatarUrl() + "?size=4096") .setImage(user.getEffectiveAvatarUrl() + "?size=4096")
.build(); .build();

@ -8,8 +8,8 @@ import me.goudham.command.annotation.Option;
import me.goudham.command.annotation.SlashCommand; import me.goudham.command.annotation.SlashCommand;
import me.goudham.command.annotation.SubCommand; import me.goudham.command.annotation.SubCommand;
import me.goudham.domain.Constants; import me.goudham.domain.Constants;
import me.goudham.service.EmbedService; import me.goudham.util.EmbedUtil;
import me.goudham.service.InfoUtil; import me.goudham.util.InfoUtil;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
@ -31,12 +31,12 @@ import org.jetbrains.annotations.NotNull;
@SlashCommand(name = "info") @SlashCommand(name = "info")
public class Info { public class Info {
private final EmbedService embedService; private final EmbedUtil embedUtil;
private final InfoUtil infoUtil; private final InfoUtil infoUtil;
@Inject @Inject
public Info(EmbedService embedService, InfoUtil infoUtil) { public Info(EmbedUtil embedUtil, InfoUtil infoUtil) {
this.embedService = embedService; this.embedUtil = embedUtil;
this.infoUtil = infoUtil; this.infoUtil = infoUtil;
} }
@ -58,7 +58,7 @@ public class Info {
OptionMapping optionalMember = slashCommandEvent.getOption("member"); OptionMapping optionalMember = slashCommandEvent.getOption("member");
Member member = optionalMember == null ? slashCommandEvent.getMember() : optionalMember.getAsMember(); Member member = optionalMember == null ? slashCommandEvent.getMember() : optionalMember.getAsMember();
MessageEmbed messageEmbed = embedService.getBaseEmbed() MessageEmbed messageEmbed = embedUtil.getBaseEmbed()
.setTitle(infoUtil.getMemberOnlineStatus(member) + " " + member.getUser().getAsTag() + " " + infoUtil.getMemberBadges(member)) .setTitle(infoUtil.getMemberOnlineStatus(member) + " " + member.getUser().getAsTag() + " " + infoUtil.getMemberBadges(member))
.setColor(infoUtil.getMemberColour(member)) .setColor(infoUtil.getMemberColour(member))
.setThumbnail(member.getUser().getEffectiveAvatarUrl() + "?size=4096") .setThumbnail(member.getUser().getEffectiveAvatarUrl() + "?size=4096")
@ -126,7 +126,7 @@ public class Info {
long humanCount = members.stream().filter(member -> !member.getUser().isBot()).count(); long humanCount = members.stream().filter(member -> !member.getUser().isBot()).count();
long botCount = members.stream().filter(member -> member.getUser().isBot()).count(); long botCount = members.stream().filter(member -> member.getUser().isBot()).count();
MessageEmbed messageEmbed = embedService.getBaseEmbed() MessageEmbed messageEmbed = embedUtil.getBaseEmbed()
.setTitle("@" + role.getName() + " Information") .setTitle("@" + role.getName() + " Information")
.setDescription(role.getAsMention() + "\n" + "**Colour:** " + roleColour) .setDescription(role.getAsMention() + "\n" + "**Colour:** " + roleColour)
.setColor(role.getColor()) .setColor(role.getColor())
@ -249,7 +249,7 @@ public class Info {
.replace("$count", boostCount) .replace("$count", boostCount)
.replace("$verifLevel", verificationLevel); .replace("$verifLevel", verificationLevel);
MessageEmbed messageEmbed = embedService.getBaseEmbed() MessageEmbed messageEmbed = embedUtil.getBaseEmbed()
.setTitle("Server Information") .setTitle("Server Information")
.setThumbnail(guild.getIconUrl()) .setThumbnail(guild.getIconUrl())
.addField("Owner", owner, false) .addField("Owner", owner, false)
@ -297,7 +297,7 @@ public class Info {
.replace("$region", region); .replace("$region", region);
return embedService.getBaseEmbed() return embedUtil.getBaseEmbed()
.setTitle("\uD83D\uDD08" + voiceChannel.getName() + " Information") .setTitle("\uD83D\uDD08" + voiceChannel.getName() + " Information")
.setDescription(description) .setDescription(description)
.setThumbnail(voiceChannel.getGuild().getIconUrl()) .setThumbnail(voiceChannel.getGuild().getIconUrl())
@ -333,7 +333,7 @@ public class Info {
.replace("$position", position) .replace("$position", position)
.replace("$topic", topic); .replace("$topic", topic);
return embedService.getBaseEmbed() return embedUtil.getBaseEmbed()
.setTitle("#" + textChannel.getName() + " Information") .setTitle("#" + textChannel.getName() + " Information")
.setDescription(description) .setDescription(description)
.setThumbnail(textChannel.getGuild().getIconUrl()) .setThumbnail(textChannel.getGuild().getIconUrl())

@ -3,25 +3,25 @@ package me.goudham.bot.command.info;
import io.micronaut.context.annotation.Executable; import io.micronaut.context.annotation.Executable;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import me.goudham.command.annotation.SlashCommand; import me.goudham.command.annotation.SlashCommand;
import me.goudham.service.EmbedService; import me.goudham.util.EmbedUtil;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
@SlashCommand(name = "ping", description = "Display the latency of the bot") @SlashCommand(name = "ping", description = "Display the latency of the bot")
public class Ping { public class Ping {
private final EmbedService embedService; private final EmbedUtil embedUtil;
@Inject @Inject
public Ping(EmbedService embedService) { public Ping(EmbedUtil embedUtil) {
this.embedService = embedService; this.embedUtil = embedUtil;
} }
@Executable @Executable
public void handle(SlashCommandEvent slashCommandEvent) { public void handle(SlashCommandEvent slashCommandEvent) {
JDA jda = slashCommandEvent.getJDA(); JDA jda = slashCommandEvent.getJDA();
jda.getRestPing().queue(ping -> { jda.getRestPing().queue(ping -> {
MessageEmbed messageEmbed = embedService.getBaseEmbed() MessageEmbed messageEmbed = embedUtil.getBaseEmbed()
.setDescription("**Rest ping: " + ping + "ms**\n**WS ping: " + jda.getGatewayPing() + "ms**") .setDescription("**Rest ping: " + ping + "ms**\n**WS ping: " + jda.getGatewayPing() + "ms**")
.build(); .build();
slashCommandEvent.replyEmbeds(messageEmbed).queue(); slashCommandEvent.replyEmbeds(messageEmbed).queue();

@ -1,191 +0,0 @@
package me.goudham.service;
import jakarta.inject.Singleton;
import java.awt.Color;
import java.util.List;
import java.util.StringJoiner;
import me.goudham.domain.Constants;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
import net.dv8tion.jda.api.utils.cache.SortedSnowflakeCacheView;
import org.jetbrains.annotations.NotNull;
@Singleton
public class InfoUtil {
public @NotNull String getColorAsHex(@NotNull Color color) {
return "#" + Integer.toHexString(color.getRGB()).toUpperCase();
}
public Color getMemberColour(@NotNull Member member) {
return member.getColor() == null ? Color.BLACK : member.getColor();
}
public @NotNull String getMemberBadges(@NotNull Member member) {
User user = member.getUser();
String memberBadges = "";
if (user.isBot()) memberBadges += Constants.BADGE_BOT;
if (member.getTimeBoosted() != null) memberBadges += Constants.BADGE_SERVER_BOOST;
memberBadges += user.getFlags().toString()
.substring(1, user.getFlags().toString().length() - 1)
.replace(",", "")
.replace("PARTNER", Constants.BADGE_PARTNER)
.replace("HYPESQUAD_BRAVERY", Constants.BADGE_BRAVERY)
.replace("HYPESQUAD_BRILLIANCE", Constants.BADGE_BRILLIANCE)
.replace("HYPESQUAD_BALANCE", Constants.BADGE_BALANCE)
.replace("VERIFIED_DEVELOPER", Constants.BADGE_EARLY_VERIFIED_BOT_DEVELOPER)
.replace("EARLY_SUPPORTER", Constants.BADGE_EARLY_SUPPORTER)
.replace("SYSTEM", Constants.BADGE_STAFF)
.replace("BUG_HUNTER_LEVEL_1", Constants.BADGE_BUG_HUNTER)
.replace("BUG_HUNTER_LEVEL_2", Constants.BADGE_BUG_HUNTER)
.replace("VERIFIED_BOT", Constants.BADGE_VERIFIED_BOT);
return memberBadges;
}
public @NotNull String getMemberOnlineStatus(@NotNull Member member) {
return member.getOnlineStatus().toString()
.replace("ONLINE", Constants.STATUS_ONLINE)
.replace("IDLE", Constants.STATUS_IDLE)
.replace("DO_NOT_DISTURB", Constants.STATUS_DND)
.replace("OFFLINE", Constants.STATUS_OFFLINE)
.replace("STREAMING", Constants.STATUS_STREAMING);
}
public @NotNull String getTopRole(@NotNull Object object) {
String topRole = "";
if (object instanceof Member member) {
List<Role> memberRoles = member.getRoles();
if (memberRoles.isEmpty()) return "No Roles";
topRole = memberRoles.get(0).getAsMention();
} else if (object instanceof Guild guild) {
StringJoiner topRoleJoiner = new StringJoiner("");
guild.getRoleCache().stream()
.findFirst()
.ifPresentOrElse(
role -> topRoleJoiner.add(role.getAsMention()),
() -> topRoleJoiner.setEmptyValue("No Roles")
);
topRole = topRoleJoiner.toString();
}
return topRole;
}
public String getListOfMembers(@NotNull List<Member> members, int limit) {
StringJoiner memberJoiner = new StringJoiner(" **|** ");
if (members.isEmpty()) return "No Members";
members.stream()
.limit(limit)
.forEach(member -> memberJoiner.add(member.getAsMention()));
if (members.size() > limit) {
int leftOverMembers = members.size() - limit;
memberJoiner.add(" and ** " + leftOverMembers + " ** more ");
}
return memberJoiner.toString();
}
public String getGuildEmotes(@NotNull Guild guild, int limit) {
SnowflakeCacheView<Emote> emoteCache = guild.getEmoteCache();
StringJoiner guildEmoteJoiner = new StringJoiner(" ");
if (emoteCache.isEmpty()) {
guildEmoteJoiner.setEmptyValue("No Emojis");
} else {
emoteCache.stream()
.limit(limit)
.forEach(emote -> guildEmoteJoiner.add(emote.getAsMention()));
}
if (emoteCache.size() > limit) {
long leftOverEmojis = emoteCache.size() - limit;
guildEmoteJoiner.add(" and ** " + leftOverEmojis + " ** more");
}
return guildEmoteJoiner.toString();
}
public String getGuildRoles(@NotNull Guild guild, int limit) {
SortedSnowflakeCacheView<Role> roleCache = guild.getRoleCache();
StringJoiner guildRolesJoiner = new StringJoiner(" **|** ");
if (roleCache.isEmpty()) {
guildRolesJoiner.setEmptyValue("No Roles");
} else {
roleCache.stream()
.limit(limit)
.forEach(role -> guildRolesJoiner.add(role.getAsMention()));
}
if (roleCache.size() > limit) {
long leftOverRoles = roleCache.size() - limit;
guildRolesJoiner.add(" and ** " + leftOverRoles + " ** more ");
}
return guildRolesJoiner.toString();
}
public String getMemberRoles(@NotNull Member member, int limit) {
StringJoiner memberRolesJoiner = new StringJoiner(" **|** ");
List<Role> memberRoles = member.getRoles();
if (memberRoles.isEmpty()) return "No Roles";
memberRoles.stream()
.limit(limit)
.forEach(role -> memberRolesJoiner.add(role.getAsMention()));
if (memberRoles.size() > limit) {
int leftOverRoles = memberRoles.size() - limit;
memberRolesJoiner.add(" and ** " + leftOverRoles + " ** more ");
}
return memberRolesJoiner.toString();
}
public long getMemberStatusCount(@NotNull Guild guild, OnlineStatus onlineStatus) {
return guild.getMemberCache().stream()
.filter(member -> member.getOnlineStatus() == onlineStatus)
.count();
}
public long getChannelCount(@NotNull Guild guild, ChannelType channelType) {
return guild.getChannels().stream()
.filter(guildChannel -> guildChannel.getType() == channelType)
.count();
}
public @NotNull String getJoinedDate(@NotNull Member member) {
long timeJoined = member.getTimeJoined().toInstant().getEpochSecond();
return "<t:" + timeJoined + ":F>";
}
public @NotNull String getCreationDate(Object object) {
long time = 0L;
if (object instanceof Role role) {
time = role.getTimeCreated().toInstant().getEpochSecond();
} else if (object instanceof Member member) {
time = member.getTimeCreated().toInstant().getEpochSecond();
} else if (object instanceof Guild guild) {
time = guild.getTimeCreated().toInstant().getEpochSecond();
} else if (object instanceof VoiceChannel voiceChannel) {
time = voiceChannel.getTimeCreated().toInstant().getEpochSecond();
} else if (object instanceof TextChannel textChannel) {
time = textChannel.getTimeCreated().toInstant().getEpochSecond();
}
return "<t:" + time + ":F>";
}
}

@ -1,9 +1,9 @@
package me.goudham.service; package me.goudham.util;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
public interface EmbedService { public interface EmbedUtil {
EmbedBuilder getBaseEmbed(); EmbedBuilder getBaseEmbed();
EmbedBuilder getUserEmbed(Member member); EmbedBuilder getUserEmbed(Member member);
} }

@ -1,4 +1,4 @@
package me.goudham.service; package me.goudham.util;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
import java.awt.Color; import java.awt.Color;
@ -8,7 +8,7 @@ import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
@Singleton @Singleton
public class EnsoEmbedService implements EmbedService { public class EnsoEmbedUtil implements EmbedUtil {
private final Random random = new Random(); private final Random random = new Random();
@Override @Override

@ -1,4 +1,4 @@
package me.goudham.service; package me.goudham.util;
import jakarta.inject.Singleton; import jakarta.inject.Singleton;
import java.awt.Color; import java.awt.Color;
@ -14,7 +14,7 @@ import java.io.InputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@Singleton @Singleton
public class EnsoImageService implements ImageService { public class EnsoImageUtil implements ImageUtil {
@Override @Override
public BufferedImage resizeImage(BufferedImage bufferedImage, int height, int width) { public BufferedImage resizeImage(BufferedImage bufferedImage, int height, int width) {

@ -1,9 +1,9 @@
package me.goudham.service; package me.goudham.util;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
public interface ImageService { public interface ImageUtil {
BufferedImage resizeImage(BufferedImage bufferedImage, int height, int width); BufferedImage resizeImage(BufferedImage bufferedImage, int height, int width);
byte[] toByteArray(BufferedImage bufferedImage, String fileFormat) throws IOException; byte[] toByteArray(BufferedImage bufferedImage, String fileFormat) throws IOException;
BufferedImage toImage(byte[] bytes) throws IOException; BufferedImage toImage(byte[] bytes) throws IOException;
Loading…
Cancel
Save