1313import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1414import net .dv8tion .jda .api .hooks .ListenerAdapter ;
1515import net .dv8tion .jda .api .interactions .commands .Command ;
16+ import net .dv8tion .jda .api .interactions .commands .OptionType ;
1617import net .dv8tion .jda .api .interactions .commands .build .Commands ;
1718import net .dv8tion .jda .api .requests .restaction .ChannelAction ;
1819import org .jetbrains .annotations .NotNull ;
@@ -85,49 +86,17 @@ public void run() {
8586 }
8687
8788 @ Override
88- public void onMessageReceived (MessageReceivedEvent event )
89- {
89+ public void onMessageReceived (MessageReceivedEvent event ) {
9090 Message message = event .getMessage ();
9191 MessageChannel channel = event .getChannel ();
9292 String content = message .getContentRaw ();
9393 User user = event .getAuthor ();
9494 Member member = event .getMember ();
9595 if (!event .isFromType (ChannelType .PRIVATE ) && !user .isBot ()){
96- //_____________________________
97- // Magische Miesmuschel
98- //_____________________________
99- if (content .equalsIgnoreCase (prefix + "miesmuschel" )){
100- int antwortType = rand .nextInt (3 );
101- if (antwortType == 0 ){
102- //positive Antwort
103- channel .sendMessage (positiveAntworten [rand .nextInt (positiveAntworten .length )]).queue ();
104- }else if (antwortType == 1 ){
105- //neutrale Antwort
106- channel .sendMessage (neutraleAntworten [rand .nextInt (neutraleAntworten .length )]).queue ();
107- }else {
108- //negative Antwort
109- channel .sendMessage (negativeAntworten [rand .nextInt (negativeAntworten .length )]).queue ();
110- }
111- }
112- //_____________________________
113- // Random Antwort
114- //_____________________________
115- else if (content .toLowerCase ().startsWith (prefix + "rand" )){
116- String arguments = "" ;
117- try {
118- arguments = content .substring (6 );
119- } catch (Exception ignore ){}
120- if (arguments .equals ("" )){
121- channel .sendMessage ("Please give Arguments" ).queue ();
122- return ;
123- }
124- String [] auswahl = arguments .split ("," );
125- channel .sendMessage (auswahl [rand .nextInt (auswahl .length )]).queue ();
126- }
12796 //_____________________________
12897 //Add Master
12998 //_____________________________
130- else if (content .startsWith (prefix + "addMaster" )){
99+ if (content .startsWith (prefix + "addMaster" )){
131100 if (member != null && member .hasPermission (Permission .ADMINISTRATOR )) {
132101 long channelid = -1L ;
133102 try {
@@ -279,16 +248,6 @@ else if (content.startsWith(prefix + "recreateTmpChannels")){
279248 channel .sendMessage ("You are not an Administrator" ).queue ();
280249 }
281250 }
282- //_____________________________
283- //Setup
284- //_____________________________
285- else if (content .startsWith (prefix + "setup" )){
286- if (member != null && member .hasPermission (Permission .ADMINISTRATOR )) {
287- guild = event .getGuild ();
288- saveConfig ();
289- channel .sendMessage ("Setup done" ).queue ();
290- }
291- }
292251 }
293252 }
294253
@@ -315,16 +274,74 @@ else if (event.getChannelLeft() != null && event.getChannelJoined() != null){
315274 @ Override
316275 public void onSlashCommandInteraction (@ NotNull SlashCommandInteractionEvent event ) {
317276 User user = event .getUser ();
277+ String command = event .getName ();
318278
319- if (event .getName ().equals ("ping" )){
279+ //_____________________________
280+ //Ping
281+ //_____________________________
282+ if (command .equals ("ping" )){
320283 event .reply ("Pong!" ).queue ();
321- } else if (event .getName ().equals ("mypicture" )) {
284+ }
285+
286+ //_____________________________
287+ //mypicture
288+ //_____________________________
289+ else if (command .equals ("mypicture" )) {
322290 if (user .getAvatarUrl () != null ){
323291 event .reply (user .getAvatarUrl ()).queue ();
324292 }else {
325293 event .reply ("No Profile Picture found" ).queue ();
326294 }
327295 }
296+
297+ //_____________________________
298+ //miesmuschel
299+ //_____________________________
300+ else if (command .equals ("miesmuschel" )){
301+ int antwortType = rand .nextInt (3 );
302+ if (antwortType == 0 ){
303+ //positive Antwort
304+ event .reply (positiveAntworten [rand .nextInt (positiveAntworten .length )]).queue ();
305+ }else if (antwortType == 1 ){
306+ //neutrale Antwort
307+ event .reply (neutraleAntworten [rand .nextInt (neutraleAntworten .length )]).queue ();
308+ }else {
309+ //negative Antwort
310+ event .reply (negativeAntworten [rand .nextInt (negativeAntworten .length )]).queue ();
311+ }
312+ }
313+
314+ //_____________________________
315+ //random
316+ //_____________________________
317+ else if (command .equals ("rand" )) {
318+ String arguments = event .getOption ("options" ) != null ? event .getOption ("options" ).getAsString () : "" ;
319+ if (arguments .equals ("" )){
320+ event .reply ("No Options provided" ).queue ();
321+ return ;
322+ }
323+ String [] auswahl = arguments .split ("," );
324+ event .reply (auswahl [rand .nextInt (auswahl .length )]).queue ();
325+ }
326+
327+ else if (command .equals ("setup" ) && !event .isFromGuild ()){
328+ event .reply ("You have to be on an Server" ).queue ();
329+ }
330+
331+ //_____________________________
332+ //Guild Commands
333+ //_____________________________
334+ else if (event .isFromGuild ()){
335+ //_____________________________
336+ //Setup
337+ //_____________________________
338+ if (command .equals ("setup" )){
339+ guild = event .getGuild ();
340+ saveConfig ();
341+ addGuildSlashCommands ();
342+ event .reply ("Setup done" ).queue ();
343+ }
344+ }
328345 super .onSlashCommandInteraction (event );
329346 }
330347
@@ -461,6 +478,9 @@ public void readConfig(){
461478 while (line != null ){
462479 if (line .startsWith ("GuildId:" )){
463480 guild = jda .getGuildById (line .split (":" )[1 ]);
481+ if (guild != null ){
482+ addGuildSlashCommands ();
483+ }
464484 }else if (line .startsWith ("DebugChannelId:" )){
465485 debugChannel = jda .getTextChannelById (line .split (":" )[1 ]);
466486 }else if (line .startsWith ("MasterChannels:" )){
@@ -492,20 +512,41 @@ public void readConfig(){
492512 public void addGlobalSlashCommands (){
493513 //check if command exists
494514 List <Command > commands = jda .retrieveCommands ().complete ();
495- final String [] commandNames = {"ping" ,"myPicture" };
515+ final String [] commandNames = {"ping" ,"myPicture" , "miesmuschel" , "rand" , "setup" };
496516 int complete = 0 ;
497517 for (Command c : commands ){
498518 if (Arrays .binarySearch (commandNames ,c .getName ()) >= 0 ){
499519 complete ++;
500520 }
501521 }
502- if (complete ! = commandNames .length ){
522+ if (complete > = commandNames .length ){
503523 //add commands
504524 jda .updateCommands ().addCommands (
505525 Commands .slash ("ping" , "Ping the bot" ),
506- Commands .slash ("mypicture" , "Get your profile picture" )
526+ Commands .slash ("mypicture" , "Get your profile picture" ),
527+ Commands .slash ("miesmuschel" , "Ask the magic miesmuschel" ),
528+ Commands .slash ("rand" , "Get a random Answer of the given Options" )
529+ .addOption (OptionType .STRING , "options" , "The Options you want to choose from (Comma Seperated)" , true ),
530+ Commands .slash ("setup" , "Run this on your Server for Setup" )
507531 ).queue ();
508532 }
509533 }
510534
535+ public void addGuildSlashCommands (){
536+ //check if command exists
537+ List <Command > commands = jda .retrieveCommands ().complete ();
538+ final String [] commandNames = {};
539+ int complete = 0 ;
540+ for (Command c : commands ){
541+ if (Arrays .binarySearch (commandNames ,c .getName ()) >= 0 ){
542+ complete ++;
543+ }
544+ }
545+ if (complete >= commandNames .length ){
546+ //add commands
547+ guild .updateCommands ().addCommands (
548+ //Commands.slash("ping", "Ping the bot")
549+ ).queue ();
550+ }
551+ }
511552}
0 commit comments