feat: support receiver
This commit is contained in:
parent
2463dbbc2b
commit
ae8eeb9a89
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "koishi-plugin-market-info",
|
"name": "koishi-plugin-market-info",
|
||||||
"description": "Koishi plugin market information",
|
"description": "Koishi plugin market information",
|
||||||
"version": "2.0.1",
|
"version": "2.1.0",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"typings": "lib/index.d.ts",
|
"typings": "lib/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
|
@ -1,27 +1,25 @@
|
|||||||
import { Context, Dict, Logger, Schema, Time } from 'koishi'
|
import { Context, Dict, Schema, Time, deepEqual, pick, sleep } from 'koishi'
|
||||||
import {} from '@koishijs/plugin-market'
|
import {} from '@koishijs/plugin-market'
|
||||||
import type { SearchObject, SearchResult } from '@koishijs/registry'
|
import type { SearchObject, SearchResult } from '@koishijs/registry'
|
||||||
|
|
||||||
const logger = new Logger('market')
|
|
||||||
|
|
||||||
export const name = 'market-info'
|
export const name = 'market-info'
|
||||||
|
|
||||||
export interface Rule {
|
interface Receiver {
|
||||||
platform: string
|
platform: string
|
||||||
|
selfId: string
|
||||||
channelId: string
|
channelId: string
|
||||||
selfId?: string
|
|
||||||
guildId?: string
|
guildId?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Rule: Schema<Rule> = Schema.object({
|
const Receiver: Schema<Receiver> = Schema.object({
|
||||||
platform: Schema.string().description('平台名称。').required(),
|
platform: Schema.string().required().description('平台名称。'),
|
||||||
channelId: Schema.string().description('频道 ID。').required(),
|
selfId: Schema.string().required().description('机器人 ID。'),
|
||||||
|
channelId: Schema.string().required().description('频道 ID。'),
|
||||||
guildId: Schema.string().description('群组 ID。'),
|
guildId: Schema.string().description('群组 ID。'),
|
||||||
selfId: Schema.string().description('机器人 ID。'),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
rules: Rule[]
|
rules: Receiver[]
|
||||||
endpoint: string
|
endpoint: string
|
||||||
interval: number
|
interval: number
|
||||||
showHidden: boolean
|
showHidden: boolean
|
||||||
@ -31,7 +29,7 @@ export interface Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const Config: Schema<Config> = Schema.object({
|
export const Config: Schema<Config> = Schema.object({
|
||||||
rules: Schema.array(Rule).description('推送规则。'),
|
rules: Schema.array(Receiver).role('table').description('推送规则列表。'),
|
||||||
endpoint: Schema.string().default('https://registry.koishi.chat/index.json').description('插件市场地址。'),
|
endpoint: Schema.string().default('https://registry.koishi.chat/index.json').description('插件市场地址。'),
|
||||||
interval: Schema.number().default(Time.minute * 30).description('轮询间隔 (毫秒)。'),
|
interval: Schema.number().default(Time.minute * 30).description('轮询间隔 (毫秒)。'),
|
||||||
showHidden: Schema.boolean().default(false).description('是否显示隐藏的插件。'),
|
showHidden: Schema.boolean().default(false).description('是否显示隐藏的插件。'),
|
||||||
@ -43,6 +41,8 @@ export const Config: Schema<Config> = Schema.object({
|
|||||||
export function apply(ctx: Context, config: Config) {
|
export function apply(ctx: Context, config: Config) {
|
||||||
ctx.i18n.define('zh', require('./locales/zh-CN'))
|
ctx.i18n.define('zh', require('./locales/zh-CN'))
|
||||||
|
|
||||||
|
const logger = ctx.logger('market')
|
||||||
|
|
||||||
const makeDict = (result: SearchResult) => {
|
const makeDict = (result: SearchResult) => {
|
||||||
const dict: Dict<SearchObject> = {}
|
const dict: Dict<SearchObject> = {}
|
||||||
for (const object of result.objects) {
|
for (const object of result.objects) {
|
||||||
@ -61,7 +61,27 @@ export function apply(ctx: Context, config: Config) {
|
|||||||
let previous = await getMarket()
|
let previous = await getMarket()
|
||||||
|
|
||||||
ctx.command('market [name]')
|
ctx.command('market [name]')
|
||||||
.action(async ({ session }, name) => {
|
.option('receive', '-r', { authority: 3, value: true })
|
||||||
|
.option('receive', '-R', { authority: 3, value: false })
|
||||||
|
.action(async ({ session, options }, name) => {
|
||||||
|
if (typeof options.receive === 'boolean') {
|
||||||
|
const index = config.rules.findIndex(receiver => {
|
||||||
|
return deepEqual(
|
||||||
|
pick(receiver, ['platform', 'selfId', 'channelId', 'guildId']),
|
||||||
|
pick(session, ['platform', 'selfId', 'channelId', 'guildId']),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
if (options.receive) {
|
||||||
|
if (index >= 0) return session.text('.not-modified')
|
||||||
|
config.rules.push(pick(session, ['platform', 'selfId', 'channelId', 'guildId']))
|
||||||
|
} else {
|
||||||
|
if (index < 0) return session.text('.not-modified')
|
||||||
|
config.rules.splice(index, 1)
|
||||||
|
}
|
||||||
|
ctx.scope.update(config, false)
|
||||||
|
return session.text('.updated')
|
||||||
|
}
|
||||||
|
|
||||||
if (!name) {
|
if (!name) {
|
||||||
const objects = Object.values(previous).filter(data => !data.manifest.hidden)
|
const objects = Object.values(previous).filter(data => !data.manifest.hidden)
|
||||||
return session.text('.overview', [objects.length])
|
return session.text('.overview', [objects.length])
|
||||||
@ -106,15 +126,12 @@ export function apply(ctx: Context, config: Config) {
|
|||||||
|
|
||||||
const content = ['[插件市场更新]', ...diff].join('\n')
|
const content = ['[插件市场更新]', ...diff].join('\n')
|
||||||
logger.info(content)
|
logger.info(content)
|
||||||
for (let { channelId, platform, selfId, guildId } of config.rules) {
|
const delay = ctx.root.config.delay.broadcast
|
||||||
if (!selfId) {
|
for (let index = 0; index < config.rules.length; ++index) {
|
||||||
const channel = await ctx.database.getChannel(platform, channelId, ['assignee', 'guildId'])
|
if (index && delay) await sleep(delay)
|
||||||
if (!channel || !channel.assignee) return
|
const { platform, selfId, channelId, guildId } = config.rules[index]
|
||||||
selfId = channel.assignee
|
const bot = ctx.bots.find(bot => bot.platform === platform && bot.selfId === selfId)
|
||||||
guildId = channel.guildId
|
bot.sendMessage(channelId, content, guildId)
|
||||||
}
|
|
||||||
const bot = ctx.bots[`${platform}:${selfId}`]
|
|
||||||
bot?.sendMessage(channelId, content, guildId)
|
|
||||||
}
|
}
|
||||||
}, config.interval)
|
}, config.interval)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user