generated from AwesomeHamster/koishi-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
{ | ||
"name": "koishi-template", | ||
"name": "koishi-plugin-bang", | ||
"version": "1.0.0", | ||
"description": "A template repository for creating koishi plugins", | ||
"description": "bang (!) command implementation in Koishi", | ||
"keywords": [ | ||
"bot", | ||
"qqbot", | ||
"chatbot", | ||
"plugin", | ||
"koishi", | ||
"koishijs" | ||
"koishijs", | ||
"linux-command", | ||
"bang" | ||
], | ||
"author": { | ||
"name": "Maiko Tan", | ||
"email": "[email protected]" | ||
}, | ||
"homepage": "https://github.com/AwesomeHamster/koishi-template", | ||
"homepage": "https://github.com/AwesomeHamster/koishi-plugin-bang", | ||
"license": "MIT", | ||
"main": "dist/index.bundle.js", | ||
"directories": { | ||
|
@@ -29,7 +31,7 @@ | |
"types": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/AwesomeHamster/koishi-template.git" | ||
"url": "git+https://github.com/AwesomeHamster/koishi-plugin-bang.git" | ||
}, | ||
"scripts": { | ||
"build": "constructeur build && tsc --emitDeclarationOnly", | ||
|
@@ -41,15 +43,15 @@ | |
"prettier": "prettier '**/*.{js,ts,json,yml,yaml,md}' '!dist/**/*'" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/AwesomeHamster/koishi-template/issues" | ||
"url": "https://github.com/AwesomeHamster/koishi-plugin-bang/issues" | ||
}, | ||
"koishi": { | ||
"description": { | ||
"en": "Koishi Template", | ||
"zh": "Koishi Template" | ||
"en": "bang (!) command implementation", | ||
"zh": "bang (!) 指令实现" | ||
}, | ||
"service": { | ||
"required": [], | ||
"required": ["database"], | ||
"optional": [], | ||
"implements": [] | ||
}, | ||
|
@@ -61,7 +63,7 @@ | |
}, | ||
"prettier": "@hamster-bot/prettier-config", | ||
"peerDependencies": { | ||
"koishi": "^4.8.5" | ||
"koishi": "^4.10.1" | ||
}, | ||
"devDependencies": { | ||
"@hamster-bot/constructeur": "*", | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,98 @@ | ||
import { Context, Schema } from 'koishi' | ||
import { Context, Schema, Session } from 'koishi' | ||
|
||
export interface Config {} | ||
declare module 'koishi' { | ||
export interface Tables { | ||
bang: { | ||
session: string | ||
commands: string[] | ||
} | ||
} | ||
} | ||
|
||
export interface Config { | ||
history: number | ||
bang: string | ||
} | ||
|
||
export const Config: Schema<Config> = Schema.object({ | ||
history: Schema.number().default(500).description('Number of commands to remember'), | ||
bang: Schema.string().default('!').description('Change it if you want to use other trigger'), | ||
}) | ||
|
||
export const name = 'bang' | ||
|
||
export const using = ['database'] | ||
|
||
export async function apply(ctx: Context, config: Config): Promise<void> { | ||
ctx.i18n.define('en-US', require('./locales/en-US.yml')) | ||
ctx.i18n.define('en', require('./locales/en-US.yml')) | ||
|
||
ctx.model.extend('bang', { | ||
session: { type: 'string' }, | ||
commands: { type: 'list' }, | ||
}, { primary: 'session' }) | ||
|
||
ctx.before('command/execute', async (argv) => { | ||
if (!argv.session || !argv.source) return | ||
|
||
const ori = (await ctx.database.get('bang', argv.session.fid))[0] | ||
if (ori) { | ||
ori.commands.push(argv.source) | ||
ori.commands = ori.commands.slice(0, config.history) | ||
ctx.database.set('bang', argv.session.fid, { commands: ori.commands }) | ||
} else { | ||
ctx.database.create('bang', { session: argv.session?.fid, commands: [argv.source] }) | ||
} | ||
}, true) | ||
|
||
async function executeCommand(session: Session, command: string, onlyPrint = false) { | ||
if (onlyPrint) { | ||
return command | ||
} | ||
|
||
return await session.execute(command) | ||
} | ||
|
||
ctx.middleware(async (session, next) => { | ||
if (!session.content.trim().startsWith(config.bang)) { | ||
return await next() | ||
} | ||
|
||
export const Config: Schema<Config> = Schema.object({}) | ||
let rest = session.content.trim().slice(config.bang.length) | ||
const history = (await ctx.database.get('bang', session.fid))[0] | ||
if (!history) session.text('bang.errors.no-history') | ||
|
||
export const name = 'koishi-template' | ||
// parse `:p` suffix, which should print the command instead of executing it | ||
let onlyPrint = false | ||
if (rest.endsWith(':p')) { | ||
rest = rest.slice(0, -2) | ||
onlyPrint = true | ||
} | ||
// TODO: support `:s` suffix, which can be used to substitute the command | ||
// TODO: support `:n` suffix (while n is a number), which can be used to capture the nth argument | ||
// Also `:$` for the last argument. | ||
|
||
export async function apply(ctx: Context): Promise<void> { | ||
// ... | ||
if (/^!$/.test(rest)) { | ||
// parse bangbang like !! | ||
const last = history.commands[history.commands.length - 1] | ||
return await executeCommand(session, last, onlyPrint) | ||
} else if (/^(-?\d+)$/.test(rest)) { | ||
// parse numbers like !1 or !-1 | ||
const index = parseInt(rest) | ||
if (index === 0) return session.text('bang.errors.invalid-index') | ||
const command = history.commands[index > 0 ? index - 1 : history.commands.length + index] | ||
if (!command) return session.text('bang.errors.invalid-index') | ||
return await executeCommand(session, command, onlyPrint) | ||
} else if (/^(\w+)$/.test(rest)) { | ||
// parse strings like !ls | ||
const command = history.commands.find(c => c.startsWith(rest)) | ||
if (!command) return session.text('bang.errors.command-not-found') | ||
return await executeCommand(session, command, onlyPrint) | ||
} else if (/^\?\w+$/.test(rest)) { | ||
// parse non-preceding strings like !?test.txt => !ls test.txt | ||
const command = history.commands.find(c => c.includes(rest.slice(1))) | ||
if (!command) return session.text('bang.errors.command-not-found') | ||
return await executeCommand(session, command, onlyPrint) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
bang: | ||
errors: | ||
no-history: There is no command history. | ||
|
||
invalid-index: Invalid index for fetching command history. | ||
|
||
command-not-found: Command not found. |