Commit ef418c37 authored by nanahira's avatar nanahira

total rework

parent 34b9b185
This diff is collapsed.
...@@ -29,8 +29,7 @@ ...@@ -29,8 +29,7 @@
}, },
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-order-picker", "homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-order-picker",
"dependencies": { "dependencies": {
"koishi-thirdeye": "^11.0.7", "koishi-thirdeye": "^11.0.9"
"moment": "^2.29.1"
}, },
"devDependencies": { "devDependencies": {
"@koishijs/plugin-console": "^4.1.1", "@koishijs/plugin-console": "^4.1.1",
...@@ -57,7 +56,7 @@ ...@@ -57,7 +56,7 @@
"ws": "^8.4.0" "ws": "^8.4.0"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.8.3", "koishi": "^4.8.4",
"koishi-plugin-cache-aragami": "^2.1.0" "koishi-plugin-cache-aragami": "^2.1.0"
}, },
"jest": { "jest": {
......
...@@ -48,9 +48,6 @@ export class OrderPickerConfig { ...@@ -48,9 +48,6 @@ export class OrderPickerConfig {
} }
return true; return true;
} }
@DefineSchema({ description: 'CD 时间', default: 60 })
cooldown: number;
} }
export type OrderPickerConfigLike = Partial<OrderPickerConfig>; export type OrderPickerConfigLike = Partial<OrderPickerConfig>;
// import 'source-map-support/register'; // import 'source-map-support/register';
import { Context, Session, Next, segment } from 'koishi'; import { Session, Next, segment } from 'koishi';
import { OrderPickerConfig, OrderPickerConfigLike } from './config'; import { OrderPickerConfig } from './config';
import { import {
DefinePlugin, DefinePlugin,
InjectConfig,
Inject, Inject,
UseMiddleware, UseMiddleware,
OnGuild, OnGuild,
OnPlatform, OnPlatform,
LifecycleEvents, UseCommand,
StarterPlugin,
Isolate,
UsingService,
PutSelfId,
UsePlugin,
PluginDef,
OnPrivate,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import moment from 'moment';
export * from './config'; export * from './config';
import AragamiPlugin, { CacheKey } from 'koishi-plugin-cache-aragami'; import AragamiPlugin, { CacheKey } from 'koishi-plugin-cache-aragami';
export class PickedOrderInfo {
sourceGuildId: string;
@CacheKey()
sourceUserId: string;
sourceUserName: string;
description: string;
time: number;
}
export class SelfOnline { export class SelfOnline {
@CacheKey() @CacheKey()
selfId: string; selfId: string;
...@@ -30,128 +26,91 @@ export class SelfOnline { ...@@ -30,128 +26,91 @@ export class SelfOnline {
online: boolean; online: boolean;
} }
@OnPlatform('onebot') const base = StarterPlugin(OrderPickerConfig);
@DefinePlugin({ name: 'order-picker', schema: OrderPickerConfig })
export default class OrderPicker implements LifecycleEvents {
constructor(private ctx: Context, config: OrderPickerConfigLike) {}
@InjectConfig()
private config: OrderPickerConfig;
@DefinePlugin()
class Utility extends base {
@Inject(true) @Inject(true)
private aragami: AragamiPlugin; private aragami: AragamiPlugin;
onApply() { async isOnline(selfId: string) {
const cmd = this.ctx const online = await this.aragami.get(SelfOnline, selfId);
.private(this.config.masterId) return !!online?.online;
.command('order-picker', '抢单管理');
cmd
.subcommand('.status', '抢单状态')
.usage('获取抢单状态')
.action(async ({ session }) => {
const online = await this.isOnline(session.selfId);
const orderInfo = await this.getLastInfo(session.selfId);
const firstLine = `抢单状态:${online ? '上班' : '下班'}`;
let secondLine = '';
if (orderInfo) {
const time = moment.unix(orderInfo.time);
const toTime = time.clone().add(this.config.cooldown, 'minutes');
secondLine = `来自群:${orderInfo.sourceGuildId}\n发布人:${
orderInfo.sourceUserName
}(${orderInfo.sourceUserId})\n描述:${
orderInfo.description
}\n时间:${time.format(
'YYYY-MM-DD HH:mm:ss',
)}\n结束时间:${toTime.format('YYYY-MM-DD HH:mm:ss')}`;
} else {
secondLine = '最近没有抢单';
}
return `${firstLine}\n${secondLine}`;
});
cmd
.subcommand('.on', '上班')
.usage('开启抢单')
.action(async ({ session }) => {
const online = await this.isOnline(session.selfId);
if (online) {
return '已经在上班了。';
}
await this.setOnline(session.selfId, true);
return '上班成功。';
});
cmd
.subcommand('.off', '下单')
.usage('关闭抢单')
.action(async ({ session }) => {
const online = await this.isOnline(session.selfId);
if (!online) {
return '还没有上班。';
}
await this.setOnline(session.selfId, false);
return '下班成功。';
});
cmd
.subcommand('.clear', '清除当前单')
.usage('强制清除当前单')
.action(async ({ session }) => {
const orderInfo = await this.getLastInfo(session.selfId);
if (!orderInfo) {
return '当前没有单。';
}
await this.removeLastInfo(session.selfId);
return '清除成功。';
});
} }
private async getLastInfo(selfId: string) { async setOnline(selfId: string, online: boolean) {
return this.aragami.get(PickedOrderInfo, selfId); await this.aragami.set(SelfOnline, { selfId, online });
} }
}
@DefinePlugin()
class ControlPanel extends base {
@Inject(true)
private uitlity: Utility;
@UseCommand('order', '抢单管理', { empty: true })
orderCommand() {}
private async setLastInfo(selfId: string, info: PickedOrderInfo) { private async switch(selfId: string, online: boolean, desc: string) {
return this.aragami.set(PickedOrderInfo, info, { const currentStatus = await this.uitlity.isOnline(selfId);
ttl: this.config.cooldown * 60 * 1000, if (currentStatus === online) {
}); return `我已经在${desc}了。`;
}
await this.uitlity.setOnline(selfId, online);
return `${desc}成功。`;
} }
private async removeLastInfo(selfId: string) { @UseCommand('order.on', '开启接单')
return this.aragami.del(PickedOrderInfo, selfId); async switchOn(@PutSelfId() id: string) {
return this.switch(id, true, '上班');
} }
private async isOnline(selfId: string) { @UseCommand('order.off', '关闭接单')
const online = await this.aragami.get(SelfOnline, selfId); async switchOff(@PutSelfId() id: string) {
return !!online?.online; return this.switch(id, false, '下班');
} }
private async setOnline(selfId: string, online: boolean) { @UseCommand('order.status', '查看上班状态')
await this.aragami.set(SelfOnline, { selfId, online }); async status(@PutSelfId() id: string) {
const online = await this.uitlity.isOnline(id);
return `我${online ? '上班' : '下班'}了。`;
}
}
@UsingService('aragami')
@Isolate('panel', 'receiver', 'utility')
@OnPlatform('onebot')
@DefinePlugin()
export default class OrderPicker extends StarterPlugin(OrderPickerConfig) {
@Inject()
private utility: Utility;
@UsePlugin()
loadUtility() {
return PluginDef(Utility, this.config);
}
@OnPrivate('{{masterId}}')
@UsePlugin()
loadControlPanel() {
return PluginDef(ControlPanel, this.config);
} }
@OnGuild() @OnGuild()
@UseMiddleware() @UseMiddleware()
async detectOrder(session: Session, next: Next) { async detectOrder(session: Session, next: Next) {
if (!this.config.acceptMessage(session)) return next(); if (!this.config.acceptMessage(session)) return next();
if (!(await this.isOnline(session.selfId))) return next(); if (!(await this.utility.isOnline(session.selfId))) return next();
if (await this.getLastInfo(session.selfId)) return next();
const description = segment const description = segment
.parse(session.content) .parse(session.content)
.filter((s) => s.type === 'text') .filter((s) => s.type === 'text')
.map((s) => s.data.content) .map((s) => s.data.content)
.join(''); .join('');
await this.setLastInfo(session.selfId, {
description,
time: moment().unix(),
sourceGuildId: session.guildId,
sourceUserId: session.userId,
sourceUserName:
session.author?.nickname ||
session.author?.username ||
session.username ||
session.userId,
});
await session.send(this.config.pickWord);
await session.bot.sendPrivateMessage( await session.bot.sendPrivateMessage(
this.config.masterId, this.config.masterId,
`接到单:\n${description}`, `接到${session.guildId} 的由 ${session.userId} 发布的单: ${description}`,
); );
return; await this.utility.setOnline(session.selfId, false);
return this.config.pickWord;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment