Skip to content

Commit 828d25d

Browse files
authored
Merge branch 'master' of https://github.com/CodeMC/Bot into feat/unit-testing
2 parents 93a18b8 + 94bd102 commit 828d25d

File tree

4 files changed

+337
-189
lines changed

4 files changed

+337
-189
lines changed

src/main/java/io/codemc/bot/commands/CmdApplication.java

Lines changed: 3 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,20 @@
2020

2121
import com.jagrosh.jdautilities.command.SlashCommand;
2222
import com.jagrosh.jdautilities.command.SlashCommandEvent;
23-
import io.codemc.api.database.DatabaseAPI;
2423
import io.codemc.bot.CodeMCBot;
25-
import io.codemc.bot.utils.APIUtil;
24+
import io.codemc.bot.utils.ApplicationHandler;
2625
import io.codemc.bot.utils.CommandUtil;
27-
import net.dv8tion.jda.api.EmbedBuilder;
2826
import net.dv8tion.jda.api.entities.Guild;
2927
import net.dv8tion.jda.api.entities.Member;
30-
import net.dv8tion.jda.api.entities.MessageEmbed;
31-
import net.dv8tion.jda.api.entities.Role;
32-
import net.dv8tion.jda.api.entities.User;
33-
import net.dv8tion.jda.api.entities.MessageEmbed.Footer;
34-
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
35-
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
36-
import net.dv8tion.jda.api.exceptions.ErrorHandler;
3728
import net.dv8tion.jda.api.interactions.InteractionHook;
3829
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
3930
import net.dv8tion.jda.api.interactions.commands.OptionType;
4031
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
41-
import net.dv8tion.jda.api.requests.ErrorResponse;
42-
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
43-
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
44-
import org.slf4j.Logger;
45-
import org.slf4j.LoggerFactory;
4632

4733
import java.util.List;
48-
import java.util.regex.Matcher;
49-
import java.util.regex.Pattern;
5034

