-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotExample.java
More file actions
135 lines (108 loc) · 6.16 KB
/
BotExample.java
File metadata and controls
135 lines (108 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package dev.spoocy;
import dev.spoocy.jdaextensions.commands.arguments.Arguments;
import dev.spoocy.jdaextensions.commands.arguments.ProvidedArgument;
import dev.spoocy.jdaextensions.commands.manager.impl.DefaultCommandManager;
import dev.spoocy.jdaextensions.commands.tree.CommandTree;
import dev.spoocy.jdaextensions.core.BotSettings;
import dev.spoocy.jdaextensions.core.SingleShardDiscordBot;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.SubscribeEvent;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
/**
* @author Spoocy99 | GitHub: Spoocy99
*/
public class BotExample extends SingleShardDiscordBot {
public static void main(String[] args) {
BotSettings settings = BotSettings.builder()
.setActivity(i -> Activity.playing("Testing..."))
.setIntents(GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MESSAGES)
.setAutoLogin(false) // disable auto-login, have to manually call login() after creating the bot instance
.setCommandManager(
DefaultCommandManager.builder()
// Add commands using the CommandTree builder
.register(
// Command: /ping
new CommandTree("ping", "Replies with Pong!")
.executes(context -> {
context.reply("Pong!");
})
.build()
)
.register(
new CommandTree("test", "Second command")
// Command: /test first
.then(CommandTree.command("first", "First subcommand")
.arg(
Arguments.string("input", "Some input", false, false)
.choice("Option 1", "option1")
.choice("Option 2", "option2")
)
.executes(context -> {
ProvidedArgument inputArg = context.getArgument("input");
String input = inputArg != null ? inputArg.getAsString() : "No input provided";
context.reply("You executed the first subcommand with input: " + input);
})
)
// Command: /test second
.then(CommandTree.command("second", "Second subcommand")
.executes(context -> {
context.reply("What do you think about this bot? Please reply within 30 seconds.");
getInstance().getEventWaiter().waitFor(MessageReceivedEvent.class)
.runIf(event -> event.getAuthor().getIdLong() == context.getUser().getIdLong())
.timeoutAfter(Duration.ofSeconds(30))
.runOnTimeout(() -> {
context.reply("You did not reply in time!");
})
.run(event -> {
context.reply("You replied with: " + event.getMessage().getContentDisplay());
})
.build();
})
)
.build()
)
.registerCommand(AnnotationCommandExample.class) // annotation-based command are also supported
.build()
).build()
;
// create the bot instance
new BotExample(settings);
}
private static BotExample INSTANCE;
public static BotExample getInstance() {
return INSTANCE;
}
public BotExample(@NotNull BotSettings settings) {
super(settings);
INSTANCE = this;
this.login(); // manually log in the bot since auto-login is disabled in the settings
}
@Override
protected void configure(@NotNull JDABuilder builder) {
// modify the JDABuilder before building the JDA instance
// (e.g. add additional event listeners, set member cache policy, etc.)
builder.addEventListeners(new ListenerExample()); // supports both ListenerAdapter and annotation-based listeners
}
@Override
protected void onStart() {
// executed once the bot starts
}
@Override
protected void onReady() {
// executed once all shards are ready
}
@Override
protected void onShutdown() {
// executed once the bot is shutting down
}
@SubscribeEvent
public void onReady(@NotNull ReadyEvent event) {
// Bot instance will listen for events by default
}
}