Skip to content

Commit ce87c71

Browse files
Add the greetings backend and /greet command
1 parent 8e0e571 commit ce87c71

6 files changed

Lines changed: 183 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.jetbrains.annotations.NotNull;
1111
import technobot.TechnoBot;
1212
import technobot.commands.fun.*;
13+
import technobot.commands.greetings.GreetCommand;
1314
import technobot.commands.levels.*;
1415
import technobot.commands.music.*;
1516
import technobot.commands.staff.*;
@@ -43,6 +44,9 @@ public class CommandRegistry extends ListenerAdapter {
4344
*/
4445
public CommandRegistry(TechnoBot bot) {
4546
mapCommand(
47+
//Greeting commands
48+
new GreetCommand(bot),
49+
4650
//Fun commands
4751
new JokeCommand(bot),
4852
new MemeCommand(bot),
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package technobot.commands.greetings;
2+
3+
import net.dv8tion.jda.api.Permission;
4+
import net.dv8tion.jda.api.entities.Guild;
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 technobot.TechnoBot;
10+
import technobot.commands.Category;
11+
import technobot.commands.Command;
12+
import technobot.data.GuildData;
13+
import technobot.handlers.GreetingHandler;
14+
import technobot.util.embeds.EmbedUtils;
15+
16+
/**
17+
* Command that configures auto greetings.
18+
*
19+
* @author TechnoVision
20+
*/
21+
public class GreetCommand extends Command {
22+
23+
public GreetCommand(TechnoBot bot) {
24+
super(bot);
25+
this.name = "greet";
26+
this.description = "Sets a message to be sent to the welcome channel when a member joins.";
27+
this.category = Category.GREETINGS;
28+
this.args.add(new OptionData(OptionType.STRING, "greeting", "The message to send as a greeting"));
29+
this.permission = Permission.MANAGE_SERVER;
30+
}
31+
32+
@Override
33+
public void execute(SlashCommandInteractionEvent event) {
34+
event.deferReply().queue();
35+
Guild guild = event.getGuild();
36+
GreetingHandler greetingHandler = GuildData.get(guild).greetingHandler;
37+
OptionMapping greetingOption = event.getOption("greeting");
38+
39+
// Remove greeting message
40+
if (greetingOption == null) {
41+
greetingHandler.removeGreet();
42+
String text = EmbedUtils.BLUE_X + " Greeting message removed!";
43+
event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue();
44+
return;
45+
}
46+
47+
// Set greeting message
48+
greetingHandler.setGreet(greetingOption.getAsString());
49+
String text = EmbedUtils.BLUE_TICK + " Greeting message updated!";
50+
event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue();
51+
}
52+
}

src/main/java/technobot/data/Database.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
import org.bson.codecs.pojo.PojoCodecProvider;
1212
import org.bson.conversions.Bson;
1313
import org.jetbrains.annotations.NotNull;
14-
import technobot.data.cache.Config;
15-
import technobot.data.cache.Leveling;
16-
import technobot.data.cache.Suggestion;
14+
import technobot.data.cache.*;
1715
import technobot.data.cache.moderation.Moderation;
18-
import technobot.data.cache.Starboard;
1916

2017
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
2118
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
@@ -33,6 +30,7 @@ public class Database {
3330
public @NotNull MongoCollection<Starboard> starboard;
3431
public @NotNull MongoCollection<Leveling> leveling;
3532
public @NotNull MongoCollection<Config> config;
33+
public @NotNull MongoCollection<Greetings> greetings;
3634

3735
/**
3836
* Connect to database using MongoDB URI and
@@ -57,12 +55,14 @@ public Database(String uri) {
5755
starboard = database.getCollection("starboard", Starboard.class);
5856
leveling = database.getCollection("leveling", Leveling.class);
5957
config = database.getCollection("config", Config.class);
58+
greetings = database.getCollection("greetings", Greetings.class);
6059

6160
Bson guildIndex = Indexes.descending("guild");
6261
suggestions.createIndex(guildIndex);
6362
moderation.createIndex(guildIndex);
6463
starboard.createIndex(guildIndex);
6564
config.createIndex(guildIndex);
65+
greetings.createIndex(guildIndex);
6666
leveling.createIndex(Indexes.compoundIndex(guildIndex, Indexes.descending("user")));
6767
}
6868
}

src/main/java/technobot/data/GuildData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class GuildData {
3333
public ModerationHandler moderationHandler;
3434
public StarboardHandler starboardHandler;
3535
public LevelingHandler levelingHandler;
36+
public GreetingHandler greetingHandler;
3637

3738
/**
3839
* Represents the local memory cache of guild data stored in the MongoDB databases.
@@ -46,6 +47,7 @@ private GuildData(Guild guild) {
4647
moderationHandler = new ModerationHandler(bot, guild);
4748
starboardHandler = new StarboardHandler(bot, guild);
4849
levelingHandler = new LevelingHandler(bot, guild);
50+
greetingHandler = new GreetingHandler(bot, guild);
4951

5052
// Setup guild config
5153
config = bot.database.config.find(Filters.eq("guild", guild.getIdLong())).first();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package technobot.data.cache;
2+
3+
import org.bson.codecs.pojo.annotations.BsonProperty;
4+
5+
/**
6+
* POJO object that stores server greeting/farewell data.
7+
*
8+
* @author TechnoVision
9+
*/
10+
public class Greetings {
11+
12+
private long guild;
13+
14+
@BsonProperty("welcome_channel")
15+
private Long welcomeChannel;
16+
17+
private String greeting;
18+
19+
private String farewell;
20+
21+
@BsonProperty("join_dm")
22+
private String joinDM;
23+
24+
public Greetings() { }
25+
26+
public Greetings(long guild) {
27+
this.guild = guild;
28+
}
29+
30+
public long getGuild() {
31+
return guild;
32+
}
33+
34+
public void setGuild(long guild) {
35+
this.guild = guild;
36+
}
37+
38+
public Long getWelcomeChannel() {
39+
return welcomeChannel;
40+
}
41+
42+
public void setWelcomeChannel(Long welcomeChannel) {
43+
this.welcomeChannel = welcomeChannel;
44+
}
45+
46+
public String getGreeting() {
47+
return greeting;
48+
}
49+
50+
public void setGreeting(String greeting) {
51+
this.greeting = greeting;
52+
}
53+
54+
public String getFarewell() {
55+
return farewell;
56+
}
57+
58+
public void setFarewell(String farewell) {
59+
this.farewell = farewell;
60+
}
61+
62+
public String getJoinDM() {
63+
return joinDM;
64+
}
65+
66+
public void setJoinDM(String joinDM) {
67+
this.joinDM = joinDM;
68+
}
69+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package technobot.handlers;
2+
3+
import com.mongodb.client.model.Filters;
4+
import com.mongodb.client.model.Updates;
5+
import net.dv8tion.jda.api.entities.Guild;
6+
import org.bson.conversions.Bson;
7+
import technobot.TechnoBot;
8+
import technobot.data.cache.Greetings;
9+
10+
/**
11+
* Handles server messages (greeting, farewell, joinDM, etc).
12+
*
13+
* @author TechnoVision
14+
*/
15+
public class GreetingHandler {
16+
17+
private final Guild guild;
18+
private final TechnoBot bot;
19+
private final Bson filter;
20+
private Greetings greetings;
21+
22+
public GreetingHandler(TechnoBot bot, Guild guild) {
23+
this.bot = bot;
24+
this.guild = guild;
25+
26+
// Get POJO object from database
27+
filter = Filters.eq("guild", guild.getIdLong());
28+
this.greetings = bot.database.greetings.find(filter).first();
29+
if (greetings == null) {
30+
greetings = new Greetings(guild.getIdLong());
31+
bot.database.greetings.insertOne(greetings);
32+
}
33+
}
34+
35+
/**
36+
* Set a greeting message for this server.
37+
*
38+
* @param msg the message to send on join.
39+
*/
40+
public void setGreet(String msg) {
41+
greetings.setGreeting(msg);
42+
bot.database.greetings.updateOne(filter, Updates.set("greeting", msg));
43+
}
44+
45+
/**
46+
* Remove greeting message from this server.
47+
*/
48+
public void removeGreet() {
49+
greetings.setGreeting(null);
50+
bot.database.greetings.updateOne(filter, Updates.unset("greeting"));
51+
}
52+
}

0 commit comments

Comments
 (0)