4 Star 5 Fork 1

冰数据 / Sika

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Sika快速入门.md 10.16 KB
一键复制 编辑 原始数据 按行查看 历史
景育 提交于 2023-03-16 00:00 . FunctionsMod实现了help、switch

简介

本文档旨在快速入门如何使用Sika,利用Sika进行简单的QQ机器人功能开发,利用Sika登录账号,以及部署至服务器。

本文档并非快速入门“如何开发Sika”。

创建文件

在下载好Sika之后,需要创建一个启动器,并且需要配置好QQ账号,之后通过运行这个启动器,便可以登录机器人的QQ账号。

创建demo启动器

创建以下内容的文件,可以保存为demo.kt

package com.icedata.amber.launcher

import com.icedata.sika.bridge.SikaGroupEvent
import com.icedata.sika.core.*
import com.icedata.sika.launcher.Launcher
import com.icedata.sika.util.Logger
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.events.MessageEvent

suspend fun main() {
    try {

        val sika = Sika("config/sika.config.json")
        val launcher = Launcher(sika.config)

        sika.init()

        /* 设定预处理器 */
        sika.mountPreProcessor(PreProcessor(1000) { event ->
            if (event is SikaGroupEvent)
                PreProcessor.Result.ADOPT
            else
                PreProcessor.Result.BLOCK
        })

        /* 设定解析处理器 */
        sika.mountParseProcessor(ParseProcessor(1000) { event ->
            listOf(Command(event))
        })

        /* 设定指令处理器 */
        
        
        
        // 注册sika的监听事件
        val listener = GlobalEventChannel.subscribeAlways(MessageEvent::class) {
            sika.newMessage(this)
        }
        launcher.launch()
    } catch (e: SikaConfigNotFoundException) {
        Logger.logError(e)
    }
}

配置QQ账号

在根目录创建config文件夹,在这个文件夹中创建sika.config.json这个文件。在开发时,这个config文件夹应当是和.gradle.ideabuild等文件夹同级的;在服务器上运行时,config文件夹应当位于根目录。

其中内容是

{
  "name": "Sika",
  "qq": 你的QQ号,
  "password": "你的QQ密码",
  "devicePath": "",
  "netLog": false,
  "botLog": false,
  "protocol": "ANDROID"
}

记得将上文中的QQ号、QQ密码改为自己需要登录的账号、密码。name可以随意指定,devicePath如果不指定,Sika会自动给您分配一个随机的设备信息。protocol是您选择的登录协议。

登录测试

完成上述demo.kt和config等的配置之后,可以在IDEA中运行。 点击suspend fun main()左侧的绿色三角形按钮便可以运行。

当终端中输出如下内容时,表示机器人已正常启动:

Thread[Thread-8,5,main]
2021-11-30 14:36:54 I/Mirai: Mirai 正在使用桌面环境. 如遇到验证码将会弹出对话框. 可添加 JVM 属性 `mirai.no-desktop` 以关闭.
2021-11-30 14:36:54 I/Mirai: Mirai is using desktop. Captcha will be thrown by window popup. You can add `mirai.no-desktop` to JVM properties (-Dmirai.no-desktop) to disable it.
2021-11-30 14:36:55 W/Mirai: 未指定设备信息, 已使用随机设备信息. 请查看 BotConfiguration.deviceInfo 以获取更多信息.
2021-11-30 14:36:55 W/Mirai: Device info isn't specified. Please refer to BotConfiguration.deviceInfo for more information
2021-11-30 14:36:57 W/Mirai: 未指定设备信息, 已使用随机设备信息. 请查看 BotConfiguration.deviceInfo 以获取更多信息.
2021-11-30 14:36:57 W/Mirai: Device info isn't specified. Please refer to BotConfiguration.deviceInfo for more information
Sika:B/BotOnline
Sika:M/Group:(90428****)冰数据研究所:(124590****)景育:[Sika:Image]
Sika:M/Group:(90428****)冰数据研究所:(124590****)景育:[Sika:Plain:正常登录应该是这样的]

简单功能开发

在上述的demo.kt中,有注释

        /* 设定指令处理器 */

我们的主要功能在这里完成。

例1 · ping指令

指令要求:当用户输入的信息恰为文本ping时,机器人输出文本Online

sika.mountCommandProcessor(
            CommandProcessor(
                function = { command ->
                    command.sendGroup?.sendMessage("Online") // 表示向发送群(sendGroup)发送消息“Online”
                    CommandProcessor.Result.ADOPT // 表示处理结果是ADOPT
                },
                priority = 1000,
                filter = { command ->
                    command.message.contentToString() == "ping" // 输入消息恰为“ping”时
                })
        )

例2 · 包含关键字