5135
public class CmdApplication extends BotCommand{
5236

53-
public static final Pattern GITHUB_URL_PATTERN = Pattern.compile("^https://github\\.com/([a-zA-Z0-9-]+)/([a-zA-Z0-9-_.]+?)(?:\\.git)?(?:/.*)?$");
54-
55-
private static final Logger LOGGER = LoggerFactory.getLogger(CmdApplication.class);
56-
5737
public CmdApplication(CodeMCBot bot){
5838
super(bot);
5939

@@ -74,168 +54,6 @@ public void withModalReply(SlashCommandEvent event){}
7454
@Override
7555
public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild guild, Member member){}
7656

77-
public static void handle(CodeMCBot bot, InteractionHook hook, Guild guild, long messageId, String str, boolean accepted){
78-
TextChannel requestChannel = guild.getTextChannelById(bot.getConfigHandler().getLong("channels", "request_access"));
79-
if(requestChannel == null){
80-
CommandUtil.EmbedReply.from(hook).error("Unable to retrieve `request-access` channel.").send();
81-
return;
82-
}
83-
84-
requestChannel.retrieveMessageById(messageId).queue(message -> {
85-
List<MessageEmbed> embeds = message.getEmbeds();
86-
if(embeds.isEmpty()){
87-
CommandUtil.EmbedReply.from(hook).error("Provided message does not have any embeds.").send();
88-
return;
89-
}
90-
91-
MessageEmbed embed = embeds.get(0);
92-
Footer footer = embed.getFooter();
93-
if(footer == null || embed.getFields().isEmpty()){
94-
CommandUtil.EmbedReply.from(hook).error("Embed does not have a footer or any Embed Fields.").send();
95-
return;
96-
}
97-
98-
String footerText = footer.getText();
99-
if(footerText == null || footerText.isEmpty()){
100-
CommandUtil.EmbedReply.from(hook).error("Embed does not have a valid footer.").send();
101-
return;
102-
}
103-
104-
String userId = footerText.trim();
105-
String userLink = null;
106-
String repoLink = null;
107-
for(MessageEmbed.Field field : embed.getFields()){
108-
String name = field.getName();
109-
if(name == null || field.getValue() == null)
110-
continue;
111-
112-
if(name.equalsIgnoreCase("user/organisation:")){
113-
userLink = field.getValue();
114-
}else
115-
if(name.equalsIgnoreCase("repository:")){
116-
String link = field.getValue();
117-
if (link == null || link.isEmpty()) {
118-
CommandUtil.EmbedReply.from(hook).error("Repository field is empty!").send();
119-
return;
120-
}
121-
122-
String url = link.substring(link.indexOf("(") + 1, link.indexOf(")"));
123-
124-
repoLink = url.isEmpty() ? link : url;
125-
}
126-
}
127-
128-
if(userLink == null || repoLink == null){
129-
CommandUtil.EmbedReply.from(hook).error("Embed does not have any valid Fields.").send();
130-
return;
131-
}
132-
133-
TextChannel channel = guild.getTextChannelById(accepted
134-
? bot.getConfigHandler().getLong("channels", "accepted_requests")
135-
: bot.getConfigHandler().getLong("channels", "rejected_requests")
136-
);
137-
if(channel == null){
138-
CommandUtil.EmbedReply.from(hook)
139-
.error("Unable to retrieve `" + (accepted ? "accepted" : "rejected") + "-requests` channel.")
140-
.send();
141-
return;
142-
}
143-
144-
Matcher matcher = GITHUB_URL_PATTERN.matcher(repoLink);
145-
if (!matcher.matches()) {
146-
CommandUtil.EmbedReply.from(hook)
147-
.error("The user/organisation or repository name is invalid!")
148-
.send();
149-
return;
150-
}
151-
152-
String username = matcher.group(1);
153-
String project = matcher.group(2);
154-
String jenkinsUrl = bot.getConfigHandler().getString("jenkins", "url") + "/job/" + username + "/job/" + project + "/";
155-
Member member = guild.getMemberById(userId);
156-
157-
if (accepted) {
158-
String password = APIUtil.newPassword();
159-
boolean jenkinsSuccess = APIUtil.createJenkinsJob(hook, username, password, project, repoLink);
160-
boolean nexusSuccess = APIUtil.createNexus(hook, username, password);
161-
if (!nexusSuccess || !jenkinsSuccess) return;
162-
163-
if (member == null)
164-
LOGGER.warn("Member with ID '{}' not found!", userId);
165-
else {
166-
if (DatabaseAPI.getUser(username) == null)
167-
DatabaseAPI.addUser(username, member.getIdLong());
168-
}
169-
}
170-
171-
channel.sendMessage(getMessage(bot, userId, userLink, repoLink, str == null ? jenkinsUrl : str, hook.getInteraction().getUser(), accepted)).queue(m -> {
172-
ThreadChannel thread = message.getStartedThread();
173-
if(thread != null && !thread.isArchived()){
174-
thread.getManager().setArchived(true)
175-
.reason("Archiving Thread of deleted Request message.")
176-
.queue();
177-
}
178-
179-
message.delete().queue();
180-
181-
if(!accepted){
182-
CommandUtil.EmbedReply.from(hook)
183-
.success("Denied Application of " + (member == null ? "Unknown" : member.getUser().getEffectiveName()) + "!")
184-
.send();
185-
return;
186-
}
187-
188-
Role authorRole = guild.getRoleById(bot.getConfigHandler().getLong("author_role"));
189-
if(authorRole == null){
190-
CommandUtil.EmbedReply.from(hook)
191-
.error("Unable to retrieve Author Role!")
192-
.send();
193-
return;
194-
}
195-
196-
if(member == null){
197-
CommandUtil.EmbedReply.from(hook)
198-
.error("Unable to apply Role. Member not found!")
199-
.send();
200-
return;
201-
}
202-
203-
guild.addRoleToMember(member, authorRole)
204-
.reason("[Access Request] Application accepted.")
205-
.queue(
206-
v -> CommandUtil.EmbedReply.from(hook)
207-
.success("Accepted application of " + member.getUser().getEffectiveName() + "!")
208-
.send(),
209-
new ErrorHandler()
210-
.handle(
211-
ErrorResponse.MISSING_PERMISSIONS,
212-
e -> CommandUtil.EmbedReply.from(hook)
213-
.appendWarning("I lack the `Manage Roles` permission to apply the role.")
214-
.send()
215-
)
216-
);
217-
});
218-
});
219-
}
220-
221-
private static MessageCreateData getMessage(CodeMCBot bot, String userId, String userLink, String repoLink, String str, User reviewer, boolean accepted){
222-
String msg = String.join("\n", bot.getConfigHandler().getStringList("messages", (accepted ? "accepted" : "denied")));
223-
224-
MessageEmbed embed = new EmbedBuilder()
225-
.setColor(accepted ? 0x00FF00 : 0xFF0000)
226-
.setDescription(msg)
227-
.addField("User/Organisation:", userLink, true)
228-
.addField("Repository:", repoLink, true)
229-
.addField("Reviewer:", reviewer.getAsMention(), true)
230-
.addField(accepted ? "New Project:" : "Reason:", str, false)
231-
.build();
232-
233-
return new MessageCreateBuilder()
234-
.addContent("<@" + userId + ">")
235-
.setEmbeds(embed)
236-
.build();
237-
}
238-
23957
private static class Accept extends BotCommand{
24058

24159
public Accept(CodeMCBot bot){
@@ -274,7 +92,7 @@ public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild g
27492
return;
27593
}
27694

277-
handle(bot, hook, guild, messageId, null, true);
95+
ApplicationHandler.handle(bot, hook, guild, messageId, null, true);
27896
} catch (NumberFormatException e) {
27997
CommandUtil.EmbedReply.from(hook).error("Invalid message ID!").send();
28098
}
@@ -322,7 +140,7 @@ public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild g
322140
return;
323141
}
324142

