Skip to content

Commit c70bb26

Browse files
committed
feat: aliasOnly support quoted command
1 parent d76ee38 commit c70bb26

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "koishi-plugin-switch",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"description": "Switch Command Contexts in Koishi",
55
"type": "module",
66
"main": "lib/index.cjs",

src/index.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export const Config: Schema<Config> = Schema.object({
2525
mutuallyExclusiveGroups: Schema.array(Schema.array(Schema.string()).role('table')).default([]).description('互斥组'),
2626
})
2727

28+
const leftQuotes = `"'“‘`
29+
const rightQuotes = `"'”’`
30+
2831
export function apply(ctx: Context, config: Config) {
2932
ctx.i18n.define('zh', zhCN)
3033

@@ -83,11 +86,13 @@ export function apply(ctx: Context, config: Config) {
8386
session.response = noop
8487
return
8588
} else if (command.config.userCall === 'aliasOnly') {
86-
const [name] = session.stripped.content.toLowerCase().slice(session.stripped.prefix.length).split(' ', 1)
87-
if (name === command.name) session.response = noop
89+
const [name] = session.stripped.content.toLowerCase().slice(session.stripped.prefix?.length).split(' ', 1)
90+
const index = leftQuotes.indexOf(name[0]), quote = rightQuotes[index]
91+
if (name === command.name || (name.at(-1) === quote && name.slice(1, -1) === command.name)) session.response = noop
8892
} else if (command.config.userCall === 'aliasOrAppel') {
89-
const [name] = session.stripped.content.toLowerCase().slice(session.stripped.prefix.length).split(' ', 1)
90-
if (name === command.name && !session.stripped.appel) session.response = noop
93+
const [name] = session.stripped.content.toLowerCase().slice(session.stripped.prefix?.length).split(' ', 1)
94+
const index = leftQuotes.indexOf(name[0]), quote = rightQuotes[index]
95+
if ((name === command.name || (name.at(-1) === quote && name.slice(1, -1) === command.name)) && !session.stripped.appel) session.response = noop
9196
}
9297
command = command.parent
9398
}
@@ -101,33 +106,33 @@ export function apply(ctx: Context, config: Config) {
101106
.option('reset', '-r')
102107
.option('resetAll', '-R')
103108
.action(async ({ session, options }, ...names: string[]) => {
104-
const channel = session.channel
109+
const observed = session.channel
105110
if (+!!options.enable + +!!options.disable + +!!options.reset + +!!options.resetAll > 1) return session.text('.conflict')
106111

107112
if (!names.length) {
108113
if (options.resetAll) {
109-
channel.enable = []
110-
channel.disable = []
111-
await channel.$update()
114+
observed.enable = []
115+
observed.disable = []
116+
await observed.$update()
112117
return session.text('.reset')
113118
}
114119

115120
const output = []
116-
if (!options.disable && channel.enable.length) output.push(session.text('.list-enabled', [channel.enable.join(', ')]))
117-
if (!options.enable && channel.disable.length) output.push(session.text('.list-disabled', [channel.disable.join(', ')]))
121+
if (!options.disable && observed.enable.length) output.push(session.text('.list-enabled', [observed.enable.join(', ')]))
122+
if (!options.enable && observed.disable.length) output.push(session.text('.list-disabled', [observed.disable.join(', ')]))
118123
if (options.reset && output.length) output.push(session.text('.reset-ready'))
119124
return output.length ? output.join('\n') : session.text('.none')
120125
}
121126

122127
const candidates: [string, boolean | undefined][] = deduplicate(names).map(x => [x, undefined])
123128
const isEnabled = (name: string, initial: boolean) => {
124-
if (initial) return !channel.disable.includes(name)
125-
else return channel.enable.includes(name)
129+
if (initial) return !observed.disable.includes(name)
130+
else return observed.enable.includes(name)
126131
}
127132

128133
const forbidden = [], enable = [], disable = []
129-
const enableMap = Object.fromEntries(channel.enable.map((name) => [name, true]))
130-
const disableMap = Object.fromEntries(channel.disable.map((name) => [name, true]))
134+
const enableMap = Object.fromEntries(observed.enable.map((name) => [name, true]))
135+
const disableMap = Object.fromEntries(observed.disable.map((name) => [name, true]))
131136

132137
for (let i = 0; i < candidates.length; i++) {
133138
const [name, pref] = candidates[i]
@@ -173,9 +178,9 @@ export function apply(ctx: Context, config: Config) {
173178
if (forbidden.length) return session.text('.forbidden', [forbidden.join(comma)])
174179
if (!enable.length && !disable.length) return session.text('.unchanged')
175180

176-
channel.enable = Object.keys(enableMap).filter((name) => enableMap[name])
177-
channel.disable = Object.keys(disableMap).filter((name) => disableMap[name])
178-
await channel.$update()
181+
observed.enable = Object.keys(enableMap).filter((name) => enableMap[name])
182+
observed.disable = Object.keys(disableMap).filter((name) => disableMap[name])
183+
await observed.$update()
179184

180185
const output: string[] = []
181186
if (enable.length) output.push(session.text('.enabled', [enable.join(comma)]))

yakumo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- tsc --clean
99
- name: yakumo-esbuild
1010
- name: yakumo-tsc
11+
- name: yakumo/run

0 commit comments

Comments
 (0)