Skip to content

Commit

Permalink
feat: new context
Browse files Browse the repository at this point in the history
  • Loading branch information
BIYUEHU committed Feb 1, 2024
1 parent cec4e04 commit 4843569
Show file tree
Hide file tree
Showing 186 changed files with 952 additions and 1,460 deletions.
23 changes: 13 additions & 10 deletions modules/testing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import Kotori from 'kotori-bot';

Kotori.command('echo <content> [num:number=3]')
.action((data, message) => {
.action((data, session) => {
Kotori.logger.debug(data, data.args[0]);
Kotori.logger.debug(message);
Kotori.logger.debug(session);
return [
`返回消息:~%message%`,
{
message: data.args[0],
},
message: data.args[0]
}
];
})
.alias('print')
.scope('group');

Kotori.regexp(/^(.*)#print$/, match => match[1]);
Kotori.regexp(/^(.*)#print$/, (match) => match[1]);

Kotori.command('ison').action((_, events) => {
if (events.api.adapter.config.master === events.userId) return `在的哟主人~`;
Expand All @@ -30,9 +30,12 @@ Kotori.on('poke', session => {
return '戳回去!!';
}); */

Kotori.on('group_decrease', session => {
session.quick([session.userId === session.operatorId ? "%target% 默默的退出了群聊" : "%target% 被 %target% 制裁了...",{
target: session.userId,
operator: session.operatorId
}]);
Kotori.on('group_decrease', (session) => {
session.quick([
session.userId === session.operatorId ? '%target% 默默的退出了群聊' : '%target% 被 %target% 制裁了...',
{
target: session.userId,
operator: session.operatorId
}
]);
});
37 changes: 37 additions & 0 deletions modules/testing/src/index1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Kotori, { CommandAccess } from 'kotori-bot';
import { join } from 'path';

/* 设置 i18n */
Kotori.i18n.use(join(__dirname, '../locales'));

/* 事件监听 */
Kotori.on('group_decrease', (session) => {
session.quick([
session.userId === session.operatorId ? '%target% 默默的退出了群聊' : '%target% 被 %target% 制裁了...',
{
target: session.userId,
operator: session.operatorId
}
]);
});

/* 中间件注册 */
ctx.midware((next, session) => {
if (session.message.startWith === '说' || session.message.includes('说')) {
session.message = `${session.api.adapter.config['command-prefix']}echo`;
}
next();
}, 10);

/* 指令注册 */
Kotori.command('echo <content> [num:number=3]')
.action((data, session) => {
Kotori.logger.debug(data, data.args[0]);
Kotori.logger.debug(message);
return [`返回消息:~%message%`, { message: data.args[0] }];
})
.alias('print')
.scope(CommandAccess.GROUP);

/* 正则注册 */
Kotori.regexp(/^(.*)#print$/, (match) => match[1]);
51 changes: 51 additions & 0 deletions modules/testing/src/index2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Context, CommandAccess, ModuleConfig, Tsu } from 'kotori-bot';

/* 设置 i18n */
export const lang = [__dirname, '../locales']; // or: const lang = path.join(__dirname, '../locales');

/* 自定义插件配置 */
export const kotoriConfigSchema = Tsu.Object({
config1: Tsu.Number().range(0, 10),
config2: Tsu.Boolean(),
config3: Tsu.Union([Tsu.Literal('on'), Tsu.Literal('off')])
});
export type Config = ModuleConfig & Tsu.infer<typeof kotoriConfigSchema>;

/* 设置依赖服务 */
export const inject = ['database'];

/* 插件入口 */
export function main(ctx: Context, config: Config) {
/* 事件监听 */
ctx.on('group_decrease', (session) => {
session.quick([
session.userId === session.operatorId ? '%target% 默默的退出了群聊' : '%target% 被 %target% 制裁了...',
{
target: session.userId,
operator: session.operatorId
}
]);
});

/* 中间件注册 */
ctx.midware((next, session) => {
if (session.message.startWith === '说' || session.message.includes('说')) {
session.message = `${session.api.adapter.config['command-prefix']}echo`;
}
next();
}, 10);

/* 指令注册 */
ctx
.command('echo <content> [num:number=3]')
.action((data, session) => {
ctx.logger.debug(data, data.args[0]);
ctx.logger.debug(message);
return [`返回消息:~%message%`, { message: data.args[0] }];
})
.alias('print')
.scope(CommandAccess.GROUP);

/* 正则注册 */
ctx.regexp(/^(.*)#print$/, (match) => match[1]);
}
60 changes: 60 additions & 0 deletions modules/testing/src/index3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Context, CommandAccess, ModuleConfig, Tsu } from 'kotori-bot';

/* 设置 i18n */
export const lang = [__dirname, '../locales'];

/* 自定义插件配置 */
export const kotoriConfigSchema = Tsu.Object({
config1: Tsu.Number().range(0, 10),
config2: Tsu.Boolean(),
config3: Tsu.Union([Tsu.Literal('on'), Tsu.Literal('off')])
});
export type Config = ModuleConfig & Tsu.infer<typeof kotoriConfigSchema>;

/* 设置依赖服务 */
export const inject = ['database'];

/* 插件入口 */
export class Main {
private ctx: Context;

private config: Config;

constructor(Ctx: Context, Config: Config) {
this.ctx = Ctx;
this.config = Config;

/* 事件监听 */
this.ctx.on('group_decrease', (session) => {
session.quick([
session.userId === session.operatorId ? '%target% 默默的退出了群聊' : '%target% 被 %target% 制裁了...',
{
target: session.userId,
operator: session.operatorId
}
]);
});

/* 中间件注册 */
this.ctx.midware((next, session) => {
if (session.message.startWith === '说' || session.message.includes('说')) {
session.message = `${session.api.adapter.config['command-prefix']}echo`;
}
next();
}, 10);

/* 指令注册 */
this.ctx
.command('echo <content> [num:number=3]')
.action((data, session) => {
ctx.logger.debug(data, data.args[0]);
ctx.logger.debug(message);
return [`返回消息:~%message%`, { message: data.args[0] }];
})
.alias('print')
.scope(CommandAccess.GROUP);

/* 正则注册 */
this.ctx.regexp(/^(.*)#print$/, (match) => match[1]);
}
}
67 changes: 67 additions & 0 deletions modules/testing/src/index4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { plugin, Tsu, ModuleConfig, EventsList, CommandAccess, CommandAction } from 'kotori-bot';

const test = plugin({
name: 'kotori-plugin-testing',
kotori: {
enforce: 'post',
meta: {
languages: ['en_US', 'ja_JP', 'zh_TW', 'zh_CN']
}
}
}); // or: const test = plugin(loadConfig(`${__dirname}/../package.json`));

const kotoriConfigSchema = Tsu.Object({
config1: Tsu.Number().range(0, 10),
config2: Tsu.Boolean(),
config3: Tsu.Union([Tsu.Literal('on'), Tsu.Literal('off')])
});
type Config = ModuleConfig & Tsu.infer<typeof kotoriConfigSchema>;

/* 插件入口 */
@test.import({
lang: [__dirname, '../locales'], // 设置 i18n
config: kotoriConfigSchema, // 自定义插件配置
inject: ['database'] // 设置全局的依赖服务
})
class Test {
constructor(private Ctx: Context, private Config: Config) { };

/* 事件监听 */
@test.event({ type: 'group_decrease', inject: ['console'] /* 设置该方法特有的依赖服务 */ });
groupDecrease(session: EventsList['group_decrease']) {
session.quick([
session.userId === session.operatorId ? '%target% 默默的退出了群聊' : '%target% 被 %target% 制裁了...',
{
target: session.userId,
operator: session.operatorId
}
]);
}

/* 中间件注册 */
@test.midware(/* {priority: 10} 设置*/)
midware(next: () => void, session: EventsList['midwares']) {
if (session.message.startWith === '说' || session.message.includes('说')) {
session.message = `${session.api.adapter.config['command-prefix']}echo`;
}
next();
};

/* 指令注册 */
@test.command({
template: 'echo <content> [num:number=3]',
scope: CommandAccess.GROUP
/* 可在此处传入额外配置也可以链式 */
}).alias('print')
echo(data: Parameters<CommandAction>[0], session: Parameters<CommandAction>[1]) {
session.ctx.logger.debug(data, data.args[0]);
session.ctx.logger.debug(session);
return [`返回消息:~%message%`, { message: data.args[0] }];
}

/* 正则注册 */
@test.regexp({ regexp: /^(.*)#print$/ })
print(match: RegExpExecArray) {
return match[1];
};
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Context, MessageQuickReal, Tsu } from 'kotori-bot';
import { Context, MessageQuick, MessageQuickReal, Tsu } from 'kotori-bot';

const hitokotoSchema = Tsu.Object({
data: Tsu.Object({
msg: Tsu.String(),
from: Tsu.String().optional(),
likes: Tsu.Number(),
type: Tsu.String(),
}),
type: Tsu.String()
})
});

const hitokotosSchema = Tsu.Object({
data: Tsu.Object({
msg: Tsu.String(),
}),
msg: Tsu.String()
})
});

export const lang = [__dirname, '../locales'];
Expand All @@ -30,7 +30,7 @@ export function main(ctx: Context) {
'\n网抑云 毒鸡汤' +
'\n舔狗语录 爱情语录' +
'\n温柔语录 个性签名' +
'\n经典语录 英汉语录',
'\n经典语录 英汉语录'
);

ctx.command('hitokoto - hitokotos.descr.hitokoto.help').action(async () => {
Expand All @@ -46,9 +46,10 @@ export function main(ctx: Context) {
};

ctx.midware((next, session) => {
if (session.message === '一言') session.message = `${session.api.adapter.config['command-prefix']}hitokoto`;
const s = session;
if (s.message === '一言') s.message = `${s.api.adapter.config['command-prefix']}hitokoto`;
next();
})
});
ctx.regexp(/^一言2$/, () => hitokotoT(1));
ctx.regexp(/^骚话$/, () => hitokotoT(2));
ctx.regexp(/^情话$/, () => hitokotoT(3));
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,5 @@
"packageManager": "pnpm@8.7.4",
"engines": {
"node": ">=17.9.0"
},
"dependencies": {
"@types/minimist": "^1.2.5",
"minimist": "^1.2.8",
"tsukiko": "^1.2.1"
}
}
27 changes: 18 additions & 9 deletions packages/core/src/base/config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import { join } from 'path';
import { loadConfig } from '@kotori-bot/tools';
import { KotoriConfig as KotoriConfigType, PackageInfo, kotoriConfigSchema, packageInfoSchema } from '../types';
import { CoreConfig, PackageInfo, coreConfigSchema, packageInfoSchema } from '../types/index';
import { CoreError } from '../utils/errror';

type ConfigType = KotoriConfigType & { pkg: PackageInfo };
type ConfigType = CoreConfig & { pkg: PackageInfo };

export class KotoriConfig implements ConfigType {
declare module '../context/index' {
interface Context {
readonly pkg: PackageInfo;
readonly baseDir: CoreConfig['baseDir'];
readonly config: CoreConfig['config'];
readonly options: CoreConfig['options'];
}
}

export class Config implements ConfigType {
readonly pkg: PackageInfo;

readonly baseDir: KotoriConfigType['baseDir'];
readonly baseDir: CoreConfig['baseDir'];

readonly config: KotoriConfigType['config'];
readonly config: CoreConfig['config'];

readonly options: KotoriConfigType['options'];
readonly options: CoreConfig['options'];

constructor(config?: KotoriConfigType) {
constructor(config?: CoreConfig) {
const info = loadConfig(join(__dirname, '../../package.json')) as unknown;
if (!info || Object.values(info).length === 0) throw new CoreError('Cannot find kotori-bot package.json');
const result = packageInfoSchema.parseSafe(info);
if (!result.value) throw new CoreError(`File package.json format error: ${result.error.message}`);
this.pkg = result.data;
const handle = kotoriConfigSchema.parseSafe(config);
const handle = coreConfigSchema.parseSafe(config);
if (!handle.value) throw new CoreError('Unexpected error in parsing config (baseDir,kotori,options)');
this.baseDir = handle.data.baseDir;
this.config = handle.data.config;
this.options = handle.data.options;
}
}

export default KotoriConfig;
export default Config;

0 comments on commit 4843569

Please sign in to comment.