Skip to content
This repository was archived by the owner on Apr 16, 2022. It is now read-only.

Commit fa6596a

Browse files
committed
Added CLI commands support and temporary commands system using message listener
1 parent 61310d6 commit fa6596a

File tree

10 files changed

+154
-5
lines changed

10 files changed

+154
-5
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

plugin/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ plugins {
77
id 'application'
88
}
99

10+
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17
11+
1012
repositories {
1113
mavenCentral()
1214
//JDA repository
@@ -17,6 +19,7 @@ repositories {
1719
}
1820

1921
dependencies {
22+
compileOnly 'org.jetbrains:annotations:23.0.0'
2023
implementation project(':pluginapi')
2124
}
2225

plugin/src/main/java/net/thesimpleteam/simplebotplugin/app/App.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package net.thesimpleteam.simplebotplugin.app;
55

66
import net.thesimpleteam.simplebotplugin.BasePlugin;
7+
import net.thesimpleteam.simplebotplugin.app.clicommands.HelloWorldCLICommand;
78
import net.thesimpleteam.simplebotplugin.app.listener.ExampleListener;
89

910
public class App extends BasePlugin {
@@ -23,6 +24,7 @@ public void onEnable() {
2324
System.out.println("Hello World from " + this.getName());
2425
System.out.println("This plugin is made by " + this.getAuthor());
2526
getLoader().addListener(this, new ExampleListener());
27+
getLoader().addCLICommand(new HelloWorldCLICommand());
2628
}
2729

2830
@Override
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.thesimpleteam.simplebotplugin.app.clicommands;
2+
3+
import net.thesimpleteam.simplebotplugin.commands.CLICommand;
4+
import net.thesimpleteam.simplebotplugin.commands.CommandEvent;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
public class HelloWorldCLICommand implements CLICommand {
9+
10+
@Override
11+
public @NotNull String name() {
12+
return "helloworld";
13+
}
14+
15+
@Override
16+
public @Nullable String help() {
17+
return null;
18+
}
19+
20+
@Override
21+
public void execute(CommandEvent event) {
22+
System.out.println("Hello World!");
23+
}
24+
}

plugin/src/main/java/net/thesimpleteam/simplebotplugin/app/listener/ExampleListener.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ public class ExampleListener implements Listener {
88

99
@EventHandler
1010
public void onMessageReceived(MessageReceiveEvent event) {
11-
event.reply("Hello World from " + event.getPlugin().getName() + " !");
11+
if(event.getMessage().startsWith("!!")) {
12+
String commandName = event.getMessage().substring(2);
13+
switch (commandName) {
14+
case "ping" -> event.reply("Pong from " + event.getPlugin().getName() + " !");
15+
case "plugin" -> event.reply("Plugin name: " + event.getPlugin().getName());
16+
sun.misc.Unsafe
17+
default -> event.reply("Unknown command " + commandName);
18+
}
19+
}
1220
}
1321

14-
}
22+
}

pluginapi/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ plugins {
77
id 'java-library'
88
}
99

10+
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17
11+
1012
repositories {
1113
mavenCentral()
1214
maven {
@@ -16,5 +18,6 @@ repositories {
1618
}
1719

1820
dependencies {
19-
implementation("net.dv8tion:JDA:4.4.0_350")
21+
compileOnly("net.dv8tion:JDA:4.4.0_350")
22+
compileOnly("com.google.guava:guava:31.0.1-jre")
2023
}

pluginapi/src/main/java/net/thesimpleteam/simplebotplugin/IPluginLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
package net.thesimpleteam.simplebotplugin;
2626

27+
import net.thesimpleteam.simplebotplugin.commands.CLICommand;
2728
import net.thesimpleteam.simplebotplugin.event.Event;
2829
import net.thesimpleteam.simplebotplugin.listener.Listener;
2930

3031
public interface IPluginLoader {
3132

3233
void callEvent(Event event);
3334
void addListener(BasePlugin plugin, Listener... listeners);
34-
35+
void addCLICommand(CLICommand... commands);
3536
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2021 minemobs
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package net.thesimpleteam.simplebotplugin.commands;
26+
27+
import org.jetbrains.annotations.NotNull;
28+
import org.jetbrains.annotations.Nullable;
29+
30+
public interface CLICommand {
31+
32+
@NotNull
33+
String name();
34+
35+
default String[] aliases() {
36+
return new String[]{name()};
37+
}
38+
39+
@Nullable
40+
String help();
41+
42+
void execute(CommandEvent event);
43+
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2021 minemobs
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package net.thesimpleteam.simplebotplugin.commands;
26+
27+
public record CommandEvent(String[] args, ICLI cli) {}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2021 minemobs
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package net.thesimpleteam.simplebotplugin.commands;
26+
27+
import com.google.common.collect.ImmutableList;
28+
29+
import java.io.IOException;
30+
import java.util.List;
31+
32+
public interface ICLI {
33+
34+
void commandsListener() throws InterruptedException, IOException;
35+
ImmutableList<CLICommand> getCommands();
36+
List<CLICommand> getPluginCommands();
37+
}

0 commit comments

Comments
 (0)