Skip to content

Latest commit

 

History

History
191 lines (149 loc) · 4.95 KB

README.md

File metadata and controls

191 lines (149 loc) · 4.95 KB

qq-official-bot-sdk

QQ官方机器人 SDK For Java

Maven metadata URL GitHub Repo stars

使用

1.添加依赖

  • Maven
<dependency>
    <groupId>me.zhenxin</groupId>
    <artifactId>qqbot-sdk</artifactId>
    <version>${version}</version>
</dependency>
  • Gradle Kotlin DSL
implementation("me.zhenxin:qqbot-sdk:${version}")
  • Gradle Groovy DSL
implementation 'me.zhenxin:qqbot-sdk:${version}'
  • Sbt
libraryDependencies += "me.zhenxin" % "qqbot-sdk" % version

2.使用

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

<dependency>
    <groupId>me.zhenxin</groupId>
    <artifactId>qqbot-api</artifactId>
    <version>${version}</version>
</dependency>
@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<Guild> 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 协议开放源代码

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 <http://www.gnu.org/licenses/>.

赞助

你可以请我喝瓶可乐~

img-pay