diff --git a/src/main/java/pw/chew/chewbotcca/commands/bot/FeedbackCommand.java b/src/main/java/pw/chew/chewbotcca/commands/bot/FeedbackCommand.java index 975ba4f..a62949e 100644 --- a/src/main/java/pw/chew/chewbotcca/commands/bot/FeedbackCommand.java +++ b/src/main/java/pw/chew/chewbotcca/commands/bot/FeedbackCommand.java @@ -16,12 +16,10 @@ */ package pw.chew.chewbotcca.commands.bot; -import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jdautilities.command.CooldownScope; 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.entities.MessageEmbed; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; @@ -30,13 +28,15 @@ import net.dv8tion.jda.api.interactions.commands.build.OptionData; import pw.chew.chewbotcca.util.ResponseHelper; -import java.awt.Color; import java.time.Instant; import java.util.Collections; -// %^feedback command +/** + *

/feedback Command

+ * + * Docs + */ public class FeedbackCommand extends SlashCommand { - public FeedbackCommand() { this.name = "feedback"; this.help = "Leave some feedback about the bot"; @@ -44,58 +44,42 @@ public FeedbackCommand() { this.cooldownScope = CooldownScope.USER; this.contexts = new InteractionContextType[]{InteractionContextType.GUILD, InteractionContextType.BOT_DM, InteractionContextType.PRIVATE_CHANNEL}; this.options = Collections.singletonList( - new OptionData(OptionType.STRING, "feedback", "The feedback you want to leave").setRequired(true) + new OptionData(OptionType.STRING, "feedback", "The feedback you want to leave", true) + .setMinLength(10) ); } @Override protected void execute(SlashCommandEvent event) { - try { - var feedback = event.optString("feedback", ""); - TextChannel feedbackChannel = retrieveFeedbackChannel(event.getJDA()); - feedbackChannel.sendMessageEmbeds(generateFeedbackEmbed(feedback, event.getUser())).queue( - message -> event.reply("I have successfully sent the feedback! Feel free to see it on the help server with `/invite`") - .setEphemeral(true) - .queue() - ); - } catch (IllegalArgumentException e) { - event.replyEmbeds(ResponseHelper.generateFailureEmbed(null, e.getMessage())).setEphemeral(true).queue(); + var feedback = event.optString("feedback", ""); + TextChannel feedbackChannel = event.getJDA().getTextChannelById("745164378659225651"); + if (feedbackChannel == null) { + event.replyEmbeds(ResponseHelper.generateFailureEmbed("Error Sending Feedback!", "Could not find feedback channel.")) + .setEphemeral(true).queue(); + return; } + feedbackChannel.sendMessageEmbeds(generateFeedbackEmbed(feedback, event.getUser())).queue( + message -> event.reply("I have successfully sent the feedback! Feel free to see it on the help server with `/invite`") + .setEphemeral(true) + .queue() + ); } - @Override - protected void execute(CommandEvent commandEvent) { - try { - var feedback = commandEvent.getArgs(); - TextChannel feedbackChannel = retrieveFeedbackChannel(commandEvent.getJDA()); - feedbackChannel.sendMessageEmbeds(generateFeedbackEmbed(feedback, commandEvent.getAuthor())).queue( - message -> commandEvent.reply("I have successfully sent the feedback! Feel free to see it on the help server with `" + commandEvent.getPrefix() + "invite`") - ); - } catch (IllegalArgumentException e) { - commandEvent.replyError(e.getMessage()); - } - } - + /** + * Generates the feedback embed + * + * @param feedback The feedback to send + * @param author The author of the feedback + * @return The feedback embed + */ private MessageEmbed generateFeedbackEmbed(String feedback, User author) { - if (feedback.length() < 10) { - throw new IllegalArgumentException("Your feedback is too short, how are we supposed to improve! Please enter at least 10 characters."); - } - var embed = new EmbedBuilder(); - embed.setTitle("New Feedback!"); - embed.setColor(Color.decode("#6166A8")); - embed.setDescription(feedback); - embed.setTimestamp(Instant.now()); - embed.setAuthor(author.getAsTag(), null, author.getAvatarUrl()); - embed.setFooter("User ID: " + author.getId()); - return embed.build(); - } - - private TextChannel retrieveFeedbackChannel(JDA jda) { - TextChannel feedbackChannel = jda.getTextChannelById("745164378659225651"); - if (feedbackChannel == null) { - throw new IllegalArgumentException("Could not find feedback channel!"); - } - return feedbackChannel; + return new EmbedBuilder() + .setTitle("New Feedback!") + .setColor(0x6166A8) + .setDescription(feedback) + .setTimestamp(Instant.now()) + .setAuthor(author.getAsTag(), null, author.getAvatarUrl()) + .setFooter("User ID: " + author.getId()).build(); } } diff --git a/src/main/java/pw/chew/chewbotcca/commands/bot/HelpCommand.java b/src/main/java/pw/chew/chewbotcca/commands/bot/HelpCommand.java index f58ee78..eb9e1b1 100644 --- a/src/main/java/pw/chew/chewbotcca/commands/bot/HelpCommand.java +++ b/src/main/java/pw/chew/chewbotcca/commands/bot/HelpCommand.java @@ -16,15 +16,18 @@ */ package pw.chew.chewbotcca.commands.bot; -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.Permission; -import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.interactions.InteractionContextType; +import pw.chew.chewbotcca.util.CommandMentionHelper; -// %^help command +/** + *

/help Command

+ * + * Docs + */ public class HelpCommand extends SlashCommand { public HelpCommand() { this.name = "help"; @@ -35,25 +38,7 @@ public HelpCommand() { @Override protected void execute(SlashCommandEvent event) { - event.replyEmbeds(generateHelpEmbed("/")).queue(); - } - - @Override - protected void execute(CommandEvent commandEvent) { - // Reply with embed - String notice = null; - if (!commandEvent.getArgs().isEmpty()) { - notice = "Psst, to view more info about a command, use `" + commandEvent.getPrefix() + "info command`!"; - } - MessageEmbed embed = generateHelpEmbed(commandEvent.getPrefix()); - if (notice == null) - commandEvent.getChannel().sendMessageEmbeds(embed).queue(); - else - commandEvent.getChannel().sendMessage(notice).setEmbeds(embed).queue(); - } - - private MessageEmbed generateHelpEmbed(String prefix) { - return new EmbedBuilder() + event.replyEmbeds(new EmbedBuilder() .setTitle("Welcome to the Chewbotcca Discord Bot") .setColor(0xd084) .setDescription(""" @@ -63,7 +48,8 @@ private MessageEmbed generateHelpEmbed(String prefix) { .addField("Commands", "You can find all my commands [here](https:/help.chew.pro/bots/discord/chewbotcca/commands)", true) .addField("Invite me!", "You can invite me to your server with [this link](https://discord.com/oauth2/authorize?client_id=604362556668248095&scope=bot&permissions=0).", true) .addField("Help Server", "Click [me](https://discord.gg/UjxQ3Bh) to join the help server.", true) - .addField("More Bot Stats", "Run `" + prefix + "stats` to see more stats!", true) - .build(); + .addField("Privacy Policy", "[View Privacy Policy](https://chew.pw/chewbotcca/discord/privacy)", true) + .addField("More Bot Stats", "Run %s to see more stats!".formatted(CommandMentionHelper.mention("stats")), true) + .build()).setEphemeral(true).queue(); } } diff --git a/src/main/java/pw/chew/chewbotcca/commands/bot/InviteCommand.java b/src/main/java/pw/chew/chewbotcca/commands/bot/InviteCommand.java index 5945ace..00abd88 100644 --- a/src/main/java/pw/chew/chewbotcca/commands/bot/InviteCommand.java +++ b/src/main/java/pw/chew/chewbotcca/commands/bot/InviteCommand.java @@ -16,15 +16,17 @@ */ package pw.chew.chewbotcca.commands.bot; -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.Permission; -import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.interactions.InteractionContextType; -// %^invite command +/** + *

/invite Command

+ * + * Docs + */ public class InviteCommand extends SlashCommand { public InviteCommand() { this.name = "invite"; @@ -35,16 +37,7 @@ public InviteCommand() { @Override protected void execute(SlashCommandEvent event) { - event.replyEmbeds(generateInviteEmbed()).queue(); - } - - @Override - protected void execute(CommandEvent commandEvent) { - commandEvent.reply(generateInviteEmbed()); - } - - private MessageEmbed generateInviteEmbed() { - return new EmbedBuilder() + event.replyEmbeds(new EmbedBuilder() .setTitle("Invite me!") .setDescription(""" [Click me to invite me to your server (recommended)](https://discord.com/api/oauth2/authorize?client_id=604362556668248095&permissions=939879492&scope=bot%20applications.commands)! @@ -52,6 +45,6 @@ private MessageEmbed generateInviteEmbed() { [Need help? Click me to join my help server](https://discord.gg/UjxQ3Bh)! - [Sponsored: Click me to get a VPS from SkySilk Cloud Services](https://www.skysilk.com/ref/4PRQpuQraD)!""").build(); + [Sponsored: Click me to get a VPS from SkySilk Cloud Services](https://www.skysilk.com/ref/4PRQpuQraD)!""").build()).setEphemeral(true).queue(); } } diff --git a/src/main/java/pw/chew/chewbotcca/commands/bot/PingCommand.java b/src/main/java/pw/chew/chewbotcca/commands/bot/PingCommand.java index 86572d7..3231f12 100644 --- a/src/main/java/pw/chew/chewbotcca/commands/bot/PingCommand.java +++ b/src/main/java/pw/chew/chewbotcca/commands/bot/PingCommand.java @@ -16,16 +16,19 @@ */ package pw.chew.chewbotcca.commands.bot; -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.Permission; import net.dv8tion.jda.api.interactions.InteractionContextType; -// %^ping command -public class PingCommand extends SlashCommand { +import java.time.OffsetDateTime; +/** + *

/ping Command

+ * + * Docs + */ +public class PingCommand extends SlashCommand { public PingCommand() { this.name = "ping"; this.help = "Ping the bot"; @@ -34,22 +37,12 @@ public PingCommand() { } @Override - protected void execute(SlashCommandEvent slashCommandEvent) { + protected void execute(SlashCommandEvent event) { // Has to be simpler due to interaction weirdness - slashCommandEvent.reply("Pong!").setEphemeral(true).queue(); - } + OffsetDateTime startTime = event.getTimeCreated(); + OffsetDateTime endTime = OffsetDateTime.now(); + long diffInMs = endTime.toInstant().toEpochMilli() - startTime.toInstant().toEpochMilli(); - @Override - protected void execute(CommandEvent commandEvent) { - // Get the timestamp of the ping message - long time = commandEvent.getMessage().getTimeCreated().toInstant().toEpochMilli(); - // Send a "Checking ping" message and calculate the difference between this message and the %^ping message - commandEvent.getChannel().sendMessageEmbeds(new EmbedBuilder().setDescription("Checking ping..").build()).queue((msg) -> { - EmbedBuilder eb = new EmbedBuilder().setDescription( - "Ping is " + (msg.getTimeCreated().toInstant().toEpochMilli() - time) + "ms\n" + - "Gateway Ping is " + commandEvent.getJDA().getGatewayPing() + "ms\n" - ); - msg.editMessageEmbeds(eb.build()).queue(); - }); + event.reply("Pong! Took %sms".formatted(Math.abs(diffInMs))).setEphemeral(true).queue(); } } diff --git a/src/main/java/pw/chew/chewbotcca/commands/bot/PrivacyCommand.java b/src/main/java/pw/chew/chewbotcca/commands/bot/PrivacyCommand.java deleted file mode 100644 index 1161e2b..0000000 --- a/src/main/java/pw/chew/chewbotcca/commands/bot/PrivacyCommand.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2024 Chewbotcca - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package pw.chew.chewbotcca.commands.bot; - -import com.jagrosh.jdautilities.command.CommandEvent; -import com.jagrosh.jdautilities.command.SlashCommand; -import com.jagrosh.jdautilities.command.SlashCommandEvent; -import net.dv8tion.jda.api.interactions.InteractionContextType; - -public class PrivacyCommand extends SlashCommand { - - public PrivacyCommand() { - this.name = "privacy"; - this.help = "Find a link to Chewbotcca's privacy policy"; - this.contexts = new InteractionContextType[]{InteractionContextType.GUILD, InteractionContextType.BOT_DM, InteractionContextType.PRIVATE_CHANNEL}; - } - - @Override - protected void execute(SlashCommandEvent event) { - event.reply("You can view the Chewbotcca privacy policy here: https://chew.pw/chewbotcca/discord/privacy").setEphemeral(true).queue(); - } - - @Override - protected void execute(CommandEvent event) { - event.reply("You can view the Chewbotcca privacy policy here: https://chew.pw/chewbotcca/discord/privacy"); - } -} diff --git a/src/main/java/pw/chew/chewbotcca/commands/bot/StatsCommand.java b/src/main/java/pw/chew/chewbotcca/commands/bot/StatsCommand.java index fb1309b..dcda4fa 100644 --- a/src/main/java/pw/chew/chewbotcca/commands/bot/StatsCommand.java +++ b/src/main/java/pw/chew/chewbotcca/commands/bot/StatsCommand.java @@ -16,7 +16,6 @@ */ package pw.chew.chewbotcca.commands.bot; -import com.jagrosh.jdautilities.command.CommandEvent; import com.jagrosh.jdautilities.command.SlashCommand; import com.jagrosh.jdautilities.command.SlashCommandEvent; import net.dv8tion.jda.api.EmbedBuilder; @@ -27,11 +26,18 @@ import net.dv8tion.jda.api.interactions.InteractionContextType; import pw.chew.chewbotcca.util.DateTime; +import java.math.RoundingMode; +import java.text.DecimalFormat; import java.time.Instant; import static pw.chew.chewbotcca.util.MiscUtil.bytesToFriendly; +import static pw.chew.chewbotcca.util.MiscUtil.delimitNumber; -// %^stats command +/** + *

/stats Command

+ * + * Docs + */ public class StatsCommand extends SlashCommand { private final static Instant startTime = Instant.now(); @@ -47,12 +53,7 @@ public StatsCommand() { @Override protected void execute(SlashCommandEvent event) { - event.replyEmbeds(generateStatsEmbed(event.getJDA())).queue(); - } - - @Override - protected void execute(CommandEvent commandEvent) { - commandEvent.reply(generateStatsEmbed(commandEvent.getJDA())); + event.replyEmbeds(generateStatsEmbed(event.getJDA())).setEphemeral(true).queue(); } /** @@ -69,31 +70,31 @@ private MessageEmbed generateStatsEmbed(JDA jda) { // TODO: Cpu stats? // OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); + // double cpuLoad = operatingSystemMXBean.getSystemLoadAverage(); // Gather other data. All must be strings - String author = jda.retrieveUserById("476488167042580481").complete().getAsMention(); - long uptimeInSeconds = Instant.now().toEpochMilli() - startTime.toEpochMilli() / 1000; + long uptimeInSeconds = Instant.now().getEpochSecond() - startTime.getEpochSecond(); String uptime = DateTime.timeAgoFromNow(startTime).replaceAll(", ", ",\n"); - String servers = String.valueOf(jda.getGuildCache().size()); - // TODO: Round commands/messages per x to 4 decimal - // DecimalFormat df = new DecimalFormat("#.####"); - // df.setRoundingMode(RoundingMode.CEILING); - // float commandsPerMinute = (float)getExecutedCommandsCount() / (float)(uptimeInSeconds / 60); - // float messagesPerSecond = (float)getMessageCount() / (float)uptimeInSeconds; - // String commands = getExecutedCommandsCount() + String.format(" [%s/m]", df.format(commandsPerMinute)); - // String messages = getMessageCount() + String.format(" [%s/s]", df.format(messagesPerSecond)); - String commands = String.valueOf(getExecutedCommandsCount()); - String messages = String.valueOf(getMessageCount()); + long servers = jda.getGuildCache().size(); + int users = jda.retrieveApplicationInfo().complete().getUserInstallCount(); + DecimalFormat df = new DecimalFormat("#.####"); + df.setRoundingMode(RoundingMode.CEILING); + // rounded to 4 decimal places + float commandsPerMinute = (float) executedCommands / ((float) uptimeInSeconds / 60); + float messagesPerSecond = (float) sentMessages / (float) uptimeInSeconds; + String commands = delimitNumber(executedCommands) + String.format(" [%s/m]", df.format(commandsPerMinute)); + String messages = delimitNumber(sentMessages) + String.format(" [%s/s]", df.format(messagesPerSecond)); return new EmbedBuilder() .setTitle("Chewbotcca - A basic, yet functioning, Discord bot") - .addField("Author", author, true) - .addField("Code", "[View code on GitHub](https://github.com/Chewbotcca/Discord)", true) + // Basic bot info + .addField("Author", "[Chew](https://github.com/Chew)", true) + .addField("Code", "[View code on GitHub](https://github.com/Chewbotcca/Chewbotcca-Discord)", true) .addField("Library", "[JDA " + JDAInfo.VERSION + "](" + JDAInfo.GITHUB + ")", true) - // Convert the time difference into a time ago + // Convert the time difference into a time ago. .addField("Uptime", uptime, true) - // Get the server count. NOT GUILD NOT GUILD NOT GUILD - .addField("Servers", servers, true) + // Get the installation count. + .addField("Installs", "Servers: %s\nUsers: %s".formatted(servers, users), true) // Memory usage .addField("Memory", memoryUsage, true) // Sent commands