Skip to content

Commit

Permalink
feat: add target for messageFromId, close #51
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Oct 2, 2022
1 parent 1784f8d commit 82af179
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,35 @@ el-bot 展示了整个 mirai-ts 的使用流程,并内置了一些如自动应
但这并不是束缚,在插件系统中你仍然可以调用机器人所有的上下文,并通过编写插件的形式快速实现你想要的功能。

> 更多请参见文档 [el-bot | El Bot Docs](https://docs.bot.elpsy.cn/)
## 开发

```bash
# clone 本项目
git clone https://github.com/YunYouJun/mirai-ts
cd mirai-ts

# 安装 mirai-console-loader,放置于 `mirai-ts/mcl` 文件夹下
# https://github.com/iTXTech/mirai-console-loader
mkdir mcl
cd mcl
# 修改链接下载 mcl 对应版本
wget https://github.com/iTXTech/mirai-console-loader/releases/download/v2.1.1/mcl-2.1.1.zip
unzip mcl-2.1.1.zip
chmod +x mcl
./mcl

# 使用 mcl 安装 mirai-api-http
# https://github.com/project-mirai/mirai-api-http#%E5%AE%89%E8%A3%85mirai-api-http
./mcl --update-package net.mamoe:mirai-api-http --channel stable-v2 --type plugin
# mcl 自动更新
./mcl -u

cd ..
pnpm mcl

# 参考 https://github.com/project-mirai/mirai-login-solver-selenium 获取 ticket

# 启动 demo
pnpm demo
```
5 changes: 3 additions & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ async function app() {

// 调用 mirai-ts 封装的 mirai-api-http 发送指令
console.log('send command help')
const data = await mirai.api.command.send('/help', [])
console.log(`帮助信息:${data}`)
const data = await mirai.api.command.execute(['/help'])
if (data.code === 0)
console.log('执行成功')

// 处理各种事件类型
// 事件订阅说明(名称均与 mirai-api-http 中事件名一致)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"build": "tsup src/index.ts",
"demo": "node demo/index.js",
"demo:ts": "tsx watch demo/index.ts",
"dev": "npm run watch",
"prepare": "husky install",
"prepublishOnly": "npm run build",
"docs:build": "typedoc",
Expand Down
33 changes: 22 additions & 11 deletions src/mirai-api-http/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* 指令系统
* @packageDocumentation
* https://github.com/project-mirai/mirai-api-http/blob/master/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/consts/paths.kt
*/

import type { AxiosResponse } from 'axios'
import type { Api, MessageType } from '../types'
import type { MiraiApiHttp } from './index'

export interface CommandInfo {
Expand All @@ -27,7 +29,7 @@ export class Command {
*/
async listen(): Promise<CommandInfo> {
const { data } = (await this.api.axios.post('/cmd', {
verifyKey: this.api.setting.verifyKey,
sessionKey: this.api.sessionKey,
})) as AxiosResponse<CommandInfo>
return data
}
Expand All @@ -37,16 +39,21 @@ export class Command {
* @param name 指令名
* @param alias 指令别名
* @param description 指令描述
* @param usage 指令描述,会在指令执行错误时显示
* @param usage 使用说明,会在指令执行错误时显示
*/
async register(
name: string,
alias: string[],
description: string,
usage?: string,
) {
const { data } = await this.api.axios.post('/cmd/register', {
verifyKey: this.api.setting.verifyKey,
const { data } = await this.api.axios.post<Api.Params.RequestParams<{
name: string
alias: string[]
description: string
usage?: string
}>>('/cmd/register', {
sessionKey: this.api.sessionKey,
name,
alias,
description,
Expand All @@ -57,14 +64,18 @@ export class Command {

/**
* 发送指令
* @param name 指令名
* @param args 指令参数
* @param command 命令与参数
*/
async execute(name: string, args: string[]) {
const { data } = await this.api.axios.post('/cmd/execute', {
verifyKey: this.api.setting.verifyKey,
name,
args,
async execute(command: string[]) {
const { data } = await this.api.axios.post<
Api.Params.RequestParams<{ command: MessageType.MessageChain }>,
AxiosResponse<Api.Response.BaseResponse>
>('/cmd/execute', {
sessionKey: this.api.sessionKey,
command: [{
type: 'Plain',
text: command.join(' '),
}],
})
return data
}
Expand Down
6 changes: 4 additions & 2 deletions src/mirai-api-http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,17 @@ export class MiraiApiHttp {
/**
* 通过 messageId 获取一条被缓存的消息
* @param id 获取消息的messageId
* @param target 好友 ID 或 群 ID
*/
async messageFromId(id: number) {
async messageFromId(id: number, target: number) {
const { data } = await this.axios.get<
Api.Params.RequestParams<{ id: number }>,
Api.Params.RequestParams<{ id: number; target: number }>,
AxiosResponse<Api.Response.MessageFromId>
>('/messageFromId', {
params: {
sessionKey: this.sessionKey,
id,
target,
},
})
if (data.code === 0)
Expand Down

0 comments on commit 82af179

Please sign in to comment.