2828import io .codemc .bot .CodeMCBot ;
2929import io .codemc .bot .utils .APIUtil ;
3030import io .codemc .bot .utils .CommandUtil ;
31+ import kotlinx .serialization .json .JsonArray ;
3132import kotlinx .serialization .json .JsonObject ;
3233import kotlinx .serialization .json .JsonPrimitive ;
3334import net .dv8tion .jda .api .EmbedBuilder ;
4243import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
4344import net .dv8tion .jda .api .requests .ErrorResponse ;
4445
46+ import net .dv8tion .jda .api .utils .MarkdownUtil ;
47+ import org .jetbrains .annotations .NotNull ;
4548import org .jetbrains .annotations .VisibleForTesting ;
4649import org .slf4j .Logger ;
4750import org .slf4j .LoggerFactory ;
4851
4952import java .time .Instant ;
53+ import java .util .ArrayList ;
54+ import java .util .Arrays ;
5055import java .util .List ;
5156import java .util .concurrent .atomic .AtomicBoolean ;
5257import java .util .concurrent .atomic .AtomicInteger ;
@@ -70,7 +75,8 @@ public CmdCodeMC(CodeMCBot bot) {
7075 new Unlink (bot ),
7176 new ChangePassword (bot ),
7277 new CreateUser (bot ),
73- new DeleteUser (bot )
78+ new DeleteUser (bot ),
79+ new MyStatus (bot )
7480 };
7581 }
7682
@@ -362,13 +368,103 @@ private boolean validate(InteractionHook hook, String username, AtomicInteger co
362368
363369 if (!noJenkins )
364370 success &= JenkinsAPI .changeJenkinsPassword (username , password );
371+ } else {
372+ success &= NexusAPI .validatePrivileges (username );
365373 }
366374
367375 count .incrementAndGet ();
368376 return success ;
369377 }
370378 }
371379
380+ @ VisibleForTesting
381+ static class MyStatus extends BotCommand {
382+
383+ public MyStatus (CodeMCBot bot ) {
384+ super (bot );
385+
386+ this .name = "my-status" ;
387+ this .help = "Checks the status of your CodeMC Jenkins and Nexus accounts." ;
388+ this .aliases = new String []{"mystatus" };
389+
390+ List <Long > roles = new ArrayList <>();
391+ roles .add (bot .getConfigHandler ().getLong ("author_role" ));
392+ roles .addAll (bot .getConfigHandler ().getLongList ("allowed_roles" , "commands" , "codemc" ));
393+
394+ this .allowedRoles = roles ;
395+ }
396+
397+ @ Override
398+ public void withModalReply (SlashCommandEvent event ) {
399+ }
400+
401+ @ Override
402+ public void withHookReply (InteractionHook hook , SlashCommandEvent event , Guild guild , Member member ) {
403+ String username = DatabaseAPI .getAllUsers ().stream ()
404+ .filter (user -> user .getDiscord () == member .getIdLong ())
405+ .map (User ::getUsername )
406+ .findFirst ()
407+ .orElse (null );
408+
409+ if (username == null ) {
410+ CommandUtil .EmbedReply .from (hook ).error ("You are not linked to any Jenkins/Nexus accounts!" ).send ();
411+ return ;
412+ }
413+
414+ MessageEmbed embed = build (username );
415+ hook .sendMessageEmbeds (embed )
416+ .setEphemeral (true )
417+ .queue ();
418+ }
419+
420+ @ VisibleForTesting
421+ @ NotNull
422+ MessageEmbed build (String username ) {
423+ int totalUsers = JenkinsAPI .getAllJenkinsUsers ().size ();
424+ int totalRepositories = NexusAPI .getRepositories ().size ();
425+
426+ String jenkinsUrl = bot .getConfigHandler ().getString ("jenkins" , "url" );
427+ String nexusUrl = bot .getConfigHandler ().getString ("nexus" , "url" );
428+
429+ boolean jenkinsExists = JenkinsAPI .existsUser (username );
430+
431+ JsonObject nexusInfo = NexusAPI .getNexusRepository (username );
432+ boolean nexusExists = nexusInfo != null && !nexusInfo .isEmpty ();
433+
434+ EmbedBuilder embed = CommandUtil .getEmbed ()
435+ .setTitle ("CodeMC Account Status for " + username )
436+ .setDescription ("Total Jenkins Users: " + totalUsers + "\n Total Nexus Repositories: " + totalRepositories )
437+ .setTimestamp (Instant .now ());
438+
439+ if (jenkinsExists ) embed .addField ("Jenkins Account" , jenkinsUrl + "/job/" + username , true );
440+ else embed .addField ("Jenkins Account" , MarkdownUtil .underline ("Does Not Exist" ), true );
441+
442+ if (nexusExists ) {
443+ embed .addField ("Nexus Repository" , nexusUrl + "/#browse/browse:" + username .toLowerCase (), true );
444+
445+ JsonObject user = NexusAPI .getNexusUser (username .toLowerCase ());
446+ String userId = ((JsonPrimitive ) user .get ("userId" )).getContent ();
447+ String roles = ((JsonArray ) user .get ("roles" ))
448+ .stream ()
449+ .map (role -> ((JsonPrimitive ) role ).getContent ())
450+ .reduce ((a , b ) -> "- " + a + "\n - " + b )
451+ .orElse ("None" );
452+ embed .addField ("Nexus User ID" , userId , true );
453+ embed .addField ("Nexus Roles" , roles , false );
454+
455+ JsonObject role = NexusAPI .getNexusRole (username .toLowerCase ());
456+ String privileges = ((JsonArray ) role .get ("privileges" ))
457+ .stream ()
458+ .map (priv -> ((JsonPrimitive ) priv ).getContent ())
459+ .reduce ((a , b ) -> "- " + a + "\n - " + b )
460+ .orElse ("None" );
461+ embed .addField ("Nexus Privileges" , privileges , false );
462+ } else embed .addField ("Nexus Repository" , MarkdownUtil .underline ("Does Not Exist" ), true );
463+
464+ return embed .build ();
465+ }
466+ }
467+
372468 @ VisibleForTesting
373469 static class Link extends BotCommand {
374470
@@ -468,11 +564,6 @@ public void withHookReply(InteractionHook hook, SlashCommandEvent event, Guild g
468564 return ;
469565 }
470566
471- if (DatabaseAPI .getUser (username ) == null ) {
472- CommandUtil .EmbedReply .from (hook ).error ("The user is not linked to any Jenkins/Nexus account!" ).send ();
473- return ;
474- }
475-
476567 DatabaseAPI .removeUser (username );
477568 CommandUtil .EmbedReply .from (hook ).success ("Unlinked Discord User " + target .getUser ().getEffectiveName () + " from their Jenkins/Nexus account!" ).send ();
478569
0 commit comments