创建 src/main/kotlin/Main.kt:
import love.forte.simbot.application.listeners
import love.forte.simbot.common.function.ConfigurerFunction
import love.forte.simbot.component.qguild.event.QGGroupAtMessageCreateEvent
import love.forte.simbot.component.qguild.firstQQGuildBotManager
import love.forte.simbot.component.qguild.useQQGuild
import love.forte.simbot.core.application.launchSimpleApplication
import love.forte.simbot.event.process
import love.forte.simbot.qguild.event.EventIntents
suspend fun main() {
val appId = System.getenv("QQ_APP_ID") ?: error("请设置 QQ_APP_ID")
val secret = System.getenv("QQ_APP_SECRET") ?: error("请设置 QQ_APP_SECRET")
val token = System.getenv("QQ_TOKEN") ?: ""
val app = launchSimpleApplication { useQQGuild() }
try {
app.listeners {
process<QGGroupAtMessageCreateEvent> { event ->
println("收到群 @ 消息:${event.messageContent.plainText}")
event.reply("你好,我已经收到消息了")
}
}
val bot = app.botManagers.firstQQGuildBotManager().register(appId, secret, token) {
botConfigure = ConfigurerFunction {
intents += EventIntents.GroupAndC2CEvent.intents
}
}
bot.start()
println("Bot 已启动,等待群 @ 消息")
app.join()
} finally {
app.cancel()
}
}
创建 src/main/java/Main.java 。在页面顶部选择 Java API 风格。示例统一以阻塞方式创建应用,使用所选风格启动 Bot、处理事件并等待应用结束:
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.component.qguild.QQGuildComponent;
import love.forte.simbot.component.qguild.QQGuildBotManagerUsageKt;
import love.forte.simbot.component.qguild.bot.QQGuildBotManager;
import love.forte.simbot.component.qguild.event.QGGroupAtMessageCreateEvent;
import love.forte.simbot.qguild.event.EventIntents;
public class Main {
private static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException("请设置 " + name);
return value;
}
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE, config -> {
config.install(QQGuildComponent.Factory);
config.install(QQGuildBotManager.Factory);
});
try {
app.getEventDispatcher().register(EventListeners.block(QGGroupAtMessageCreateEvent.class, (context, event) -> {
System.out.println("收到群 @ 消息:" + event.getMessageContent().getPlainText());
event.replyBlocking("你好,我已经收到消息了");
return EventResult.empty();
}));
var bot = QQGuildBotManagerUsageKt.firstQQGuildBotManager(app.getBotManagers())
.register(required("QQ_APP_ID"), required("QQ_APP_SECRET"),
System.getenv().getOrDefault("QQ_TOKEN", ""), config -> config.botConfig(botConfig ->
botConfig.setIntentsValue(botConfig.getIntentsValue()
| EventIntents.GroupAndC2CEvent.INSTANCE.getIntentsValue())));
bot.startBlocking();
System.out.println("Bot 已启动,等待群 @ 消息");
app.joinBlocking();
} finally {
app.cancel();
}
}
}
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.component.qguild.QQGuildComponent;
import love.forte.simbot.component.qguild.QQGuildBotManagerUsageKt;
import love.forte.simbot.component.qguild.bot.QQGuildBotManager;
import love.forte.simbot.component.qguild.event.QGGroupAtMessageCreateEvent;
import love.forte.simbot.qguild.event.EventIntents;
import java.util.concurrent.CompletableFuture;
public class Main {
private static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException("请设置 " + name);
return value;
}
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE, config -> {
config.install(QQGuildComponent.Factory);
config.install(QQGuildBotManager.Factory);
});
try {
app.getEventDispatcher().register(EventListeners.async(QGGroupAtMessageCreateEvent.class, (context, event) -> {
System.out.println("收到群 @ 消息:" + event.getMessageContent().getPlainText());
return event.replyAsync("你好,我已经收到消息了").thenApply(ignored -> EventResult.empty());
}));
var bot = QQGuildBotManagerUsageKt.firstQQGuildBotManager(app.getBotManagers())
.register(required("QQ_APP_ID"), required("QQ_APP_SECRET"),
System.getenv().getOrDefault("QQ_TOKEN", ""), config -> config.botConfig(botConfig ->
botConfig.setIntentsValue(botConfig.getIntentsValue()
| EventIntents.GroupAndC2CEvent.INSTANCE.getIntentsValue())));
bot.startAsync()
.thenRun(() -> System.out.println("Bot 已启动,等待群 @ 消息"))
.thenCompose(ignored -> app.asFuture())
.join();
} finally {
app.cancel();
}
}
}
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.component.qguild.QQGuildComponent;
import love.forte.simbot.component.qguild.QQGuildBotManagerUsageKt;
import love.forte.simbot.component.qguild.bot.QQGuildBotManager;
import love.forte.simbot.component.qguild.event.QGGroupAtMessageCreateEvent;
import love.forte.simbot.qguild.event.EventIntents;
import reactor.core.publisher.Mono;
public class Main {
private static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException("请设置 " + name);
return value;
}
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE, config -> {
config.install(QQGuildComponent.Factory);
config.install(QQGuildBotManager.Factory);
});
try {
app.getEventDispatcher().register(EventListeners.nonBlock(QGGroupAtMessageCreateEvent.class, (context, event) -> {
System.out.println("收到群 @ 消息:" + event.getMessageContent().getPlainText());
return EventResult.of(Mono.from(event.replyReactive("你好,我已经收到消息了")));
}));
var bot = QQGuildBotManagerUsageKt.firstQQGuildBotManager(app.getBotManagers())
.register(required("QQ_APP_ID"), required("QQ_APP_SECRET"),
System.getenv().getOrDefault("QQ_TOKEN", ""), config -> config.botConfig(botConfig ->
botConfig.setIntentsValue(botConfig.getIntentsValue()
| EventIntents.GroupAndC2CEvent.INSTANCE.getIntentsValue())));
Mono.from(bot.startReactive())
.doOnSuccess(ignored -> System.out.println("Bot 已启动,等待群 @ 消息"))
.then(Mono.from(app.joinReactive()))
.block();
} finally {
app.cancel();
}
}
}