Skip to content

Commit a642d7b

Browse files
Add the /auto-role command and backend
1 parent ce1cb07 commit a642d7b

14 files changed

Lines changed: 289 additions & 52 deletions

src/main/java/technobot/commands/Category.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ public enum Category {
1313
ECONOMY(":moneybag:", "Economy"),
1414
STARBOARD(":star:", "Starboard"),
1515
FUN(":smile:", "Fun"),
16+
AUTOMATION(":gear:", "Automation"),
1617
UTILITY(":tools:", "Utility"),
1718
GREETINGS(":wave:", "Greetings"),
18-
SUGGESTIONS(":thought_balloon:", "Suggestions");
19+
SUGGESTIONS(":thought_balloon:", "Suggestions"),
20+
CASINO(":game_die:", "Casino"),
21+
PETS(":dog:", "Pets");
1922

2023
public final String emoji;
2124
public final String name;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
1212
import org.jetbrains.annotations.NotNull;
1313
import technobot.TechnoBot;
14+
import technobot.commands.automation.AutoRoleCommand;
1415
import technobot.commands.economy.*;
1516
import technobot.commands.fun.*;
1617
import technobot.commands.greetings.FarewellCommand;
@@ -51,6 +52,9 @@ public class CommandRegistry extends ListenerAdapter {
5152
*/
5253
public CommandRegistry(TechnoBot bot) {
5354
mapCommand(
55+
//Automation commands
56+
new AutoRoleCommand(bot),
57+
5458
//Economy commands
5559
new WorkCommand(bot),
5660
new CrimeCommand(bot),
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package technobot.commands.automation;
2+
3+
import net.dv8tion.jda.api.EmbedBuilder;
4+
import net.dv8tion.jda.api.Permission;
5+
import net.dv8tion.jda.api.entities.MessageEmbed;
6+
import net.dv8tion.jda.api.entities.Role;
7+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
8+
import net.dv8tion.jda.api.interactions.commands.OptionType;
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.handlers.ConfigHandler;
15+
import technobot.util.embeds.EmbedColor;
16+
import technobot.util.embeds.EmbedUtils;
17+
18+
import java.util.Set;
19+
20+
/**
21+
* Command that sets roles to be given on user join.
22+
*
23+
* @author TechnoVision
24+
*/
25+
public class AutoRoleCommand extends Command {
26+
27+
public static final int MAX_AUTO_ROLES = 10;
28+
29+
public AutoRoleCommand(TechnoBot bot) {
30+
super(bot);
31+
this.name = "auto-role";
32+
this.description = "Set roles to be given to all new members.";
33+
this.category = Category.AUTOMATION;
34+
this.permission = Permission.MANAGE_SERVER;
35+
this.botPermission = Permission.MANAGE_ROLES;
36+
this.subCommands.add(new SubcommandData("add", "Add a role to be given to new members on joining.")
37+
.addOption(OptionType.ROLE, "role", "The role to be given", true));
38+
this.subCommands.add(new SubcommandData("remove", "Remove a role from the auto-role list.")
39+
.addOption(OptionType.ROLE, "role", "The role to remove", true));
40+
this.subCommands.add(new SubcommandData("list", "List the current auto-roles for this server."));
41+
}
42+
43+
@Override
44+
public void execute(SlashCommandInteractionEvent event) {
45+
ConfigHandler configHandler = GuildData.get(event.getGuild()).configHandler;
46+
MessageEmbed embed = null;
47+
switch (event.getSubcommandName()) {
48+
case "add" -> {
49+
Role role = event.getOption("role").getAsRole();
50+
if (role.isManaged() || role.isPublicRole() || role.getPosition() >= event.getGuild().getBotRole().getPosition()) {
51+
event.replyEmbeds(EmbedUtils.createError("I cannot give out roles that have a higher position than me!")).setEphemeral(true).queue();
52+
return;
53+
}
54+
if (configHandler.getConfig().getAutoRoles().size() >= 1 && !configHandler.isPremium()) {
55+
event.replyEmbeds(EmbedUtils.createError("You can set multiple auto-roles with premium! For more info, use `/premium`.")).setEphemeral(true).queue();
56+
return;
57+
}
58+
if (configHandler.getConfig().getAutoRoles().size() == MAX_AUTO_ROLES) {
59+
event.replyEmbeds(EmbedUtils.createError("You have hit the maximum number of auto-roles for this guild!")).setEphemeral(true).queue();
60+
return;
61+
}
62+
embed = EmbedUtils.createDefault(EmbedUtils.BLUE_TICK + " The <@&"+role.getId()+"> role will be given to all new members when they join the server.");
63+
configHandler.addAutoRole(role.getIdLong());
64+
}
65+
case "remove" -> {
66+
Role role = event.getOption("role").getAsRole();
67+
if (!configHandler.getConfig().getAutoRoles().contains(role.getIdLong())) {
68+
event.replyEmbeds(EmbedUtils.createError("The <@&"+role.getId()+"> role is not set as an auto-role.")).setEphemeral(true).queue();
69+
return;
70+
}
71+
embed = EmbedUtils.createDefault(EmbedUtils.BLUE_X + " The <@&"+role.getId()+"> role will no longer be given to new members when they join the server.");
72+
if (!configHandler.isPremium()) {
73+
configHandler.clearAutoRoles();
74+
} else {
75+
configHandler.removeAutoRole(role.getIdLong());
76+
}
77+
}
78+
case "list" -> {
79+
EmbedBuilder embedBuilder = new EmbedBuilder().setTitle("Auto Roles").setColor(EmbedColor.DEFAULT.color);
80+
Set<Long> roles = configHandler.getConfig().getAutoRoles();
81+
if (roles == null || roles.isEmpty()) {
82+
embedBuilder.setDescription("Use `/auto-role add <role>` to set your first auto role!");
83+
} else {
84+
int max = configHandler.isPremium() ? MAX_AUTO_ROLES : 1;
85+
if (max == 1) {
86+
embedBuilder.appendDescription("Add additional roles with `/premium`\n");
87+
} else {
88+
embedBuilder.appendDescription("There are "+roles.size()+" auto roles given to new members:\n");
89+
}
90+
int count = 0;
91+
for (long roleID : roles) {
92+
if (event.getGuild().getRoleById(roleID) != null) {
93+
count++;
94+
embedBuilder.appendDescription("\n**"+count+".** <@&"+roleID+">");
95+
if (count == max) break;
96+
}
97+
}
98+
}
99+
embed = embedBuilder.build();
100+
}
101+
}
102+
event.replyEmbeds(embed).queue();
103+
}
104+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void execute(SlashCommandInteractionEvent event) {
6262
event.deferReply().queue();
6363
Bson filter = Filters.eq("guild", event.getGuild().getIdLong());
6464
GuildData data = GuildData.get(event.getGuild());
65-
Config config = data.config;
65+
Config config = data.configHandler.getConfig();
6666

6767
String text = "";
6868
Bson update = null;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public void execute(SlashCommandInteractionEvent event) {
9696
BufferedImage background;
9797
if (!profile.getBackground().isEmpty()) {
9898
background = ImageIO.read(new URL(profile.getBackground()));
99-
} else if (data.config.getLevelingBackground() != null) {
100-
background = ImageIO.read(new URL(data.config.getLevelingBackground()));
99+
} else if (data.configHandler.getConfig().getLevelingBackground() != null) {
100+
background = ImageIO.read(new URL(data.configHandler.getConfig().getLevelingBackground()));
101101
} else {
102102
background = ImageIO.read(cl.getResource(PATH + "background.png"));
103103
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public RewardsCommand(TechnoBot bot) {
3333
public void execute(SlashCommandInteractionEvent event) {
3434
event.deferReply().queue();
3535
LinkedHashMap<String, Integer> rewards = new LinkedHashMap<>();
36-
GuildData.get(event.getGuild()).config.getRewards().entrySet()
36+
GuildData.get(event.getGuild()).configHandler.getConfig().getRewards().entrySet()
3737
.stream()
3838
.sorted(Map.Entry.comparingByValue())
3939
.forEachOrdered(x -> rewards.put(x.getKey(), x.getValue()));

src/main/java/technobot/commands/staff/MuteRoleCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public void execute(SlashCommandInteractionEvent event) {
5757
case "set" -> {
5858
// Set existing role as the mute role
5959
Role role = event.getOption("role").getAsRole();
60+
if (role.isManaged() || role.isPublicRole()) {
61+
event.getHook().sendMessageEmbeds(EmbedUtils.createError("I cannot set bot/managed roles as the mute role!")).setEphemeral(true).queue();
62+
return;
63+
}
6064
data.moderationHandler.setMuteRole(role.getIdLong());
6165
String text = EmbedUtils.BLUE_TICK + " The "+role.getAsMention()+" role will be used for the `mute` command.";
6266
event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue();

src/main/java/technobot/commands/staff/RoleCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void execute(SlashCommandInteractionEvent event) {
4343
event.replyEmbeds(EmbedUtils.createError("That user is not in your server!")).setEphemeral(true).queue();
4444
return;
4545
}
46-
if (role.isManaged()) {
46+
if (role.isManaged() || role.isPublicRole()) {
4747
event.replyEmbeds(EmbedUtils.createError("I cannot give/remove bot or managed roles!")).setEphemeral(true).queue();
4848
return;
4949
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package technobot.data;
22

3-
import com.mongodb.client.model.Filters;
43
import net.dv8tion.jda.api.entities.Guild;
54
import org.jetbrains.annotations.NotNull;
65
import technobot.TechnoBot;
7-
import technobot.data.cache.Config;
86
import technobot.handlers.*;
97
import technobot.handlers.economy.EconomyHandler;
108

@@ -27,14 +25,14 @@ public class GuildData {
2725
private static boolean initialized;
2826

2927
/** Local memory caches. */
30-
public Config config;
3128
public MusicHandler musicHandler;
3229
public SuggestionHandler suggestionHandler;
3330
public ModerationHandler moderationHandler;
3431
public StarboardHandler starboardHandler;
3532
public LevelingHandler levelingHandler;
3633
public GreetingHandler greetingHandler;
3734
public EconomyHandler economyHandler;
35+
public ConfigHandler configHandler;
3836

3937
/**
4038
* Represents the local memory cache of guild data stored in the MongoDB databases.
@@ -50,13 +48,7 @@ private GuildData(Guild guild) {
5048
levelingHandler = new LevelingHandler(bot, guild);
5149
greetingHandler = new GreetingHandler(bot, guild);
5250
economyHandler = new EconomyHandler(bot, guild);
53-
54-
// Setup guild config
55-
config = bot.database.config.find(Filters.eq("guild", guild.getIdLong())).first();
56-
if (config == null) {
57-
config = new Config(guild.getIdLong());
58-
bot.database.config.insertOne(config);
59-
}
51+
configHandler = new ConfigHandler(bot, guild);
6052
}
6153

6254
/**

src/main/java/technobot/data/cache/Config.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.bson.codecs.pojo.annotations.BsonProperty;
44

55
import java.util.HashMap;
6+
import java.util.HashSet;
67
import java.util.Map;
8+
import java.util.Set;
79

810
/**
911
* POJO object that stores config data for a guild.
@@ -14,6 +16,8 @@ public class Config {
1416

1517
private long guild;
1618

19+
private Long premium;
20+
1721
@BsonProperty("leveling_channel")
1822
private Long levelingChannel;
1923

@@ -34,17 +38,24 @@ public class Config {
3438

3539
private Map<String,Integer> rewards;
3640

37-
public Config() { }
41+
@BsonProperty("auto_roles")
42+
private Set<Long> autoRoles;
43+
44+
public Config() {
45+
autoRoles = new HashSet<>();
46+
}
3847

3948
public Config(long guild) {
4049
this.guild = guild;
41-
levelingChannel = null;
42-
levelingMessage = null;
43-
levelingDM = false;
44-
levelingMod = 1;
45-
levelingMute = false;
46-
levelingBackground = null;
47-
rewards = new HashMap<>();
50+
this.premium = null;
51+
this.levelingChannel = null;
52+
this.levelingMessage = null;
53+
this.levelingDM = false;
54+
this.levelingMod = 1;
55+
this.levelingMute = false;
56+
this.levelingBackground = null;
57+
this.rewards = new HashMap<>();
58+
this.autoRoles = new HashSet<>();
4859
}
4960

5061
public long getGuild() {
@@ -55,6 +66,14 @@ public void setGuild(long guild) {
5566
this.guild = guild;
5667
}
5768

69+
public Long getPremium() {
70+
return premium;
71+
}
72+
73+
public void setPremium(Long premium) {
74+
this.premium = premium;
75+
}
76+
5877
public Long getLevelingChannel() {
5978
return levelingChannel;
6079
}
@@ -116,4 +135,18 @@ public void addReward(int level, String roleID) {
116135
}
117136

118137
public void removeReward(String roleID) { this.rewards.remove(roleID); }
138+
139+
public Set<Long> getAutoRoles() {
140+
return autoRoles;
141+
}
142+
143+
public void setAutoRoles(Set<Long> autoRoles) {
144+
this.autoRoles = autoRoles;
145+
}
146+
147+
public void addAutoRole(long roleID) {
148+
this.autoRoles.add(roleID);
149+
}
150+
151+
public void removeAutoRole(long roleID) { this.autoRoles.remove(roleID); }
119152
}

0 commit comments

Comments
 (0)