325-
handle(bot, hook, guild, messageId, reason, false);
143+
ApplicationHandler.handle(bot, hook, guild, messageId, reason, false);
326144
} catch (NumberFormatException e) {
327145
CommandUtil.EmbedReply.from(hook).error("Invalid message ID!").send();
328146
}

src/main/java/io/codemc/bot/listeners/ButtonListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package io.codemc.bot.listeners;
2020

2121
import io.codemc.bot.CodeMCBot;
22-
import io.codemc.bot.commands.CmdApplication;
22+
import io.codemc.bot.utils.ApplicationHandler;
2323
import io.codemc.bot.utils.CommandUtil;
2424
import net.dv8tion.jda.api.entities.Guild;
2525
import net.dv8tion.jda.api.entities.Member;
@@ -106,7 +106,7 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent event){
106106
}
107107

108108
event.deferReply(true).queue(
109-
hook -> CmdApplication.handle(bot, hook, guild, event.getMessageIdLong(), null, true)
109+
hook -> ApplicationHandler.handle(bot, hook, guild, event.getMessageIdLong(), null, true)
110110
);
111111
}else{
112112
if(lacksRole(roleIds, denyApplicationRoles)){

src/main/java/io/codemc/bot/listeners/ModalListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import io.codemc.api.jenkins.JenkinsAPI;
2222
import io.codemc.bot.CodeMCBot;
23-
import io.codemc.bot.commands.CmdApplication;
23+
import io.codemc.bot.utils.ApplicationHandler;
2424
import io.codemc.bot.utils.CommandUtil;
2525
import net.dv8tion.jda.api.entities.Guild;
2626
import net.dv8tion.jda.api.entities.Message;
@@ -253,7 +253,7 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event){
253253
if(reason == null || reason.isEmpty())
254254
reason = "*No reason provided*";
255255

256-
CmdApplication.handle(this.bot, hook, guild, messageId, reason, false);
256+
ApplicationHandler.handle(this.bot, hook, guild, messageId, reason, false);
257257
});
258258

259259
default -> CommandUtil.EmbedReply.from(event)

0 commit comments

Comments
 (0)