diff --git a/packages/koishi-core/src/app.ts b/packages/koishi-core/src/app.ts index d88ca76c92..b4cea13b74 100644 --- a/packages/koishi-core/src/app.ts +++ b/packages/koishi-core/src/app.ts @@ -327,7 +327,7 @@ export class App extends Context { } // attach user data - const userFields = new Set(['name', 'flag']) + const userFields = new Set(['flag']) this.receiver.emit('before-user', userFields, parsedArgv || { meta }) const user = await this.database.observeUser(meta.userId, Array.from(userFields)) Object.defineProperty(meta, '$user', { value: user, writable: true }) diff --git a/packages/koishi-core/src/database.ts b/packages/koishi-core/src/database.ts index 7fc04ac9aa..ca28c5f48a 100644 --- a/packages/koishi-core/src/database.ts +++ b/packages/koishi-core/src/database.ts @@ -14,7 +14,6 @@ export const userFlags: (keyof typeof UserFlag)[] = ['ignore'] export interface UserData { id: number - name: string flag: number authority: number usage: Record diff --git a/packages/koishi-core/src/utils.ts b/packages/koishi-core/src/utils.ts index fb1560dc5a..7b21c1039a 100644 --- a/packages/koishi-core/src/utils.ts +++ b/packages/koishi-core/src/utils.ts @@ -7,12 +7,6 @@ import { messages } from './messages' import { format } from 'util' import leven from 'leven' -export function getSenderName (meta: MessageMeta) { - const userId = '' + meta.userId - return meta.$user && meta.$user.name !== userId ? meta.$user.name - : meta.sender ? meta.sender.card || meta.sender.nickname : userId -} - export function getTargetId (target: string | number) { if (typeof target !== 'string' && typeof target !== 'number') return let qq = +target @@ -51,7 +45,7 @@ export function showSuggestions (options: SuggestOptions): Promise { const [suggestion] = suggestions const command = typeof options.command === 'function' ? options.command(suggestion) : options.command const identifier = meta.userId + meta.$ctxType + meta.$ctxId - const userFields = new Set(['name']) + const userFields = new Set() const groupFields = new Set() Command.attachUserFields(userFields, { command, meta }) Command.attachGroupFields(groupFields, { command, meta }) diff --git a/packages/koishi-core/tests/utils.spec.ts b/packages/koishi-core/tests/utils.spec.ts index bae7329cd7..21a4ae80d1 100644 --- a/packages/koishi-core/tests/utils.spec.ts +++ b/packages/koishi-core/tests/utils.spec.ts @@ -1,39 +1,4 @@ -import { getSenderName, createUser, getTargetId } from 'koishi-core' -import { createSender } from 'koishi-test-utils' -import { observe } from 'koishi-utils' - -describe('getSenderName', () => { - test('userData with userId', () => { - expect(getSenderName({ - userId: 123, - $user: observe(createUser(123, 1)), - })).toBe('123') - }) - - test('userData with name', () => { - expect(getSenderName({ - userId: 123, - $user: observe({ ...createUser(123, 1), name: '456' }), - sender: createSender(123, 'bar', 'foo'), - })).toBe('456') - }) - - test('userData with card', () => { - expect(getSenderName({ - userId: 123, - $user: observe(createUser(123, 1)), - sender: createSender(123, 'bar', 'foo'), - })).toBe('foo') - }) - - test('userData with nickname', () => { - expect(getSenderName({ - userId: 123, - $user: observe(createUser(123, 1)), - sender: createSender(123, 'bar'), - })).toBe('bar') - }) -}) +import { getTargetId } from 'koishi-core' describe('getTargetId', () => { test('with id', () => { diff --git a/packages/plugin-common/src/authorize.ts b/packages/plugin-common/src/authorize.ts index 2a0356f81f..ded31f8ce3 100644 --- a/packages/plugin-common/src/authorize.ts +++ b/packages/plugin-common/src/authorize.ts @@ -31,7 +31,6 @@ interface AuthorizeInfo { export default function apply (ctx: Context, config: AuthorizeConfig) { const { app, database } = ctx const { authorizeUser = {}, authorizeGroup = {} } = config - const logger = ctx.logger('authorize') const authorityMap: Record = [] /** diff --git a/packages/plugin-common/src/callme.ts b/packages/plugin-common/src/callme.ts deleted file mode 100644 index df69deee8c..0000000000 --- a/packages/plugin-common/src/callme.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Context, CommandConfig, Meta } from 'koishi-core' - -export interface CallmeOptions extends CommandConfig { - validateName?: (name: string, meta: Meta) => string | void -} - -const defaultOptions: CallmeOptions = { - maxUsage: 3, - validateName (name, meta) { - if (name === meta.$user.name) return '称呼未发生变化。' - }, -} - -export default function apply (ctx: Context, options: CallmeOptions = {}) { - ctx.command('callme ', '修改自己的称呼', { ...defaultOptions, ...options }) - .userFields(['name']) - .shortcut('叫我', { prefix: true, fuzzy: true, oneArg: true }) - .action(async ({ meta }, name: string) => { - if (name === undefined || /^\s*$/.test(name)) { - return meta.$send(`好的,${meta.$user.name},请多指教!`) - } - - name = String(name).trim() - const message = options.validateName(name, meta) - if (message) return meta.$send(message) - - try { - await ctx.database.setUser(meta.userId, { name }) - return meta.$send(`好的,${name},请多指教!`) - } catch (error) { - if (error.code === 'ER_DUP_ENTRY') { - return meta.$send('禁止与其他用户重名。') - } else if (error.code === 'ER_DATA_TOO_LONG') { - return meta.$send('称呼超出长度限制。') - } - } - }) -} diff --git a/packages/plugin-common/src/index.ts b/packages/plugin-common/src/index.ts index 47a089dc44..0b49afac5e 100644 --- a/packages/plugin-common/src/index.ts +++ b/packages/plugin-common/src/index.ts @@ -2,12 +2,11 @@ import { Context, CommandConfig } from 'koishi-core' import admin from './admin' import authorize, { AuthorizeConfig } from './authorize' import broadcast, { BroadcastOptions } from './broadcast' -import callme, { CallmeOptions } from './callme' import contextify from './contextify' import echo from './echo' import exit from './exit' import help from './help' -import info from './info' +import info, { InfoOptions } from './info' import repeater, { RepeaterOptions } from './repeater' import requestHandler, { HandlerConfig } from './request-handler' import respondent, { Respondent } from './respondent' @@ -20,7 +19,6 @@ export { admin, authorize, broadcast, - callme, contextify, echo, exit, @@ -35,12 +33,11 @@ export { interface CommonPluginConfig extends HandlerConfig, AuthorizeConfig { admin?: false | CommandConfig broadcast?: false | BroadcastOptions - callme?: false | CallmeOptions contextify?: false | CommandConfig echo?: false | CommandConfig exit?: false | CommandConfig help?: false | CommandConfig - info?: false | CommandConfig + info?: false | InfoOptions repeater?: false | RepeaterOptions respondent?: Respondent[] welcome?: false | WelcomeMessage @@ -64,7 +61,6 @@ export function apply (ctx: Context, options: CommonPluginConfig = {}) { .plugin(admin, options.admin) .plugin(authorize, options) .plugin(broadcast, options.broadcast) - .plugin(callme, options.callme) .plugin(info, options.info) } } diff --git a/packages/plugin-common/src/info.ts b/packages/plugin-common/src/info.ts index 12e42a0701..3d9a24fea7 100644 --- a/packages/plugin-common/src/info.ts +++ b/packages/plugin-common/src/info.ts @@ -12,11 +12,11 @@ export function registerUserInfo (callback: UserInfoCallba } } -export interface InfoConfig extends CommandConfig { +export interface InfoOptions extends CommandConfig { getSenderName? (user: UserData, meta: Meta<'message'>): string } -const defaultConfig: InfoConfig = { +const defaultConfig: InfoOptions = { authority: 0, getSenderName (user, meta) { if (meta.userId === user.id && meta.sender) { @@ -25,7 +25,7 @@ const defaultConfig: InfoConfig = { }, } -export default function apply (ctx: Context, config: InfoConfig = {}) { +export default function apply (ctx: Context, config: InfoOptions = {}) { config = { ...defaultConfig, ...config } ctx.command('info', '查看用户信息', config) .alias('i') diff --git a/packages/plugin-common/tests/info.spec.ts b/packages/plugin-common/tests/info.spec.ts index 9033244421..4342ca5c59 100644 --- a/packages/plugin-common/tests/info.spec.ts +++ b/packages/plugin-common/tests/info.spec.ts @@ -1,6 +1,6 @@ import { MockedApp, MemoryDatabase, Session } from 'koishi-test-utils' import { registerDatabase } from 'koishi-core' -import info, { registerUserInfo, InfoConfig } from '../src/info' +import info, { registerUserInfo, InfoOptions } from '../src/info' registerDatabase('memory', MemoryDatabase) @@ -38,7 +38,7 @@ test('basic support', async () => { }) test('getSenderName', async () => { - app.plugin(info, { + app.plugin(info, { getSenderName (user, meta) { if (user.id !== meta.userId) return 'bar' }, diff --git a/packages/test-utils/src/database.ts b/packages/test-utils/src/database.ts index 7b1dac0c75..2cf37330cf 100644 --- a/packages/test-utils/src/database.ts +++ b/packages/test-utils/src/database.ts @@ -111,7 +111,7 @@ export function testDatabase (config: DatabaseConfig, options: TestDatabaseOptio }) test('observeUser merge', async () => { - const user: UserData = { id: 1000, flag: 3, name: '', authority: 1, usage: {} } + const user: UserData = { id: 1000, flag: 3, authority: 1, usage: {} } const observedUser = await db.observeUser(user, 1) expect(observedUser).toMatchObject(user) observedUser.flag = 5 diff --git a/packages/test-utils/src/utils.ts b/packages/test-utils/src/utils.ts index db55b2b17a..0329d4dda4 100644 --- a/packages/test-utils/src/utils.ts +++ b/packages/test-utils/src/utils.ts @@ -1,4 +1,3 @@ -import { SenderInfo } from 'koishi-core' import debug from 'debug' export const BASE_SELF_ID = 514 @@ -20,7 +19,3 @@ export function fromEntries (entries: Iterable) { export function createArray (length: number, create: (index: number) => T) { return Array(length).fill(undefined).map((_, index) => create(index)) } - -export function createSender (userId: number, nickname: string, card = '') { - return { userId, nickname, card, sex: 'unknown', age: 20 } as SenderInfo -}