Commit ef418c37 authored by nanahira's avatar nanahira

total rework

parent 34b9b185
This diff is collapsed.
......@@ -29,8 +29,7 @@
},
"homepage": "https://code.mycard.moe/3rdeye/koishi-plugin-order-picker",
"dependencies": {
"koishi-thirdeye": "^11.0.7",
"moment": "^2.29.1"
"koishi-thirdeye": "^11.0.9"
},
"devDependencies": {
"@koishijs/plugin-console": "^4.1.1",
......@@ -57,7 +56,7 @@
"ws": "^8.4.0"
},
"peerDependencies": {
"koishi": "^4.8.3",
"koishi": "^4.8.4",
"koishi-plugin-cache-aragami": "^2.1.0"
},
"jest": {
......
......@@ -48,9 +48,6 @@ export class OrderPickerConfig {
}
return true;
}
@DefineSchema({ description: 'CD 时间', default: 60 })
cooldown: number;
}
export type OrderPickerConfigLike = Partial<OrderPickerConfig>;
// import 'source-map-support/register';
import { Context, Session, Next, segment } from 'koishi';
import { OrderPickerConfig, OrderPickerConfigLike } from './config';
import { Session, Next, segment } from 'koishi';
import { OrderPickerConfig } from './config';
import {
DefinePlugin,
InjectConfig,
Inject,
UseMiddleware,
OnGuild,
OnPlatform,
LifecycleEvents,
UseCommand,
StarterPlugin,
Isolate,
UsingService,
PutSelfId,
UsePlugin,
PluginDef,
OnPrivate,
} from 'koishi-thirdeye';
import moment from 'moment';
export * from './config';
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 {
@CacheKey()
selfId: string;
......@@ -30,128 +26,91 @@ export class SelfOnline {
online: boolean;
}
@OnPlatform('onebot')
@DefinePlugin({ name: 'order-picker', schema: OrderPickerConfig })
export default class OrderPicker implements LifecycleEvents {
constructor(private ctx: Context, config: OrderPickerConfigLike) {}
@InjectConfig()
private config: OrderPickerConfig;
const base = StarterPlugin(OrderPickerConfig);
@DefinePlugin()
class Utility extends base {
@Inject(true)
private aragami: AragamiPlugin;
onApply() {
const cmd = this.ctx
.private(this.config.masterId)
.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 '清除成功。';
});
async isOnline(selfId: string) {
const online = await this.aragami.get(SelfOnline, selfId);
return !!online?.online;
}
private async getLastInfo(selfId: string) {
return this.aragami.get(PickedOrderInfo, selfId);
async setOnline(selfId: string, online: boolean) {
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) {
return this.aragami.set(PickedOrderInfo, info, {
ttl: this.config.cooldown * 60 * 1000,
});
private async switch(selfId: string, online: boolean, desc: string) {
const currentStatus = await this.uitlity.isOnline(selfId);
if (currentStatus === online) {
return `我已经在${desc}了。`;
}
await this.uitlity.setOnline(selfId, online);
return `${desc}成功。`;
}
private async removeLastInfo(selfId: string) {
return this.aragami.del(PickedOrderInfo, selfId);
@UseCommand('order.on', '开启接单')
async switchOn(@PutSelfId() id: string) {
return this.switch(id, true, '上班');
}
private async isOnline(selfId: string) {
const online = await this.aragami.get(SelfOnline, selfId);
return !!online?.online;
@UseCommand('order.off', '关闭接单')
async switchOff(@PutSelfId() id: string) {
return this.switch(id, false, '下班');
}
private async setOnline(selfId: string, online: boolean) {
await this.aragami.set(SelfOnline, { selfId, online });
@UseCommand('order.status', '查看上班状态')
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()
@UseMiddleware()
async detectOrder(session: Session, next: Next) {
if (!this.config.acceptMessage(session)) return next();
if (!(await this.isOnline(session.selfId))) return next();
if (await this.getLastInfo(session.selfId)) return next();
if (!(await this.utility.isOnline(session.selfId))) return next();
const description = segment
.parse(session.content)
.filter((s) => s.type === 'text')
.map((s) => s.data.content)
.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(
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