Skip to content

Commit

Permalink
chore: update many plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Jul 30, 2024
1 parent 85b58ea commit 9deb638
Show file tree
Hide file tree
Showing 54 changed files with 932 additions and 629 deletions.
14 changes: 6 additions & 8 deletions modules/adapter-cmd/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:09
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2024-07-27 14:45:43
* @LastEditTime: 2024-07-30 19:49:25
*/
import { Adapter, type AdapterConfig, type Context, MessageScope, Tsu } from 'kotori-bot'
import CmdApi from './api'
import CmdElements from './elements'

export const config = Tsu.Object({
nickname: Tsu.String().default('Kotarou'),
age: Tsu.Number().min(0).default(18),
sex: Tsu.Union(Tsu.Literal('male'), Tsu.Literal('female')),
'self-nickname': Tsu.String().default('KotoriO'),
'self-id': Tsu.String().default('720')
})

type CmdConfig = Tsu.infer<typeof config> & AdapterConfig

export class CmdAdapter extends Adapter<CmdApi, CmdConfig> {
export class CmdAdapter extends Adapter<CmdApi, CmdConfig, CmdElements> {
private messageId = ''

public readonly platform = 'cmd'

public readonly api: CmdApi

public readonly elements = new CmdElements()
public readonly elements: CmdElements

public constructor(ctx: Context, config: CmdConfig, identity: string) {
super(ctx, config, identity)
this.api = new CmdApi(this)
this.elements = new CmdElements(this)
this.selfId = config['self-id']
process.stdin.on('data', (data) => this.handle(data))
}
Expand All @@ -44,11 +43,10 @@ export class CmdAdapter extends Adapter<CmdApi, CmdConfig> {
type: MessageScope.PRIVATE,
messageId: this.messageId,
message,
messageAlt: message,
userId: this.config.master,
sender: {
nickname: this.config.nickname,
sex: this.config.sex,
age: this.config.age
nickname: this.config.nickname
},
time: Date.now()
})
Expand Down
11 changes: 8 additions & 3 deletions modules/adapter-cmd/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
* @Blog: https://hotaru.icu
* @Date: 2023-09-29 14:31:13
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2023-12-02 16:21:22
* @LastEditTime: 2024-07-30 19:06:41
*/
import { Api, string, Message } from 'kotori-bot'
import { Api, type Message } from 'kotori-bot'

export class CmdApi extends Api {
public sendPrivateMsg(message: Message, userId: string) {
public getSupportedEvents(): ReturnType<Api['getSupportedEvents']> {
return ['on_message']
}

public async sendPrivateMsg(message: Message, userId: string) {
this.adapter.send('send_private_msg', { user_id: userId, message })
return { messageId: '', time: 0 }
}
}

Expand Down
57 changes: 32 additions & 25 deletions modules/adapter-cmd/src/elements.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import { Elements, string, none } from 'kotori-bot'
import { Elements, type Message, type MessageMapping, MessageSingle } from 'kotori-bot'

export class CmdElements extends Elements {
public at(target: string) {
none(this)
return `@${target} `
public getSupportsElements(): (keyof MessageMapping)[] {
return ['mention', 'mentionAll', 'image', 'voice', 'video', 'text', 'location', 'file']
}

public image(url: string) {
none(this)
return `[image,${url}]`
public decode(message: Exclude<Message, string>): string {
if (!(message instanceof MessageSingle))
return Array.from(message)
.map((el) => this.decode(el))
.join(' ')
switch (message.data.type) {
case 'text':
return message.toString()
case 'image':
return `[image,${message.data.content}]`
case 'voice':
return `[voice,${message.data.content}]`
case 'video':
return `[video,${message.data.content}]`
case 'file':
return `[file,${message.data.content}]`
case 'location':
return `[location,${message.data.content}]`
case 'mention':
return `@${message.data.userId}`
case 'mentionAll':
return '@all'
case 'reply':
return `[reply,${message.data.messageId}]`
default:
return `[${message.data.type},unsupported element]`
}
}

public voice(url: string) {
none(this)
return `[voice,${url}]`
}

public video(url: string) {
none(this)
return `[video,${url}]`
}

public face(id: string) {
none(this)
return `[face,${id}]`
}

public file(data: string) {
none(data, this)
return `[file]`
public encode(raw: string): Message {
return new MessageSingle('text', { text: raw })
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/better-join/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class Main {
}

protected handle(session: EventsList['on_group_increase']) {
const standard = randomInt(0, 2)
const standard = randomInt(1, 3)
for (let init = 0; init < standard; init += 1) {
session.quick([data[randomInt(0, Object.keys(data).length)], { at: session.el.mention(session.userId) }])
session.quick([data[randomInt(0, Object.keys(data).length)], [session.el.mention(session.userId)]])
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/color/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ColorPlugin extends KotoriPlugin {
} else {
data = randomFromArray(colorData)
}
if (!data) return ['color.msg.color.error2', [session.api.adapter.config['command-prefix']]]
if (!data) return session.format('color.msg.color.error2', [session.api.adapter.config['command-prefix']])

const { rgb } = data
return Messages(
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Blog: https://hotaru.icu
* @Date: 2023-07-11 14:18:27
* @LastEditors: Hotaru [email protected]
* @LastEditTime: 2024-07-29 19:34:45
* @LastEditTime: 2024-07-30 16:17:29
*/

import { UserAccess, CommandError, type Context, MessageScope, TsuError, type LocaleType, Symbols } from 'kotori-bot'
Expand Down Expand Up @@ -197,7 +197,7 @@ export function main(ctx: Context) {
let modulesList = Array.from(ctx[Symbols.modules].values())
if (name) {
modulesList = modulesList.filter(([{ pkg }]) => pkg.name.startsWith(name))
if (modulesList.length === 0) return ['core.msg.module.not_found', { name }]
if (modulesList.length === 0) return session.format('core.msg.module.not_found', { name })
}
return session.format('core.msg.module', {
list: modulesList
Expand Down
4 changes: 2 additions & 2 deletions modules/drift-bottle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const lang = [__dirname, '../locales']
export const inject = ['file']

export const config = Tsu.Object({
max: Tsu.Number().default(4)
max: Tsu.Number().default(4).describe('The person can throw bottles at most one day')
})

type Bottle = [string, number, number | string, (number | string)?]
Expand Down Expand Up @@ -49,7 +49,7 @@ export function main(ctx: Context, conf: Tsu.infer<typeof config>) {
const data = getBottle(session.api.adapter.identity)
if (!data || data.length <= 0) return 'drift_bottle.msg.pick.none'

const bottle = data[randomInt(0, data.length - 1)]
const bottle = data[randomInt(0, data.length)]
return session.format('drift_bottle.msg.pick.info', [
bottle[0],
session.i18n.time(new Date(bottle[1])),
Expand Down
65 changes: 57 additions & 8 deletions modules/filter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@
import { Tsu, type Context, plugins, Symbols, CORE_MODULES, KotoriPlugin } from 'kotori-bot'
import {
Tsu,
type Context,
plugins,
Symbols,
CORE_MODULES,
KotoriPlugin,
FilterTestList,
type FilterOption,
Filter
} from 'kotori-bot'
import pm from 'picomatch'

declare module 'kotori-bot' {
interface ModuleConfig {
filter?: FilterOption
}
}

const plugin = plugins([__dirname, '../'])
// TODO: modules context filter

export const filterOptionBaseSchema = Tsu.Object({
test: Tsu.Custom<FilterTestList>(
(value) => typeof value === 'string' && Object.values(FilterTestList).includes(value as FilterTestList)
).describe('Testing item'),
operator: Tsu.Union(
Tsu.Literal('=='),
Tsu.Literal('!='),
Tsu.Literal('>'),
Tsu.Literal('<'),
Tsu.Literal('>='),
Tsu.Literal('<=')
).describe('Testing operation'),
value: Tsu.Union(Tsu.String(), Tsu.Number(), Tsu.Boolean()).describe('Expect value')
})

export const filterOptionGroupSchema = Tsu.Object({
type: Tsu.Union(Tsu.Literal('all_of'), Tsu.Literal('any_of'), Tsu.Literal('none_of')),
filters: Tsu.Array(filterOptionBaseSchema)
})

export const filterOptionSchema = Tsu.Union(filterOptionBaseSchema, filterOptionGroupSchema)

@plugin.import
export class FilterPlugin extends KotoriPlugin<Tsu.infer<typeof FilterPlugin.schema>> {
@plugin.schema
public static schema = Tsu.Object({
mode: Tsu.Union(Tsu.Literal('include'), Tsu.Literal('exclude')).default('exclude'),
list: Tsu.Array(Tsu.String()).default([])
mode: Tsu.Union(Tsu.Literal('include'), Tsu.Literal('exclude')).default('exclude').describe('Filter mode'),
list: Tsu.Array(Tsu.String()).default([]).describe('Filter list (supports string match)')
})

public constructor(ctx: Context, config: Tsu.infer<typeof FilterPlugin.schema>) {
super(ctx, config)
this.loaderFilter()
this.filterModules()
this.filterSet()
}

private loaderFilter() {
private filterModules() {
const { mode, list } = this.config
const runner = this.ctx.get<{ [Symbols.modules]: Map<string, [{ main?: string }, object]> }>('runner')
if (!runner) {
Expand All @@ -35,6 +74,16 @@ export class FilterPlugin extends KotoriPlugin<Tsu.infer<typeof FilterPlugin.sch
}
})
}
}

// plugin.import(Plugin);
private filterSet() {
for (const [name, [, { filter }]] of this.ctx[Symbols.modules]) {
if (!filter) continue
const result = filterOptionSchema.parseSafe(filter)
if (!result.value) {
this.ctx.logger.error(`filter option of module ${name} is invalid`, result.error)
continue
}
this.ctx[Symbols.filter].set(name, new Filter(result.data))
}
}
}
2 changes: 1 addition & 1 deletion modules/github/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kotori-plugin-github",
"version": "1.2.2",
"version": "1.3.0",
"description": "github repository searching",
"main": "lib/index.js",
"keywords": ["kotori", "chatbot", "kotori-plugin"],
Expand Down
55 changes: 37 additions & 18 deletions modules/github/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { type Context, Messages, Tsu } from 'kotori-bot'
import { parse } from 'node:url'

const HEAD_URL = 'https://opengraph.githubassets.com/c9f4179f4d560950b2355c82aa2b7750bffd945744f9b8ea3f93cc24779745a0'

const githubSchema = Tsu.Union(
Tsu.Object({
Expand All @@ -21,24 +24,40 @@ const githubSchema = Tsu.Union(
export const lang = [__dirname, '../locales']

export function main(ctx: Context) {
ctx.command('github <repository> - github.descr.github').action(async (data, session) => {
const res = githubSchema.parse(await ctx.http.get(`https://api.github.com/repos/${data.args[0]}`))
if (!('full_name' in res)) return session.format('github.msg.github.fail', { input: data.args[0] })
session.quick([
'github.msg.github',
{
name: res.full_name || 'BOT_RESULT.EMPTY',
description: res.description || 'BOT_RESULT.EMPTY',
language: res.language || 'BOT_RESULT.EMPTY',
author: res.owner ? res.owner.login || 'BOT_RESULT.EMPTY' : 'BOT_RESULT.EMPTY',
create: res.created_at || 'BOT_RESULT.EMPTY',
update: res.updated_at || 'BOT_RESULT.EMPTY',
push: res.pushed_at || 'BOT_RESULT.EMPTY',
license: res.license ? res.license.name || 'BOT_RESULT.EMPTY' : 'BOT_RESULT.EMPTY'
ctx.command('github <content> - github.descr.github').action(async ({ args: [content] }, session) => {
// it is a repository
if (content.split('/').length === 2) {
const res = githubSchema.parse(await ctx.http.get(`https://api.github.com/repos/${content}`))
if (!('full_name' in res)) {
return session.format('github.msg.github.fail', { input: content })
}
])
return Messages.image(
`https://opengraph.githubassets.com/c9f4179f4d560950b2355c82aa2b7750bffd945744f9b8ea3f93cc24779745a0/${res.full_name}`
)
session.quick([
'github.msg.github',
{
name: res.full_name,
description: res.description,
language: res.language,
author: res.owner?.login,
create: res.created_at,
update: res.updated_at,
push: res.pushed_at,
license: res.license ? res.license.name : 'BOT_RESULT.EMPTY'
}
])
}
return Messages.image(`${HEAD_URL}/${content}`)
})

// * github pull request https://github.com/kotorijs/kotori/issues/3
// * github repository github.com/kotorijs/kotori
// * github issue http://github.com/kotorijs/kotori/pull/2
// ! not match https://gitee.com/shit/shit
// ! not match https://github.com/kotori
// ! not match https://github.com/kotorijs/kotori/blob/
// ! not match https://github.dev/kotorijs/kotori

ctx.regexp(/(https?:\/\/)?github.com\/[\w-]+\/[\w]+\/?(\/(pull|issues)\/\d+)?$/, (_, session) => {
const { pathname } = parse(session.message.toString())
return Messages.image(`${HEAD_URL}${pathname}`)
})
}
34 changes: 17 additions & 17 deletions modules/goodnight/locales/en_US.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"goodnight.msg.morning.male": "boy",
"goodnight.msg.morning.female": "girl",
"goodnight.msg.morning.already": "%at%You already got up today, didn't you? (╬ ̄皿 ̄)",
"goodnight.msg.morning.morning": "%at%Good morning! You are the %count% %sex% to wake up today ( ̄▽ ̄)~*",
"goodnight.msg.afternoon.afternoon": "%at%It's already afternoon! You are the %count% %sex% to wake up today~",
"goodnight.msg.morning.early": "%at%Please get enough sleep, you shouldn't wake up before %hour%:00 (╬ ̄皿 ̄)=○",
"goodnight.msg.morning.late": "%at%Waking up now is even worse than not waking up! You are the %count% %sex% to wake up today",
"goodnight.msg.night.already": "%at%You already said good night, go to bed early~",
"goodnight.msg.night.not": "%at%Don't sleep without waking up, just die in your sleep! (▼ヘ▼#)",
"goodnight.msg.night.less": "%at%You just woke up not long ago and now you want to sleep, don't challenge my bottom line! (#`皿′)",
"goodnight.msg.night.later": "%at%Wow it's really late now, your wake time today is %time%, you need to take responsibility for your own health, I really care about you (;′⌒`)",
"goodnight.msg.night.late": "%at%Good night~ Don't stay up too late, your wake time today is %time% (*^▽^*)",
"goodnight.msg.night.normal": "%at%Good night, your wake time today is %time% (*^▽^*)",
"goodnight.msg.night.early": "%at%You're sleeping too early, your wake time today is %time% ψ(*`ー′)ψ",
"goodnight.msg.night.hours": "hours",
"goodnight.msg.night.minutes": "minutes",
"goodnight.msg.night.seconds": "seconds"
"goodnight.msg.morning.male": "boy",
"goodnight.msg.morning.female": "girl",
"goodnight.msg.morning.already": "{0}You already got up today, didn't you? (╬ ̄皿 ̄)",
"goodnight.msg.morning.morning": "{0}Good morning! You are the {1} {2} to wake up today ( ̄▽ ̄)~*",
"goodnight.msg.afternoon.afternoon": "{0}It's already afternoon! You are the {1} {2} to wake up today~",
"goodnight.msg.morning.early": "{0}Please get enough sleep, you shouldn't wake up before {1}:00 (╬ ̄皿 ̄)=○",
"goodnight.msg.morning.late": "{0}Waking up now is even worse than not waking up! You are the {1} {2} to wake up today",
"goodnight.msg.night.already": "{0}You already said good night, go to bed early~",
"goodnight.msg.night.not": "{0}Don't sleep without waking up, just die in your sleep! (▼ヘ▼#)",
"goodnight.msg.night.less": "{0}You just woke up not long ago and now you want to sleep, don't challenge my bottom line! (#`皿′)",
"goodnight.msg.night.later": "{0}Wow it's really late now, your wake time today is {1}, you need to take responsibility for your own health, I really care about you (;′⌒`)",
"goodnight.msg.night.late": "{0}Good night~ Don't stay up too late, your wake time today is {1} (*^▽^*)",
"goodnight.msg.night.normal": "{0}Good night, your wake time today is {1} (*^▽^*)",
"goodnight.msg.night.early": "{0}You're sleeping too early, your wake time today is {1} ψ(*`ー′)ψ",
"goodnight.msg.night.hours": "hours",
"goodnight.msg.night.minutes": "minutes",
"goodnight.msg.night.seconds": "seconds"
}
Loading

0 comments on commit 9deb638

Please sign in to comment.