Commit 85fe2f56 authored by nanahira's avatar nanahira

trace last message

parent 938f4dae
// import 'source-map-support/register';
import { DefineSchema, RegisterSchema } from 'koishi-thirdeye';
import { MaybeArray, Schema, segment, Session } from 'koishi';
import { segment, Session } from 'koishi';
@RegisterSchema()
export class OrderPickerConfig {
......@@ -25,23 +25,23 @@ export class OrderPickerConfig {
@DefineSchema({ description: '抢单的文本。', default: '我要' })
pickWord: string;
isMatchingTarget(session: Session) {
return (
session.content &&
session.guildId &&
this.targetGuilds.includes(session.guildId.toString())
);
}
acceptMessage(session: Session) {
if (!session.content) {
if (!session.userId || !this.publishers.includes(session.userId)) {
return false;
}
if (
!(
session.guildId &&
this.targetGuilds.includes(session.guildId.toString()) &&
session.userId &&
this.publishers.includes(session.userId)
)
)
return false;
if (
this.blacklistMatchers.some((matcher) => session.content.match(matcher))
)
) {
return false;
}
const segments = segment.parse(session.content);
if (!segments.find((s) => s.type === 'at' && s.data.type === 'all')) {
return false;
......
......@@ -18,7 +18,7 @@ import {
Provide,
} from 'koishi-thirdeye';
export * from './config';
import AragamiPlugin, { CacheKey } from 'koishi-plugin-cache-aragami';
import AragamiPlugin, { CacheKey, CacheTTL } from 'koishi-plugin-cache-aragami';
export class SelfOnline {
@CacheKey()
......@@ -27,6 +27,21 @@ export class SelfOnline {
online: boolean;
}
@CacheTTL(60000)
export class LastMessage {
@CacheKey()
guildId: string;
userId: string;
content: string;
fromSession(session: Session) {
this.guildId = session.guildId;
this.userId = session.userId;
this.content = session.content;
return this;
}
}
const base = StarterPlugin(OrderPickerConfig);
@Provide('utility', { immediate: true })
......@@ -43,6 +58,21 @@ class Utility extends base {
async setOnline(selfId: string, online: boolean) {
await this.aragami.set(SelfOnline, { selfId, online });
}
async saveLastMessage(session: Session) {
const lastMessage = new LastMessage().fromSession(session);
return this.aragami.set(lastMessage);
}
async fetchLastMessage(session: Session) {
const lastMessage = await this.aragami.get(LastMessage, session.guildId);
if (lastMessage) {
await this.aragami.del(LastMessage, session.guildId);
if (lastMessage.userId === session.userId) {
return lastMessage.content;
}
}
}
}
@DefinePlugin()
......@@ -101,13 +131,24 @@ export default class OrderPicker extends base {
@OnGuild()
@UseMiddleware()
async detectOrder(session: Session, next: Next) {
if (!this.config.acceptMessage(session)) return next();
if (!this.config.isMatchingTarget(session)) return next();
if (!this.config.acceptMessage(session)) {
await this.utility.saveLastMessage(session);
return next();
}
const lastMessageContent = await this.utility.fetchLastMessage(session);
if (!(await this.utility.isOnline(session.selfId))) return next();
const description = segment
let description = segment
.parse(session.content)
.filter((s) => s.type === 'text')
.map((s) => s.data.content)
.join('');
.join('')
.trim();
if (!description) {
description = lastMessageContent || '';
}
await session.bot.sendPrivateMessage(
this.config.masterId,
`接到群 ${session.guildId} 的由 ${session.userId} 发布的单: ${description}`,
......
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