diff --git a/packages/plugin-common/src/info.ts b/packages/plugin-common/src/info.ts index bf7b020a47..12e42a0701 100644 --- a/packages/plugin-common/src/info.ts +++ b/packages/plugin-common/src/info.ts @@ -1,4 +1,4 @@ -import { Context, getSenderName, UserField, UserData, getTargetId, CommandConfig } from 'koishi-core' +import { Context, UserField, UserData, getTargetId, CommandConfig, Meta } from 'koishi-core' type UserInfoCallback = (user: Pick) => string @@ -12,8 +12,22 @@ export function registerUserInfo (callback: UserInfoCallba } } -export default function apply (ctx: Context, config: CommandConfig = {}) { - ctx.command('info', '查看用户信息', { authority: 0, ...config }) +export interface InfoConfig extends CommandConfig { + getSenderName? (user: UserData, meta: Meta<'message'>): string +} + +const defaultConfig: InfoConfig = { + authority: 0, + getSenderName (user, meta) { + if (meta.userId === user.id && meta.sender) { + return meta.sender.card || meta.sender.nickname + } + }, +} + +export default function apply (ctx: Context, config: InfoConfig = {}) { + config = { ...defaultConfig, ...config } + ctx.command('info', '查看用户信息', config) .alias('i') .shortcut('我的信息') .option('-u, --user [target]', '指定目标', { authority: 3 }) @@ -25,10 +39,15 @@ export default function apply (ctx: Context, config: CommandConfig = {}) { if (!id) return meta.$send('未找到用户。') user = await ctx.database.getUser(id, -1, Array.from(infoFields)) if (!user) return meta.$send('未找到用户。') - output.push(`用户 ${id} 的权限为 ${user.authority} 级。`) + const name = config.getSenderName(user, meta) + if (!name) { + output.push(`${id} 的权限为 ${user.authority} 级。`) + } else { + output.push(`${name} (${id}) 的权限为 ${user.authority} 级。`) + } } else { - user = await ctx.database.getUser(meta.userId, 0, Array.from(infoFields)) - output.push(`${getSenderName(meta)},您的权限为 ${user.authority} 级。`) + user = await ctx.database.getUser(meta.userId, Array.from(infoFields)) + output.push(`${config.getSenderName(user, meta) || meta.userId},您的权限为 ${user.authority} 级。`) } for (const callback of infoCallbacks) { diff --git a/packages/plugin-common/tests/info.spec.ts b/packages/plugin-common/tests/info.spec.ts index 0a31cc1eff..9033244421 100644 --- a/packages/plugin-common/tests/info.spec.ts +++ b/packages/plugin-common/tests/info.spec.ts @@ -1,6 +1,6 @@ -import { MockedApp, MemoryDatabase } from 'koishi-test-utils' +import { MockedApp, MemoryDatabase, Session } from 'koishi-test-utils' import { registerDatabase } from 'koishi-core' -import info, { registerUserInfo } from '../src/info' +import info, { registerUserInfo, InfoConfig } from '../src/info' registerDatabase('memory', MemoryDatabase) @@ -8,22 +8,44 @@ registerDatabase('memory', MemoryDatabase) registerUserInfo(() => '') registerUserInfo(() => 'foo', ['flag']) -const app = new MockedApp({ database: { memory: {} } }) -const session = app.createSession('user', 123) +let app: MockedApp, session: Session -app.plugin(info) - -beforeAll(async () => { +beforeEach(async () => { + app = new MockedApp({ database: { memory: {} } }) + session = app.createSession('user', 123) await app.start() await app.database.getUser(123, 3) await app.database.getUser(456, 4) }) -afterAll(() => app.stop()) +afterEach(() => app.stop()) test('basic support', async () => { + app.plugin(info) + + session.meta.sender = { + userId: 123, + nickname: 'nick', + card: '', + sex: 'unknown', + age: 20, + } + + await session.shouldHaveReply('info', 'nick,您的权限为 3 级。\nfoo') + await session.shouldHaveReply('info -u', '未找到用户。') + await session.shouldHaveReply('info -u 654', '未找到用户。') + await session.shouldHaveReply('info -u 456', '456 的权限为 4 级。\nfoo') +}) + +test('getSenderName', async () => { + app.plugin(info, { + getSenderName (user, meta) { + if (user.id !== meta.userId) return 'bar' + }, + }) + await session.shouldHaveReply('info', '123,您的权限为 3 级。\nfoo') await session.shouldHaveReply('info -u', '未找到用户。') await session.shouldHaveReply('info -u 654', '未找到用户。') - await session.shouldHaveReply('info -u 456', '用户 456 的权限为 4 级。\nfoo') + await session.shouldHaveReply('info -u 456', 'bar (456) 的权限为 4 级。\nfoo') })