指令要求:当用户输入的文本包括字符串SCU时,机器人输出赢麻了

        sika.mountCommandProcessor(
            CommandProcessor(
                function = { command ->
                    command.sendGroup?.sendMessage("赢麻了")
                    CommandProcessor.Result.ADOPT
                },
                priority = 1000,
                filter = { command ->
                    command.message.contentToString().contains("SCU") // 使用String类的contains()方法
                })
        )

这属于Kotlin的基本语法,同样地,可以构建与或非关系。

例3 · 用户信息

指令要求:当用户输入文本是查询信息时,输出当前群名称、群号、用户群名片、用户QQ号。

sika.mountCommandProcessor(
            CommandProcessor(
                function = { command ->
                    command.sendGroup?.sendMessage(
                        "${command.sendGroup?.name} (${command.sendGroup?.id})\n" +
                        "${command.sender.groupName} (${command.sender.id})"
                    )
                    CommandProcessor.Result.ADOPT
                },
                priority = 1000,
                filter = { command ->
                    command.message.contentToString() == "查询信息"
                })
        )

本例子展示了command对象中包含了sendGroup、sender等的等信息。

例4 · 加法计算器

指令要求:用户输入的内容以sum 开头,之后又若干个整数数字,用空格隔开;输出这些数字的和。 示例输入:sum 5 9 15 示例输出:29

        sika.mountCommandProcessor(
            CommandProcessor(
                function = { command ->
                    // 去掉输入内容的前4个字符,剩下的内容才是数字
                    val inputString = command.message.contentToString().substring(4)
                    val strings = inputString.split(" ") // 使用空格来分割字符串
                    var sum = 0 // 求和变量初始化为0
                    try {
                        for (string in strings) { // 遍历分割后的字符串array
                            sum += string.toInt() // 逐一转化为Int型 加在sum上
                        }
                        command.sendGroup?.sendMessage(sum.toString()) // 输出sum
                    } catch (e : NumberFormatException) { // 如果抛出了数字转化异常
                        command.sendGroup?.sendMessage("输入的内容不是整数。")
                    }
                    CommandProcessor.Result.ADOPT
                },
                priority = 1000,
                filter = { command ->
                    command.message.contentToString().startsWith("sum ") // 输入内容以“sum ”开头
                })
        )

本质还是Kotlin的基本语法,和Sika其实关系不大。

例5 · 表情

指令要求:用户输入的内容为惊讶时;输出一个消息,这个消息为“惊讶”两个字 + QQ自带的惊讶表情。

        sika.mountCommandProcessor(
            CommandProcessor(
                function = { command ->
                    val sikaMessageChainBuilder = SikaMessageChainBuilder() // 创建Builder
                    sikaMessageChainBuilder.add("惊讶") // 添加字符串
                    sikaMessageChainBuilder.add(SikaFaceMessage(0)) // 添加表情
                    command.sendGroup?.sendMessage(sikaMessageChainBuilder.build())
                    CommandProcessor.Result.ADOPT
                },
                priority = 1000,
                filter = { command ->
                    command.message.contentToString() == "惊讶"
                })
        )

本例展示了字符串与表情的混合排版,需要用到SikaMessageChainBuilder去建立Sika消息链。

编译与发布

请确保在IDEA中完成相关测试无误后进行编译。

编译

本程序不可以使用IDEA的Build -> Build Aritifacts菜单来编译打包。需要使用Gradle来编译。 在IDEA中,使用Gradle窗口的Tasks -> build -> jar来进行编译。

使用Gradle来编译jar

正常编译的结果:

正常编译的结果

编译完成后会生成jar文件,位于根目录的build/libs下。 使用一款压缩解压软件打开jar包,在META-INF下找到四个BC开头的奇怪文件,将它们删除:

需要删除的文件

随后保存得到的jar包,即为编译完成的jar包。

发布

服务器

建议使用境内的Windows操作系统的服务器。

账号

  • 建议在服务器上事先使用QQ官方Windows客户端登录QQ账号,进行简单聊天,并登录保持一周。
  • 建议关闭设备锁等,避免不必要的麻烦。
  • 在发布之前,使用服务器上的任一浏览器访问QQ安全中心,并在浏览器中登录账号。

启动

将编译好的jar文件放到想放到的文件夹之中(例如C:\sika\amble\app.jar),并且在jar文件同级创建config文件夹,在文件夹中有json文件(例如C:\Sika\amble\config\sika.config.json)。 (Windows操作系统的路径是不区分大小写的) 您可以使用终端登录,例如打开PowerShell,使用指令cd切换到当前目录,然后java -jar .\app.jar来运行。 您也可以创建一个bat脚本,来帮助启动:

cd C:\sika\amble\
java -jar .\app.jar

那么之后只要双击运行这个bat脚本就可以启动了。

Kotlin
1
https://gitee.com/icedata-foundation-frame/sika.git
git@gitee.com:icedata-foundation-frame/sika.git
icedata-foundation-frame
sika
Sika
newJing

搜索帮助