Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
fix: check nullable config, fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 10, 2023
1 parent de94914 commit 6debc32
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion client/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const text = ref('')
const sid = computed(() => {
if (local.value.name !== '@koishijs/plugin-adapter-onebot') return
return `${config.value.platform || 'onebot'}:${config.value.selfId}`
return `${config.value?.platform || 'onebot'}:${config.value?.selfId}`
})
const data = computed(() => {
Expand Down
27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-gocqhttp",
"description": "go-cqhttp launcher for Koishi",
"version": "3.1.4",
"version": "3.2.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down Expand Up @@ -38,27 +38,32 @@
"plugin"
],
"koishi": {
"description": {
"zh": "[go-cqhttp](https://github.com/Mrs4s/go-cqhttp) 启动器",
"en": "[go-cqhttp](https://github.com/Mrs4s/go-cqhttp) launcher"
},
"service": {
"optional": [
"console"
]
}
},
"peerDependencies": {
"@koishijs/plugin-adapter-onebot": "^5.2.7",
"koishi": "^4.9.6"
"@koishijs/plugin-adapter-onebot": "^5.5.4",
"koishi": "^4.11.1"
},
"devDependencies": {
"@koishijs/client": "^4.6.8",
"@koishijs/plugin-adapter-onebot": "^5.2.7",
"@koishijs/plugin-console": "^4.6.8",
"@koishijs/plugin-market": "^1.4.1",
"@types/node": "^18.6.4",
"koishi": "^4.9.6",
"typescript": "4.8.2"
"@koishijs/client": "^5.1.2",
"@koishijs/plugin-adapter-onebot": "^5.5.4",
"@koishijs/plugin-console": "^5.1.2",
"@koishijs/plugin-market": "^1.8.6",
"@types/node": "^18.11.17",
"koishi": "^4.11.1",
"typescript": "^4.9.4"
},
"dependencies": {
"go-cqhttp": "^1.1.5",
"@koishijs/utils": "6.4.0",
"go-cqhttp": "^1.1.6",
"strip-ansi": "^6.0.1"
}
}
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ class Launcher extends DataService<Dict<Data>> {
super(ctx, 'gocqhttp', { authority: 4 })
logger.level = config.logLevel || 3

ctx.on('bot-connect', async (bot: OneBotBot<Context>) => {
ctx.on('bot-connect', async (bot: OneBotBot) => {
if (!bot.config.gocqhttp?.enabled) return
return this.connect(bot)
})

ctx.on('bot-disconnect', async (bot: OneBotBot<Context>) => {
ctx.on('bot-disconnect', async (bot: OneBotBot) => {
if (!bot.config.gocqhttp?.enabled) return
return this.disconnect(bot)
})
Expand All @@ -95,7 +95,7 @@ class Launcher extends DataService<Dict<Data>> {
})

ctx.console.addListener('gocqhttp/write', (sid, text) => {
const bot = ctx.bots[sid] as OneBotBot<Context>
const bot = ctx.bots[sid] as OneBotBot
return new Promise<void>((resolve, reject) => {
bot.process.stdin.write(text + '\n', (error) => {
error ? reject(error) : resolve()
Expand All @@ -112,7 +112,7 @@ class Launcher extends DataService<Dict<Data>> {
return readFile(filename, 'utf8')
}

private async getConfig(bot: OneBotBot<Context>) {
private async getConfig(bot: OneBotBot) {
const template = await (this.templateTask ||= this.getTemplate())
const config = {
message: JSON.stringify(this.config.message),
Expand All @@ -133,12 +133,12 @@ class Launcher extends DataService<Dict<Data>> {
return this.payload
}

private setData(bot: OneBotBot<Context>, data: Data) {
private setData(bot: OneBotBot, data: Data) {
this.payload[bot.sid] = data
this.refresh()
}

async connect(bot: OneBotBot<Context>) {
async connect(bot: OneBotBot) {
// create working folder
const cwd = resolve(bot.ctx.baseDir, this.config.root, bot.selfId)
await mkdir(cwd, { recursive: true })
Expand All @@ -152,7 +152,7 @@ class Launcher extends DataService<Dict<Data>> {
// spawn go-cqhttp process
bot.process = gocqhttp({ cwd, faststart: true })

bot.process.stdout.on('data', async (data: string) => {
bot.process.stdout.on('data', async (data) => {
data = strip(data.toString()).trim()
if (!data) return
for (const line of data.trim().split('\n')) {
Expand Down Expand Up @@ -217,7 +217,7 @@ class Launcher extends DataService<Dict<Data>> {
})
}

async disconnect(bot: OneBotBot<Context>) {
async disconnect(bot: OneBotBot) {
bot.process?.kill()
bot.process = null
this.setData(bot, { status: 'offline' })
Expand Down

0 comments on commit 6debc32

Please sign in to comment.