Add channel info command

java-rewrite
Hammy 3 years ago
parent 6cd4dd6c4d
commit 817337e9c6

@ -2,21 +2,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 java.awt.Color;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.StringJoiner;
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.domain.Constants; import me.goudham.domain.Constants;
import me.goudham.service.EmbedService; import me.goudham.service.EmbedService;
import me.goudham.service.InfoUtil;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.Region;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.OptionType;
@ -26,10 +30,12 @@ import org.jetbrains.annotations.NotNull;
@SlashCommand(name = "info") @SlashCommand(name = "info")
public class Info { public class Info {
private final EmbedService embedService; private final EmbedService embedService;
private final InfoUtil infoUtil;
@Inject @Inject
public Info(EmbedService embedService) { public Info(EmbedService embedService, InfoUtil infoUtil) {
this.embedService = embedService; this.embedService = embedService;
this.infoUtil = infoUtil;
} }
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
@ -51,13 +57,13 @@ public class Info {
Member member = optionalMember == null ? slashCommandEvent.getMember() : optionalMember.getAsMember(); Member member = optionalMember == null ? slashCommandEvent.getMember() : optionalMember.getAsMember();
MessageEmbed messageEmbed = embedService.getBaseEmbed() MessageEmbed messageEmbed = embedService.getBaseEmbed()
.setTitle(getMemberOnlineStatus(member) + " " + member.getUser().getAsTag() + " " + getMemberBadges(member)) .setTitle(infoUtil.getMemberOnlineStatus(member) + " " + member.getUser().getAsTag() + " " + infoUtil.getMemberBadges(member))
.setColor(getMemberColour(member)) .setColor(infoUtil.getMemberColour(member))
.setThumbnail(member.getUser().getEffectiveAvatarUrl() + "?size=4096") .setThumbnail(member.getUser().getEffectiveAvatarUrl() + "?size=4096")
.addField("Registered", getRegisteredDate(member), false) .addField("Registered", infoUtil.getCreationDate(member), false)
.addField("Joined", getJoinedDate(member), false) .addField("Joined", infoUtil.getJoinedDate(member), false)
.addField("Top Role", getTopRole(member), false) .addField("Top Role", infoUtil.getTopRole(member), false)
.addField("Roles (" + member.getRoles().size() + ")", getMemberRoles(member, 20), false) .addField("Roles (" + member.getRoles().size() + ")", infoUtil.getMemberRoles(member, 20), false)
.setFooter("ID: " + member.getId()) .setFooter("ID: " + member.getId())
.build(); .build();
@ -97,14 +103,19 @@ public class Info {
} }
Task<List<Member>> membersWithRoles = guild.findMembersWithRoles(role); Task<List<Member>> membersWithRoles = guild.findMembersWithRoles(role);
String roleColour = role.getColor() == null ? "N/A" : getColorAsHex(role.getColor()); String roleColour = role.getColor() == null ? "N/A" : infoUtil.getColorAsHex(role.getColor());
String isMentionable = role.isMentionable() ? Constants.CHECK : Constants.CROSS; String isMentionable = role.isMentionable() ? Constants.CHECK : Constants.CROSS;
String isHoisted = role.isHoisted() ? Constants.CHECK : Constants.CROSS; String isHoisted = role.isHoisted() ? Constants.CHECK : Constants.CROSS;
String isManaged = role.isManaged() ? Constants.CHECK : Constants.CROSS; String isManaged = role.isManaged() ? Constants.CHECK : Constants.CROSS;
String miscString = "Mentionable: " + isMentionable String miscString = """
+ "\n" + "Hoisted: " + isHoisted Mentionable: $isMentionable
+ "\n" + "Managed: " + isManaged; Hoisted: $isHoisted
Managed: $isManaged
"""
.replace("$isMentionable", isMentionable)
.replace("$isHoisted", isHoisted)
.replace("$isManaged", isManaged);
EnumSet<Permission> permissions = role.getPermissions(slashCommandEvent.getGuildChannel()); EnumSet<Permission> permissions = role.getPermissions(slashCommandEvent.getGuildChannel());
System.out.println(permissions); System.out.println(permissions);
@ -114,14 +125,14 @@ public class Info {
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 = embedService.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())
.setThumbnail(guild.getIconUrl() + "?size=4096") .setThumbnail(guild.getIconUrl() + "?size=4096")
.addField("Creation At", getCreationDate(role), false) .addField("Creation At", infoUtil.getCreationDate(role), false)
.addField("Members (" + members.size() + ")", "Humans: " + humanCount + "\nBots: " + botCount, true) .addField("Members (" + members.size() + ")", "Humans: " + humanCount + "\nBots: " + botCount, true)
.addField("Misc", miscString, true) .addField("Misc", miscString, true)
.addField("List of Members (" + members.size() + ")", getListOfMembers(members, 20), false) .addField("List of Members (" + members.size() + ")", infoUtil.getListOfMembers(members, 20), false)
.setFooter("ID: " + role.getId()) .setFooter("ID: " + role.getId())
.build(); .build();
@ -129,99 +140,131 @@ public class Info {
}); });
} }
private @NotNull String getColorAsHex (@NotNull Color color) { @SuppressWarnings("ConstantConditions")
return "#" + Integer.toHexString(color.getRGB()).toUpperCase(); @Executable
} @SubCommand(
name = "channel",
private Color getMemberColour(@NotNull Member member) { description = "Retrieve the current channel or another channel's information",
return member.getColor() == null ? Color.BLACK : member.getColor(); options = {
} @Option(
optionType = OptionType.CHANNEL,
private @NotNull String getMemberBadges(@NotNull Member member) { name = "channel",
User user = member.getUser(); description = "A channel within the server",
String memberBadges = ""; isRequired = false
)
if (user.isBot()) memberBadges += Constants.BADGE_BOT; }
if (member.getTimeBoosted() != null) memberBadges += Constants.BADGE_SERVER_BOOST; )
public void channelCommand(@NotNull SlashCommandEvent slashCommandEvent) {
memberBadges += user.getFlags().toString() OptionMapping optionalChannel = slashCommandEvent.getOption("channel");
.substring(1, user.getFlags().toString().length() - 1) JDA jda = slashCommandEvent.getJDA();
.replace(",", "") MessageEmbed messageEmbed;
.replace("PARTNER", Constants.BADGE_PARTNER)
.replace("HYPESQUAD_BRAVERY", Constants.BADGE_BRAVERY) if (optionalChannel == null) {
.replace("HYPESQUAD_BRILLIANCE", Constants.BADGE_BRILLIANCE) ChannelType channelType = slashCommandEvent.getChannelType();
.replace("HYPESQUAD_BALANCE", Constants.BADGE_BALANCE) if (channelType == ChannelType.TEXT) {
.replace("VERIFIED_DEVELOPER", Constants.BADGE_EARLY_VERIFIED_BOT_DEVELOPER) messageEmbed = handleTextChannel(slashCommandEvent.getTextChannel());
.replace("EARLY_SUPPORTER", Constants.BADGE_EARLY_SUPPORTER) } else {
.replace("SYSTEM", Constants.BADGE_STAFF) slashCommandEvent.reply("Channel Type Not Supported").queue();
.replace("BUG_HUNTER_LEVEL_1", Constants.BADGE_BUG_HUNTER) return;
.replace("BUG_HUNTER_LEVEL_2", Constants.BADGE_BUG_HUNTER) }
.replace("VERIFIED_BOT", Constants.BADGE_VERIFIED_BOT); } else {
ChannelType channelType = optionalChannel.getChannelType();
return memberBadges; GuildChannel guildChannel = optionalChannel.getAsGuildChannel();
}
private @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);
}
private @NotNull String getTopRole(@NotNull Member member) {
List<Role> memberRoles = member.getRoles();
if (memberRoles.isEmpty()) return "No Roles";
return memberRoles.get(0).getAsMention();
}
private String getListOfMembers(@NotNull List<Member> members, int limit) {
StringJoiner memberJoiner = new StringJoiner(" **|** ");
if (members.isEmpty()) return "No Members In Role";
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();
}
private 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) { if (channelType == ChannelType.TEXT) {
int leftOverRoles = memberRoles.size() - limit; messageEmbed = handleTextChannel(jda.getTextChannelById(guildChannel.getId()));
memberRolesJoiner.add(" and ** " + leftOverRoles + " ** more "); } else if (channelType == ChannelType.VOICE) {
messageEmbed = handleVoiceChannel(jda.getVoiceChannelById(guildChannel.getId()));
} else {
slashCommandEvent.reply("Channel Type Not Supported").queue();
return;
}
} }
return memberRolesJoiner.toString(); slashCommandEvent.replyEmbeds(messageEmbed).queue();
} }
private @NotNull String getCreationDate(@NotNull Role role) { @Executable
long timeCreated = role.getTimeCreated().toInstant().getEpochSecond(); @SubCommand(
return "<t:" + timeCreated + ":F>"; name = "server",
description = "Retrieve information about the current server you are in"
)
public void serverCommand(SlashCommandEvent slashCommandEvent) {
Guild guild = slashCommandEvent.getGuild();
} }
private @NotNull String getRegisteredDate(@NotNull Member member) { private @NotNull MessageEmbed handleVoiceChannel(@NotNull VoiceChannel voiceChannel) {
long timeCreated = member.getTimeCreated().toInstant().getEpochSecond(); String region;
return "<t:" + timeCreated + ":F>"; if (voiceChannel.getRegion() == Region.AUTOMATIC) {
region = "\uD83C\uDFF3\u200D\uD83C\uDF08 " + voiceChannel.getRegion().getName();
} else {
region = voiceChannel.getRegion().getEmoji() + " " + voiceChannel.getRegion().getName();
}
String userLimit = voiceChannel.getUserLimit() == 0 ? "No Limit" : String.valueOf(voiceChannel.getUserLimit());
String bitrate = voiceChannel.getBitrate() + "bit/s";
String isSynced = voiceChannel.isSynced() ? Constants.CHECK : Constants.CROSS;
String category = voiceChannel.getParent() == null ? "No Category" : voiceChannel.getParent().getName();
String position = "#" + voiceChannel.getPosition();
String miscString = "Synced: " + isSynced;
String description = """
$channelMention
**Category:** $category
**Position:** $position
**Bitrate:** $bitrate
**User Limit:** $userLimit
**Region:** $region
"""
.replace("$channelMention", voiceChannel.getAsMention())
.replace("$category", category)
.replace("$position", position)
.replace("$bitrate", bitrate)
.replace("$userLimit", userLimit)
.replace("$region", region);
return embedService.getBaseEmbed()
.setTitle("\uD83D\uDD08" + voiceChannel.getName() + " Information")
.setDescription(description)
.setThumbnail(voiceChannel.getGuild().getIconUrl())
.addField("Creation At", infoUtil.getCreationDate(voiceChannel), false)
.addField("Misc", miscString, false)
.setFooter("ID: " + voiceChannel.getId())
.build();
} }
private @NotNull String getJoinedDate(@NotNull Member member) { private @NotNull MessageEmbed handleTextChannel(@NotNull TextChannel textChannel) {
long timeJoined = member.getTimeJoined().toInstant().getEpochSecond(); String isNsfw = textChannel.isNSFW() ? Constants.CHECK : Constants.CROSS;
return "<t:" + timeJoined + ":F>"; String isNews = textChannel.isNews() ? Constants.CHECK : Constants.CROSS;
String isSynced = textChannel.isSynced() ? Constants.CHECK : Constants.CROSS;
String topic = textChannel.getTopic() == null ? "No Topic Set" : textChannel.getTopic();
String category = textChannel.getParent() == null ? "No Category" : textChannel.getParent().getName();
String position = "#" + textChannel.getPosition();
String miscString = """
Nsfw: $isNsfw
News: $isNews
Synced: $isSynced
"""
.replace("$isNsfw", isNsfw)
.replace("$isNews", isNews)
.replace("$isSynced", isSynced);
String description = """
$channelMention
**Category:** $category
**Position:** $position
**Topic:** $topic
"""
.replace("$channelMention", textChannel.getAsMention())
.replace("$category", category)
.replace("$position", position)
.replace("$topic", topic);
return embedService.getBaseEmbed()
.setTitle("#" + textChannel.getName() + " Information")
.setDescription(description)
.setThumbnail(textChannel.getGuild().getIconUrl())
.addField("Creation At", infoUtil.getCreationDate(textChannel), false)
.addField("Misc", miscString, false)
.setFooter("ID: " + textChannel.getId())
.build();
} }
} }

Loading…
Cancel
Save