Skip to content

Commit

Permalink
Command Cleanup #1: Bot Commands
Browse files Browse the repository at this point in the history
All:
- Removed text-based response handling
- Cleaned up code
- Responses are now ephemeral

Feedback:
- Added 10 char requirement check

Help:
- Now shows privacy policy
- Stats command is now clickable

Ping:
- Now shows ms ping

Privacy:
- Removed, merged into Help

Stats:
- Now shows commands/min
- Now shows messages/sec
- Now shows user installation count
- Numbers are delimited properly
  • Loading branch information
Chew committed Aug 22, 2024
1 parent df80c06 commit 86c39c4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 171 deletions.
80 changes: 32 additions & 48 deletions src/main/java/pw/chew/chewbotcca/commands/bot/FeedbackCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,72 +28,58 @@
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
/**
* <h2><code>/feedback</code> Command</h2>
*
* <a href="https://help.chew.pro/bots/discord/chewbotcca/commands/feedback">Docs</a>
*/
public class FeedbackCommand extends SlashCommand {

public FeedbackCommand() {
this.name = "feedback";
this.help = "Leave some feedback about the bot";
this.cooldown = 30;
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();
}
}
34 changes: 10 additions & 24 deletions src/main/java/pw/chew/chewbotcca/commands/bot/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
* <h2><code>/help</code> Command</h2>
*
* <a href="https://help.chew.pro/bots/discord/chewbotcca/commands/help">Docs</a>
*/
public class HelpCommand extends SlashCommand {
public HelpCommand() {
this.name = "help";
Expand All @@ -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("""
Expand All @@ -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();
}
}
21 changes: 7 additions & 14 deletions src/main/java/pw/chew/chewbotcca/commands/bot/InviteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
* <h2><code>/invite</code> Command</h2>
*
* <a href="https://help.chew.pro/bots/discord/chewbotcca/commands/invite">Docs</a>
*/
public class InviteCommand extends SlashCommand {
public InviteCommand() {
this.name = "invite";
Expand All @@ -35,23 +37,14 @@ 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)!
[Click me to invite me to your server (admin)](https://discord.com/api/oauth2/authorize?client_id=604362556668248095&permissions=8&scope=bot%20applications.commands)!
[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();
}
}
31 changes: 12 additions & 19 deletions src/main/java/pw/chew/chewbotcca/commands/bot/PingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <h2><code>/ping</code> Command</h2>
*
* <a href="https://help.chew.pro/bots/discord/chewbotcca/commands/ping">Docs</a>
*/
public class PingCommand extends SlashCommand {
public PingCommand() {
this.name = "ping";
this.help = "Ping the bot";
Expand All @@ -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();
}
}
41 changes: 0 additions & 41 deletions src/main/java/pw/chew/chewbotcca/commands/bot/PrivacyCommand.java

This file was deleted.

Loading

0 comments on commit 86c39c4

Please sign in to comment.