# qqbot-sdk
**Repository Path**: armoe-project/qqbot-sdk
## Basic Information
- **Project Name**: qqbot-sdk
- **Description**: QQ官方机器人 SDK For Java
- **Primary Language**: Java
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: https://sdk.armoe.cn/qq/
- **GVP Project**: No
## Statistics
- **Stars**: 9
- **Forks**: 0
- **Created**: 2021-12-22
- **Last Updated**: 2024-07-18
## Categories & Tags
**Categories**: Uncategorized
**Tags**: qq, 机器人, Java, Kotlin
## README
# qq-official-bot-sdk
QQ官方机器人 SDK For Java
[![Maven metadata URL][img-maven]][maven]
![GitHub Repo stars][img-stars]
* [加入QQ频道][qqguild]
* [使用文档][docs]
## 使用
1.添加依赖
* Maven
```xml
me.zhenxin
qqbot-sdk
${version}
```
* Gradle Kotlin DSL
```kotlin
implementation("me.zhenxin:qqbot-sdk:${version}")
```
* Gradle Groovy DSL
```groovy
implementation 'me.zhenxin:qqbot-sdk:${version}'
```
* Sbt
```sbt
libraryDependencies += "me.zhenxin" % "qqbot-sdk" % version
```
2.使用
```java
class Example {
public static void main(String[] args) {
AccessInfo accessInfo = new AccessInfo();
accessInfo.setBotAppId(0); // 管理端的BotAppId
accessInfo.setBotToken(""); // 管理端的BotToken
// 使用沙盒模式
accessInfo.useSandBoxMode();
// 创建实例
BotCore bot = new BotCore(accessInfo);
// 获取API管理器
ApiManager api = bot.getApiManager();
// 注册AT消息相关事件
bot.registerAtMessageEvent();
// 设置事件处理器
IEventHandler handler = new IEventHandler(api);
// handler.setRemoveAt(false); // 取消删除消息中的艾特
bot.setEventHandler(handler);
// 启动
bot.start();
}
}
// 自定义事件处理器 继承EventHandler
@Slf4j
@AllArgsConstructor
class IEventHandler extends EventHandler {
private final ApiManager api;
// 处理错误
@Override
public void onError(Throwable t) {
}
@Override
public void onAtMessage(AtMessageEvent event) {
Message message = event.getMessage();
String guildId = message.getGuildId();
String channelId = message.getChannelId();
String content = message.getContent();
String messageId = message.getId();
User author = message.getAuthor();
try {
String[] args = content.split(" ");
String command = args[0];
switch (command) {
case "ping":
api.getMessageApi()
.sendMessage(channelId, "pong", messageId);
break;
}
} catch (ApiException e) {
log.error("消息处理发生异常: {} {}({})", e.getCode(), e.getMessage(), e.getError());
api.getMessageApi().sendMessage(channelId, "消息处理失败: " + e.getMessage(), messageId);
}
}
}
```
## 单独使用API
```xml
me.zhenxin
qqbot-api
${version}
```
```java
@Slf4j
class Example {
public static void main(String[] args) {
AccessInfo accessInfo = new AccessInfo();
accessInfo.setBotAppId(0); // 管理端的BotAppId
accessInfo.setBotToken(""); // 管理端的BotToken
// 使用沙盒模式
accessInfo.useSandBoxMode();
// 创建实例
ApiManager api = new ApiManager(accessInfo);
// 调用
List guilds = api.getUserApi().getMeGuilds();
log.info("{}", guilds);
}
}
```
## 自定义日志级别
添加环境变量 `LogLevel` 设置日志级别
| 等级 | 描述 |
|-------|------------------|
| INFO | 信息 对应log.info() |
| DEBUG | 调试 对应log.debug() |
| WARN | 警告 对应log.warn() |
| ERROR | 错误 对应log.error() |
## 模块说明
| 模块 | 说明 |
|-----------------|-------------|
| qqbot-common | 公共文件 实体类等 |
| qqbot-api | API实现 可单独使用 |
| qqbot-websocket | Ws实现 |
| qqbot-sdk | SDK集成 |
## 开源协议
本项目使用 [GPL-3.0](LICENSE) 协议开放源代码
```text
qq-official-bot-sdk - QQ Official Bot SDK For Java
Copyright (C) 2022 xiaoye-bot Project Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
```
## 赞助
你可以请我喝瓶可乐~
![img-pay]
[maven]: https://search.maven.org/artifact/me.zhenxin/qqbot-sdk
[docs]: https://sdk.armoe.cn/qq/
[qqguild]: https://qun.qq.com/qqweb/qunpro/share?_wv=3&_wwv=128&inviteCode=GECpm&from=246610&biz=ka
[img-maven]: https://img.shields.io/maven-central/v/me.zhenxin/qqbot-sdk
[img-stars]: https://img.shields.io/github/stars/xiaoye-bot/qq-official-bot-sdk
[img-pay]: https://s2.loli.net/2022/01/12/wqhXNKAlnMZ5oci.png