Skip to content

Commit

Permalink
feat: add general tips for booru users (#176)
Browse files Browse the repository at this point in the history
* feat: add general tips for booru users

* feat: Random.pick

* feat: add configuration for whether show tips

* docs: add config for showTips coniguration
  • Loading branch information
MaikoTan authored Mar 21, 2024
1 parent bd17daf commit 31f2805
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/zh-CN/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@

设置输出的信息。

## showTips

- 类型: `boolean`
- 默认值: `true`

是否输出使用提示信息。

## 图源全局设置

:::tip
Expand Down
27 changes: 26 additions & 1 deletion packages/core/src/command.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Session } from 'koishi'
import { Channel, Context, Random, Session, User } from 'koishi'
import { Config, OutputType, SpoilerType } from '.'

export const inject = {
Expand All @@ -7,6 +7,18 @@ export const inject = {
}

export function apply(ctx: Context, config: Config) {
const getTips = (session: Session) => {
for (const locale of ctx.i18n.fallback([
...((session.user as User.Observed)?.locales || []),
...((session.channel as Channel.Observed)?.locales || []),
])) {
if (ctx.i18n._data[locale]) {
const tips = Object.keys(ctx.i18n._data[locale] || {}).filter((k) => k.startsWith('booru.tips'))
if (tips.length) return tips
}
}
}

const count = (value: string, session: Session) => {
const count = parseInt(value)
if (count < 1 || count > config.maxCount) {
Expand Down Expand Up @@ -59,6 +71,19 @@ export function apply(ctx: Context, config: Config) {
const output: Element[] = []

for (const image of filtered) {
if (config.showTips) {
const tips = getTips(session)
if (tips) {
const tip = Random.pick(tips)
output.unshift(
<p>
<i18n path='.tips'></i18n>
<i18n path={tip}></i18n>
</p>,
)
}
}

if (config.asset && ctx.assets) {
image.url = await ctx.booru.imgUrlToAssetUrl(image)
if (!image.url) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export interface Config {
asset: boolean
base64: boolean
spoiler: SpoilerType
showTips: boolean
}

interface ImageArray extends Array<ImageSource.Result> {
Expand Down Expand Up @@ -181,6 +182,7 @@ export const Config = Schema.intersect([
.description('发送为隐藏图片,单击后显示(在 QQ 平台中以「合并转发」发送)。')
.default(0)
.experimental(),
showTips: Schema.boolean().default(true).description('是否输出使用提示信息。'),
}).description('输出设置'),
])

Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ commands:
link: '页面地址: {0}'
source: '图源: {0}'
tags: '标签: {0}'
tips: '<b>提示:</b>{0}'

booru:
tips:
general:
- 不带参数调用此命令将会随机返回一张图片
- 可以使用 <code>-l &lt;label&gt;</code> 或 <code>--label &lt;label&gt;</code> 参数指定图源标签
- 可以使用 <code>-c &lt;count&gt;</code> 或 <code>--count &lt;count&gt;</code> 参数指定返回图片数量
- 指定的图片数量与实际返回的图片数量可能有所不同

0 comments on commit 31f2805

Please sign in to comment.