Skip to content

Commit

Permalink
Improvements to channelinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed Jul 29, 2024
1 parent 74f17a0 commit ce1d1b3
Showing 1 changed file with 43 additions and 63 deletions.
106 changes: 43 additions & 63 deletions src/main/java/pw/chew/chewbotcca/commands/info/ChannelInfoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
*/
package pw.chew.chewbotcca.commands.info;

import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.Region;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.Webhook;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
Expand All @@ -42,7 +43,11 @@
import java.util.List;
import java.util.Map;

// %^channelinfo command
/**
* <h2>ChannelInfo Command</h2>
*
* <a href="https://help.chew.pro/bots/discord/chewbotcca/commands/channelinfo">Docs</a>
*/
public class ChannelInfoCommand extends SlashCommand {
public ChannelInfoCommand() {
this.name = "channelinfo";
Expand All @@ -51,7 +56,7 @@ public ChannelInfoCommand() {
this.botPermissions = new Permission[]{Permission.MESSAGE_EMBED_LINKS};
this.contexts = new InteractionContextType[]{InteractionContextType.GUILD};
this.options = Arrays.asList(
new OptionData(OptionType.CHANNEL, "channel", "The channel to find info about").setRequired(true),
new OptionData(OptionType.CHANNEL, "channel", "The channel to find info about", false),
new OptionData(OptionType.STRING, "type", "The type of information to return. default: general")
.addChoices(
new Command.Choice("General Information", "general"),
Expand All @@ -63,92 +68,62 @@ public ChannelInfoCommand() {
@Override
protected void execute(SlashCommandEvent event) {
GuildChannel channel = event.optGuildChannel("channel", event.getGuildChannel());
event.replyEmbeds(switch (event.optString("type", "general")) {
case "pins" -> getPinsInfo((TextChannel) channel, event.getJDA()).build();
default -> gatherMainInfo(channel, null).build();
}).queue();
}
String type = event.optString("type", "general");

@Override
protected void execute(CommandEvent commandEvent) {
// Get args
String args = commandEvent.getArgs();
String mode = "";

// Set the mode to pins if requested
if(args.contains("pins")) {
mode = "pins";
args = args.replace("--pins", "").trim();
}

// Find the channel based on input if possible
GuildChannel channel;
if(args.length() == 0) {
channel = commandEvent.getGuild().getGuildChannelById(commandEvent.getChannel().getId());
} else if(args.contains("<#")) {
String id = args.replace("<#", "").replace(">", "");
channel = commandEvent.getGuild().getGuildChannelById(id);
if (type.equals("general")) {
event.replyEmbeds(gatherMainInfo(channel).build()).queue();
} else {
try {
channel = commandEvent.getGuild().getGuildChannelById(args);
} catch(NumberFormatException e) {
commandEvent.reply("This channel could not be found!");
if (channel.getType() != ChannelType.TEXT) {
event.reply("Pins can only be retrieved from text channels!").setEphemeral(true).queue();
return;
}
}

// If there's no channel
if(channel == null) {
commandEvent.reply("This channel could not be found!");
return;
}

// Generate and send data based on input
if(mode.equals("pins") && channel.getType() == ChannelType.TEXT) {
commandEvent.reply(getPinsInfo((TextChannel)channel, commandEvent.getJDA()).build());
} else if(mode.equals("pins")) {
commandEvent.reply("Pins sub-command only works in Text channels!");
} else {
commandEvent.reply(gatherMainInfo(channel, commandEvent).build());
event.replyEmbeds(getPinsInfo((TextChannel) channel, event.getJDA()).build()).queue();
}
}

/**
* Gather main info
*
* @param channel the channel
* @param commandEvent the command event
* @return an embed ready to be built
*/
public EmbedBuilder gatherMainInfo(GuildChannel channel, CommandEvent commandEvent) {
public EmbedBuilder gatherMainInfo(GuildChannel channel) {
// Start generating an embed
EmbedBuilder e = new EmbedBuilder();
// If it's a text channel, add a hashtag, no need for one otherwise
if(channel.getType() == ChannelType.TEXT) {
if (channel.getType() == ChannelType.TEXT) {
e.setTitle("Channel Info for #" + channel.getName());
e.setDescription(((TextChannel)channel).getTopic());
} else {
e.setTitle("Channel Info for " + channel.getName());
}

e.addField("ID", channel.getId(), true);
e.addField("Type", channel.getType().toString(), true);

if (channel.getType() == ChannelType.VOICE) {
e.setDescription(((VoiceChannel) channel).getStatus());
}

// Users in channel if it's an audio channel.
if (channel.getType().isAudio()) {
AudioChannel vc = ((AudioChannel) channel);
e.addField("Users in Channel", String.valueOf(vc.getMembers().size()), true);
e.addField("Voice Region", vc.getRegion().getEmoji() + " " + vc.getRegion().getName(), true);
if (vc.getRegion() == Region.AUTOMATIC) {
e.addField("Voice Region", "Automatic", true);
} else {
e.addField("Voice Region", vc.getRegion().getEmoji() + " " + vc.getRegion().getName(), true);
}
}

e.addField("Type", channel.getType().toString(), true);

// If it's a text channel and we can access the webhooks, add the count.
// If it's a text channel, and we can access the webhooks, add the count.
if (channel.getType() == ChannelType.TEXT) {
TextChannel textChannel = ((TextChannel) channel);
if (commandEvent != null && commandEvent.getSelfMember().hasPermission(Permission.MANAGE_WEBHOOKS)) {
e.setDescription(textChannel.getTopic());
if (channel.getGuild().getSelfMember().hasPermission(Permission.MANAGE_WEBHOOKS)) {
List<Webhook> hooks = textChannel.retrieveWebhooks().complete();
e.addField("Webhooks", String.valueOf(hooks.size()), true);
}
if (commandEvent != null && commandEvent.getSelfMember().hasPermission(Permission.VIEW_CHANNEL)) {
if (channel.getGuild().getSelfMember().hasPermission(Permission.VIEW_CHANNEL)) {
e.addField("Pins", textChannel.retrievePinnedMessages().complete().size() + " / 50", true);
}
List<String> info = new ArrayList<>();
Expand All @@ -168,8 +143,9 @@ public EmbedBuilder gatherMainInfo(GuildChannel channel, CommandEvent commandEve

/**
* Get pins info for a channel
*
* @param channel the channel
* @param jda jda for getting users
* @param jda jda for getting users
* @return an embed ready to be build
*/
public EmbedBuilder getPinsInfo(TextChannel channel, JDA jda) {
Expand All @@ -178,34 +154,38 @@ public EmbedBuilder getPinsInfo(TextChannel channel, JDA jda) {
List<Message> pins = channel.retrievePinnedMessages().complete();
// Find the top pins users and sort it
HashMap<String, Integer> topPins = new HashMap<>();
for(Message message : pins) {
for (Message message : pins) {
String authorId = message.getAuthor().getId();
int current = topPins.getOrDefault(authorId, 0);
topPins.put(authorId, current + 1);
}
ArrayList<Map.Entry<String, Integer>> l = new ArrayList<>(topPins.entrySet());
l.sort(Map.Entry.comparingByValue());
// Have to reverse since it's in 1, 2, 3 order. Ascending i think it's called
// Have to reverse since it's in 1, 2, 3 order. Ascending I think it's called
Collections.reverse(l);
// Make an arraylist with the "#x: User - Pins"
List<CharSequence> top = new ArrayList<>();
top.add("Total Pins: " + pins.size() + " / 50");
for(int i = 0; i < l.size(); i++) {
for (int i = 0; i < l.size(); i++) {
Map.Entry<String, Integer> entry = l.get(i);
String user = entry.getKey();
int pinCount = entry.getValue();
User userById = jda.getUserById(user);
String tag;
if(userById == null) {
if (userById == null) {
try {
tag = jda.retrieveUserById(user).complete().getAsTag();
} catch (ErrorResponseException ignored) {
tag = "Unknown User";
}
} else {
tag = userById.getAsTag();
if (userById.isBot()) {
tag = userById.getAsTag();
} else {
tag = userById.getName();
}
}
top.add("#" + (i+1) + ": " + pinCount + " pins - " + tag);
top.add((i + 1) + ". " + pinCount + " pins - " + tag);
}
e.setDescription(String.join("\n", top));
return e;
Expand Down

0 comments on commit ce1d1b3

Please sign in to comment.