Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
1.修复未登录的账号直接移除报错问题
feat:
2. 移除账号添加force选项,在force为true是,将删除账号data目录
  • Loading branch information
lc-cn committed Nov 30, 2022
1 parent f41b041 commit 5e33c6d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ onebots
```
# 使用接口管理oneBot

| url | method | params | desc |
|:--------| :--- |:-----------------|:----------------------|
| /list | GET | | 获取当前运行的机器人列表 |
| /detail | GET | uin | 获取指定机器人配置 |
| /qrcode | GET | uin | 获取指定机器人登录二维码 |
| /add | POST | {uin,...config} | 添加机器人 config 为机器人配置 |
| /edit | POST | {uin,...config} | 修改机器人配置 config 为机器人配置 |
| /remove | get | uin | 移除机器人 |
| url | method | params | desc |
|:--------| :--- |:----------------|:-------------------------------|
| /list | GET | | 获取当前运行的机器人列表 |
| /detail | GET | uin | 获取指定机器人配置 |
| /qrcode | GET | uin | 获取指定机器人登录二维码 |
| /add | POST | {uin,...config} | 添加机器人 config 为机器人配置 |
| /edit | POST | {uin,...config} | 修改机器人配置 config 为机器人配置 |
| /remove | get | uin,force | 移除机器人,force为true时,将删除机器人data目录 |

# 鸣谢
1. [takayama-lily/oicq](https://github.com/takayama-lily/oicq) 底层服务支持
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "onebots",
"version": "0.0.9",
"version": "0.0.10",
"description": "基于oicq的多例oneBot实现",
"main": "lib/index.js",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions src/onebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export class OneBot<V extends OneBot.Version> extends EventEmitter{
instance.start(this.instances.length>1?'/'+instance.version:undefined)
}
}
async stop(){
async stop(force?:boolean){
for(const instance of this.instances){
await instance.stop()
await instance.stop(force)
}
this.client.off('system',this.dispatch.bind(this))
this.client.off('notice',this.dispatch.bind(this))
Expand Down
8 changes: 4 additions & 4 deletions src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ export class App extends Koa{
this.removeAccount(uin)
this.addAccount(uin,newConfig)
}
public removeAccount(uin:number|`${number}`){
public removeAccount(uin:number|`${number}`,force?:boolean){
if(typeof uin!=="number")uin=Number(uin)
if(Number.isNaN(uin)) throw new Error('无效的账号')
const currentIdx=this.oneBots.findIndex(oneBot=>oneBot.uin===uin)

if(currentIdx<0) throw new Error('账户不存在')
const oneBot=this.oneBots[currentIdx]
oneBot.stop()
oneBot.stop(force)
delete this.config[uin]
this.oneBots.splice(currentIdx,1)
writeFileSync(App.configPath,yaml.dump(this.config))
Expand Down Expand Up @@ -157,9 +157,9 @@ export class App extends Koa{
}
})
this.router.get('/remove',(ctx,next)=>{
const {uin}=ctx.request.query
const {uin,force}=ctx.request.query
try{
this.removeAccount(Number(uin))
this.removeAccount(Number(uin),Boolean(force))
ctx.body=`移除成功`
}catch (e){
console.log(e)
Expand Down
8 changes: 5 additions & 3 deletions src/service/V11/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {BOOLS, NotFoundError} from "@/onebot";
import http from "http";
import https from "https";
import {EventEmitter} from "events";
import {unlinkSync} from "fs";
import {rmSync, unlinkSync} from "fs";

export class V11 extends EventEmitter implements OneBot.Base{
public action:Action
Expand Down Expand Up @@ -166,11 +166,13 @@ export class V11 extends EventEmitter implements OneBot.Base{
}
})
}
async stop() {
async stop(force?:boolean) {
if(this.client.status===OnlineStatus.Online){
await this.client.logout()
}
unlinkSync(this.client.dir)
if(force){
rmSync(this.client.dir,{force:true,recursive:true})
}
}
dispatch(data:any) {
if(!data.post_type)data.post_type='system'
Expand Down
8 changes: 5 additions & 3 deletions src/service/V12/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {toBool, toHump, toLine, uuid} from "@/utils";
import {Db} from "@/db";
import {App} from "@/server/app";
import Payload = V12.Payload;
import {unlinkSync} from "fs";
import {rmSync} from "fs";

export class V12 extends EventEmitter implements OneBot.Base{
public version='V12'
Expand Down Expand Up @@ -194,11 +194,13 @@ export class V12 extends EventEmitter implements OneBot.Base{
}
})
}
async stop() {
async stop(force?:boolean) {
if(this.client.status===OnlineStatus.Online){
await this.client.logout()
}
unlinkSync(this.client.dir)
if(force){
rmSync(this.client.dir,{force:true,recursive:true})
}
}
dispatch<T extends Record<string, any>>(data:T= {} as any) {
if(!data)data={} as any
Expand Down

0 comments on commit 5e33c6d

Please sign in to comment.