Skip to content

Commit

Permalink
feat: 新增函数式语法糖
Browse files Browse the repository at this point in the history
  • Loading branch information
shijinn520 committed Jul 20, 2024
1 parent 4326031 commit a6e86dc
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 34 deletions.
11 changes: 6 additions & 5 deletions docs/.vitepress/config.mts → docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { defineConfig } from 'vitepress'
export default defineConfig({
lang: 'zh-CN',
base: '/Karin',
title: 'Karin',
description: '基于 Kritor 和 noebots 进行开发的nodejs机器人框架',
title: 'karin',
description: '基于 Kritor 进行开发的nodejs机器人框架',
themeConfig: {
siteTitle: 'Karin主页',
outline: {
level: [2, 4],
label: '页面导航'
},
nav: [
{
text: '快速开始',
Expand Down Expand Up @@ -122,9 +126,6 @@ export default defineConfig({
socialLinks: [
{ icon: 'github', link: 'https://github.com/KarinJS/Karin' }
],
outline: {
label: '页面导航'
},
search: {
provider: 'algolia',
options: {
Expand Down
1 change: 0 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ features:
- title: 详尽文档
details: 提供全面的文档支持,帮助开发者轻松掌握框架的使用和插件开发。
---

128 changes: 122 additions & 6 deletions docs/plugins/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,132 @@

- 打开`plugins/karin-plugin-example`目录,在此新建一个`index-demo.js`文件。

## 一个简单的示例
## 函数式语法糖示例

- <Badge type="warning" text="文本" />

```js
import karin from 'node-karin'

// 直接回复字符串
export const hello = karin.command(/^#hello$/, 'hello')

// 回调函数
export const hello = karin.command(/^#你好$/, async (e) => {
await e.reply('hello')
return true
})

```

- <Badge type="warning" text="图片" />

```js
import karin, { segment } from 'node-karin'
import { axios } from 'node-karin/modules.js'

export const image = karin.command(/^#动漫壁纸$/, async (e) => {
// 来自: https://blog.jixiaob.cn/?post=93
const { data } = await axios.get('https://t.alcy.cc/mp')
await e.reply(segment.image(data.url))
return true
})

```

- <Badge type="warning" text="at" />

```js
import karin, { segment } from 'node-karin'

export const at = karin.command(/^#at$/, async (e) => {
await e.reply('\n这是一个at元素', { at: true })
return true
})

// or 上方代码等同于下方代码

export const at = karin.command(/^#at$/, async (e) => {
await e.reply([
segment.at(e.user_id),
'\n这是一个at元素',
])
return true
})

```

- <Badge type="warning" text="引用回复" />

```js
import karin, { segment } from 'node-karin'

export const reply = karin.command(/^#reply$/, async (e) => {
await e.reply('这是一个引用回复', { reply: true })
return true
})

// or 上方代码等同于下方代码

export const reply = karin.command(/^#reply$/, async (e) => {
await e.reply([
segment.reply(e.message_id),
'这是一个引用回复',
])
return true
})

```

- <Badge type="warning" text="语音" />

```js
import karin, { segment } from 'node-karin'

export const record = karin.command(/^#语音$/, async (e) => {
await e.reply(segment.record('base64://...'))
return true
})

```

- <Badge type="warning" text="视频" />

```js
import karin, { segment } from 'node-karin'

export const video = karin.command(/^#视频$/, async (e) => {
await e.reply(segment.video('base64://...'))
return true
})

```

- <Badge type="warning" text="表情" />

```js
import karin, { segment } from 'node-karin'

export const face = karin.command(/^#表情$/, async (e) => {
// 表情id请参考 https://bot.q.qq.com/wiki/develop/api-v2/openapi/emoji/model.html#Emoji
await e.reply(segment.face(1))
return true
})

```

<Badge type="danger" text="待完善..." />

## 类语法糖示例

> 该示例为消息插件示例
> 将下面的代码复制到`index-demo.js`中,保存
>对机器人发送`#你好`,机器人会回复`你好`、图片、语音、视频、@某人
```js
import { plugin, segment } from '#Karin'
import { Plugin, segment } from 'node-karin'

export class hello extends plugin {
export class hello extends Plugin {
constructor () {
super({
name: '插件名称',
Expand Down Expand Up @@ -53,12 +169,12 @@ export class hello extends plugin {

```

## 一个更复杂的示例
## 更复杂的类语法糖示例

```js
import { plugin, segment } from '#Karin'
import { Plugin, segment } from 'node-karin'

export class hello extends plugin {
export class hello extends Plugin {
constructor () {
super({
name: '插件名称',
Expand Down
5 changes: 5 additions & 0 deletions docs/start/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
### 完善此文档

[点击此处跳转到文档贡献页面](https://github.com/KarinJS/Karin/tree/docs)

<!-- - <Badge type="info" text="default" />
- <Badge type="tip" text="^1.9.0" />
- <Badge type="warning" text="beta" />
- <Badge type="danger" text="caution" /> -->
74 changes: 53 additions & 21 deletions docs/start/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
以下,除了`NodeJs`,其他均为可选项,根据实际情况进行安装。
:::

### NodeJs
### NodeJs <Badge type="danger" text="必装 " />

[NodeJs官网][NodeJs]

Expand All @@ -20,26 +20,20 @@
- `Karin`大部分插件开发者的开发环境均在`v20+`版本,推荐使用`v20+`版本。
- 如果无需使用相关插件或功能,可自行选择是否安装`NodeJs`的版本。

### Redis

[redis官网](https://redis.io/)

- 对于`redis`,如果你不使用到相关的插件和功能,可以选择跳过这里。
- `windows`用户推荐使用[redis-windows][redis-windows],非官方版本,请注意查看仓库说明。
- 如果无需使用相关插件或功能,可自行选择是否安装`redis`

### Git
### Git <Badge type="danger" text="必装 " />

[git官网](https://git-scm.com/)

- `git`是一个分布式版本控制软件,~~`Karin`的所有文件默认均使用`git`进行管理、安装、更新。 ~~
- `windows`用户如下载缓慢,可使用[腾讯软件管家][腾讯软件管家]进行加速下载。
- 还是一样,`git`也是一个可选项,所有插件包括`Karin`本身都可以直接下载压缩包进行安装。

- 目前`Karin`正在向纯`npm`包管理迁移,插件还在使用`git`,请继续安装。

## 部署karin

> [!TIP] 注意!
> 如果你无法访问`npm`官网,请先看最下方的更换 [更换npm源](#更换npm源)
### 安装`pnpm`

::: code-group
Expand All @@ -54,8 +48,55 @@ npm --registry=https://registry.npmmirror.com install pnpm -g

:::

### 安装

先新建一个空白文件夹,以下命令在文件夹里面执行,执行完成不出意外已经启动完毕了~

```bash
pnpm i node-karin && npx npx init && node .
```

### 基本指令

::: code-group

```bash [前台启动]
node .
```

```bash [前台启动]
# 这是备用指令
node node_modules/node-karin/lib/index.js
```

```bash [后台启动]
# 使用pm2托管
pnpm pm2
```

```bash [后台停止]
pnpm stop
```

```bash

```

:::

#### 后台启动

### 安装渲染器

- [karin-puppeteer](./render.md)

## 其他

### 更换npm源

<details>
<summary>点我展开</summary>

::: warning 注意
如果你是中国大陆服务器,并且无法访问`npm`官方源,这里请务必更换为镜像源。
:::
Expand Down Expand Up @@ -86,17 +127,8 @@ npm config set registry https://registry.npmjs.org

:::

### 安装

先新建一个空白文件夹,以下命令在文件夹里面执行。
</details>

```bash
pnpm i node-karin && npx init && node .
```

## 安装渲染器

- [karin-puppeteer](./render.md)

[NodeJs]: https://nodejs.org/en
[腾讯软件管家]: https://sw.pcmgr.qq.com/1e05804bd17b358a8c88284df8331fcd/65fcde89/spcmgr/download/Git-2.44.0-64-bit.exe
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "karin-docs",
"author": "Lain",
"version": "0.0.3",
"type": "module",
"devDependencies": {
"vitepress": "^1.2.3"
"vitepress": "^1.3.0"
},
"scripts": {
"dev": "vitepress dev docs",
Expand Down

0 comments on commit a6e86dc

Please sign in to comment.