Skip to content

Commit

Permalink
feat(info): custom getSenderName
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 16, 2020
1 parent 87be3c1 commit 5c38348
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
31 changes: 25 additions & 6 deletions packages/plugin-common/src/info.ts
Original file line number Diff line number Diff line change
@@ -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 <K extends UserField = UserField> = (user: Pick<UserData, K>) => string

Expand All @@ -12,8 +12,22 @@ export function registerUserInfo <K extends UserField> (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 })
Expand All @@ -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) {
Expand Down
40 changes: 31 additions & 9 deletions packages/plugin-common/tests/info.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
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)

// make coverage happy
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<InfoConfig>(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')
})

0 comments on commit 5c38348

Please sign in to comment.