Skip to content

Commit 73611c6

Browse files
Add the /greetings command
1 parent ce87c71 commit 73611c6

6 files changed

Lines changed: 132 additions & 8 deletions

File tree

src/main/java/technobot/commands/CommandRegistry.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import technobot.TechnoBot;
1212
import technobot.commands.fun.*;
1313
import technobot.commands.greetings.GreetCommand;
14+
import technobot.commands.greetings.GreetingsCommand;
1415
import technobot.commands.levels.*;
1516
import technobot.commands.music.*;
1617
import technobot.commands.staff.*;
@@ -46,6 +47,7 @@ public CommandRegistry(TechnoBot bot) {
4647
mapCommand(
4748
//Greeting commands
4849
new GreetCommand(bot),
50+
new GreetingsCommand(bot),
4951

5052
//Fun commands
5153
new JokeCommand(bot),
@@ -142,7 +144,9 @@ public static List<CommandData> unpackCommandData() {
142144
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
143145
String name = event.getName();
144146
Command cmd = commandsMap.get(name);
145-
cmd.execute(event);
147+
if (cmd != null) {
148+
cmd.execute(event);
149+
}
146150
}
147151

148152
/**

src/main/java/technobot/commands/greetings/GreetCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public void execute(SlashCommandInteractionEvent event) {
3939
// Remove greeting message
4040
if (greetingOption == null) {
4141
greetingHandler.removeGreet();
42-
String text = EmbedUtils.BLUE_X + " Greeting message removed!";
42+
String text = EmbedUtils.BLUE_X + " Greeting message successfully removed!";
4343
event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue();
4444
return;
4545
}
4646

4747
// Set greeting message
4848
greetingHandler.setGreet(greetingOption.getAsString());
49-
String text = EmbedUtils.BLUE_TICK + " Greeting message updated!";
49+
String text = EmbedUtils.BLUE_TICK + " Greeting message successfully updated!";
5050
event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue();
5151
}
5252
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package technobot.commands.greetings;
2+
3+
import net.dv8tion.jda.api.Permission;
4+
import net.dv8tion.jda.api.entities.ChannelType;
5+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
6+
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
7+
import net.dv8tion.jda.api.interactions.commands.OptionType;
8+
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
9+
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
10+
import technobot.TechnoBot;
11+
import technobot.commands.Category;
12+
import technobot.commands.Command;
13+
import technobot.data.GuildData;
14+
import technobot.data.cache.Greetings;
15+
import technobot.handlers.GreetingHandler;
16+
import technobot.util.embeds.EmbedUtils;
17+
18+
/**
19+
* Command that displays and modifies greetings config.
20+
*
21+
* @author TechnoVision
22+
*/
23+
public class GreetingsCommand extends Command {
24+
25+
public GreetingsCommand(TechnoBot bot) {
26+
super(bot);
27+
this.name = "greetings";
28+
this.description = "Modify this server's greetings config.";
29+
this.category = Category.GREETINGS;
30+
this.permission = Permission.MANAGE_SERVER;
31+
this.subCommands.add(new SubcommandData("channel", "Sets a channel to send welcome messages to.")
32+
.addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to send welcome messages to")
33+
.setChannelTypes(ChannelType.TEXT, ChannelType.NEWS)));
34+
this.subCommands.add(new SubcommandData("config", "Displays the greetings config for this server."));
35+
}
36+
37+
@Override
38+
public void execute(SlashCommandInteractionEvent event) {
39+
event.deferReply().queue();
40+
GreetingHandler greetingHandler = GuildData.get(event.getGuild()).greetingHandler;
41+
42+
String text = "";
43+
switch(event.getSubcommandName()) {
44+
case "channel" -> {
45+
OptionMapping channelOption = event.getOption("channel");
46+
if (channelOption == null) {
47+
// Remove welcome channel if not specified
48+
greetingHandler.removeChannel();
49+
text = EmbedUtils.BLUE_X + " Welcome channel successfully removed!";
50+
} else {
51+
// Set welcome channel
52+
Long channelID = channelOption.getAsGuildChannel().getIdLong();
53+
greetingHandler.setChannel(channelID);
54+
text = EmbedUtils.BLUE_X + " Welcome channel set to <#" + channelID + ">";
55+
}
56+
}
57+
case "config" -> {
58+
text = configToString(greetingHandler.getConfig());
59+
event.getHook().sendMessage(text).queue();
60+
return;
61+
}
62+
}
63+
event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue();
64+
}
65+
66+
/**
67+
* Converts the greetings config into a readable string
68+
*
69+
* @param greetings an instance of the guild greetings config.
70+
* @return Stringified config (greetings only).
71+
*/
72+
private String configToString(Greetings greetings) {
73+
String text = "";
74+
if (greetings.getWelcomeChannel() == null) {
75+
text += "**Welcome Channel:** none\n";
76+
} else {
77+
text += "**Welcome Channel:** <#" + greetings.getWelcomeChannel() + ">\n";
78+
}
79+
if (greetings.getGreeting() == null) {
80+
text += "**Greeting:** none\n";
81+
} else {
82+
text += "**Greeting:** '" + greetings.getGreeting() + "'\n";
83+
}
84+
if (greetings.getFarewell() == null) {
85+
text += "**Farewell:** none\n";
86+
} else {
87+
text += "**Farewell:** " + greetings.getFarewell() + "\n";
88+
}
89+
if (greetings.getJoinDM() == null) {
90+
text += "**Join DM:** none\n";
91+
} else {
92+
text += "**Join DM:** " + greetings.getJoinDM() + "\n";
93+
}
94+
return text;
95+
}
96+
}

src/main/java/technobot/commands/levels/LevelingCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
import java.io.IOException;
2525
import java.net.URL;
2626

27+
/**
28+
* Command that displays and modifies leveling config.
29+
*
30+
* @author TechnoVision
31+
*/
2732
public class LevelingCommand extends Command {
2833

2934
public LevelingCommand(TechnoBot bot) {

src/main/java/technobot/commands/utility/HelpCommand.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import technobot.util.embeds.EmbedColor;
2222
import technobot.util.embeds.EmbedUtils;
2323

24-
import java.util.ArrayList;
25-
import java.util.HashMap;
26-
import java.util.List;
27-
import java.util.UUID;
24+
import java.util.*;
2825
import java.util.concurrent.TimeUnit;
2926

3027
public class HelpCommand extends Command {
@@ -47,7 +44,7 @@ public HelpCommand(TechnoBot bot) {
4744

4845
public void execute(SlashCommandInteractionEvent event) {
4946
// Create a hashmap that groups commands by categories.
50-
HashMap<Category, List<Command>> categories = new HashMap<>();
47+
HashMap<Category, List<Command>> categories = new LinkedHashMap<>();
5148
EmbedBuilder builder = new EmbedBuilder().setColor(EmbedColor.DEFAULT.color);
5249
for (Category category : Category.values()) {
5350
categories.put(category, new ArrayList<>());

src/main/java/technobot/handlers/GreetingHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,26 @@ public void removeGreet() {
4949
greetings.setGreeting(null);
5050
bot.database.greetings.updateOne(filter, Updates.unset("greeting"));
5151
}
52+
53+
/**
54+
* Set the welcome channel.
55+
*
56+
* @param channelID the ID of the channel to set.
57+
*/
58+
public void setChannel(Long channelID) {
59+
greetings.setWelcomeChannel(channelID);
60+
bot.database.greetings.updateOne(filter, Updates.set("welcome_channel", channelID));
61+
}
62+
63+
/**
64+
* Remove the welcome channel.
65+
*/
66+
public void removeChannel() {
67+
greetings.setWelcomeChannel(null);
68+
bot.database.greetings.updateOne(filter, Updates.unset("welcome_channel"));
69+
}
70+
71+
public Greetings getConfig() {
72+
return greetings;
73+
}
5274
}

0 commit comments

Comments
 (0)