-
-
Notifications
You must be signed in to change notification settings - Fork 256
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
69 additions
and
48 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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Command, User } from 'koishi-core' | ||
|
||
export interface UserTrap<T = any, K extends User.Field = never> { | ||
fields: Iterable<K> | ||
get(data: Pick<User, K>): T | ||
} | ||
|
||
export namespace UserTrap { | ||
const traps: Record<string, UserTrap<any, any>> = {} | ||
|
||
export function define<T, K extends User.Field = never>(key: string, trap: UserTrap<T, K>) { | ||
traps[key] = trap | ||
} | ||
|
||
export function prepare(cmd: Command, fields: string[]) { | ||
for (const field of fields) { | ||
const trap = traps[field] | ||
cmd.userFields(trap ? trap.fields : [field]) | ||
} | ||
} | ||
|
||
export function get($user: {}, fields: string[]) { | ||
const result: Partial<User> = {} | ||
for (const field of fields) { | ||
const trap = traps[field] | ||
Reflect.set(result, field, trap ? trap.get($user) : $user[field]) | ||
} | ||
return result | ||
} | ||
} |
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