Skip to content

Commit

Permalink
feat: hmr for bot dev
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Sep 21, 2024
1 parent 3ccb983 commit ac07f0d
Show file tree
Hide file tree
Showing 37 changed files with 584 additions and 501 deletions.
20 changes: 20 additions & 0 deletions examples/simple/bot/plugins/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineBotPlugin, pluginLogger } from 'el-bot'
import { Structs } from 'node-napcat-ts'

export default defineBotPlugin({
pkg: {
name: 'ping',
},
setup: (ctx) => {
pluginLogger.info('ping 自己的插件日志')

const { napcat } = ctx
napcat.on('message', (msg) => {
if (msg.raw_message === '捅死你') {
ctx.reply(msg, [
Structs.text('我爱你'),
])
}
})
},
})
7 changes: 7 additions & 0 deletions examples/simple/el-bot.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ export default defineConfig({
host: '127.0.0.1',
port: 3001,
accessToken: 'yunyoujun',

// ↓ 自动重连(可选)
reconnection: {
enable: true,
attempts: 10,
delay: 5000,
},
},
})
1 change: 1 addition & 0 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "A simple demo for el-bot.",
"scripts": {
"dev": "el-bot",
"dev:w": "vite-node -w ../../packages/el-bot/bin/el-bot.ts",
"build": "tsup",
"start": "tsx src/index.ts",
"start:prod": "nodemon dist/index.js"
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
"lint-staged": "^15.2.10",
"nodemon": "^3.1.7",
"npm-run-all": "^4.1.5",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"tsx": "^4.19.1",
"typescript": "^5.6.2"
},
Expand Down
11 changes: 6 additions & 5 deletions packages/cli/src/install/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type commander from 'commander'
import { Logger } from '@yunyoujun/logger'
import { logger } from 'packages/el-bot'

export default function (cli: commander.Command) {
/**
* @deprecated
*/
export default function (cli: any) {
cli
.command('install [project]')
.description('安装依赖')
Expand All @@ -12,10 +14,9 @@ export default function (cli: commander.Command) {
})
}

const logger = new Logger({ prefix: '[cli(install)]' })

/**
* 安装 mirai
* @deprecated
*/
function installMirai() {
logger.warning(
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/install/repo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'node:fs'
import { Logger } from '@yunyoujun/logger'
import axios from 'axios'
import download from 'download'
import { createLogger } from 'packages/el-bot'
import ProgressBar from 'progress'

const logger = new Logger({ prefix: '[cli(repo)]' })
const logger = createLogger().child({ label: '📦' })

/**
* Repo 类
Expand Down
37 changes: 11 additions & 26 deletions packages/cli/src/start/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import type commander from 'commander'
import { spawn } from 'node:child_process'
import fs from 'node:fs'
import { resolve } from 'node:path'
import process from 'node:process'
import { Logger } from '@yunyoujun/logger'
import { utils } from 'el-bot'
import { createLogger, utils } from 'el-bot'
import shell from 'shelljs'

// 实例目录下的 package.json
// eslint-disable-next-line ts/no-require-imports
const pkg = require(getAbsolutePath('./package.json'))

const logger = new Logger({ prefix: '[cli(start)]' })
const logger = createLogger().child({ label: '🚀' })

/**
* 获取当前目录下的绝对路径
Expand All @@ -21,40 +17,29 @@ function getAbsolutePath(file: string) {
return resolve(process.cwd(), file)
}

export default async function (cli: commander.Command) {
/**
* @deprecated
* TODO: refactor for napcat
*/
export default async function (cli: any) {
/**
* 启动机器人
*/
function startBot() {
const execFile = pkg.main || 'index.js' || 'index.ts' || 'index.mjs'
const file = getAbsolutePath(execFile)

if (fs.existsSync(file)) {
if (file.includes('.ts'))
spawn('ts-node', [file], { stdio: 'inherit' })
else
spawn('node', [file], { stdio: 'inherit' })

return true
}
else {
logger.error(
'不存在可执行文件,请检查 package.json 中 main 入口文件是否存在,或参考文档新建 bot/index.js 机器人实例。',
)
return false
}
// el-bot
}

/**
* 启动 MCL
* @deprecated mirai
*/
function startMcl(folder?: string) {
// 先进入目录
try {
shell.cd(folder || (pkg.mcl ? pkg.mcl.folder : 'mcl'))
}
catch (err) {
utils.handleError(err, logger)
utils.handleError(err)
logger.error('mcl 目录不存在')
}

Expand All @@ -67,7 +52,7 @@ export default async function (cli: commander.Command) {
.command('start [project]')
.description('启动 el-bot')
.option('-f --folder', 'mirai/mcl 所在目录')
.action((project, options) => {
.action((project: string, options: { folder: string | undefined }) => {
if (!project || project === 'bot')
startBot()
else if (project === 'mcl')
Expand Down
4 changes: 4 additions & 0 deletions packages/el-bot/TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO

- remove useless dependencies
- commander
2 changes: 1 addition & 1 deletion packages/el-bot/bin/el-bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env vite-node --script
#!/usr/bin/env vite-node --watch --script

import { run } from '../node'

Expand Down
1 change: 1 addition & 0 deletions packages/el-bot/core/bot/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# CLI for Bot
2 changes: 1 addition & 1 deletion packages/el-bot/core/bot/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type commander from 'commander'
*/
import type { Bot } from 'el-bot'
import type { MessageType } from 'mirai-ts'
import type { PluginType } from '../plugins'
import type { PluginType } from '../plugins/class'
import { Command } from 'commander'
import * as shell from 'shelljs'
import { aboutInfo } from './utils'
Expand Down
Loading

0 comments on commit ac07f0d

Please sign in to comment.