Skip to content

Commit a8ca50f

Browse files
committed
quotes: feat: avoid sending same quotes twice
1 parent 8cb8ad7 commit a8ca50f

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

apps/quotes.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { Quotes } from '#gc';
88
import { Quote } from '#gc.model';
99

1010
export class QuotesPlugin extends Plugin {
11+
private recentQuoteIdsByGroup: { [groupId: number]: number[] } = {};
12+
private readonly RECENT_CACHE_SIZE = 5;
13+
1114
constructor() {
1215
super({
1316
name: '语录插件',
@@ -145,13 +148,30 @@ export class QuotesPlugin extends Plugin {
145148
if (!this.e.isGroup || !this.e.group_id) return;
146149

147150
const tags = this.getTags(this.e.msg, '语录');
148-
const quotes = (await Quotes.DbService.getInstance()).findQuotesByTags(this.e.group_id, tags);
151+
const allQuotes = (await Quotes.DbService.getInstance()).findQuotesByTags(this.e.group_id, tags);
149152

150-
if (quotes.length === 0) {
153+
if (allQuotes.length === 0) {
151154
return this.e.reply('没有找到相关语录');
152155
}
156+
157+
const recentIds = this.recentQuoteIdsByGroup[this.e.group_id] || [];
158+
let selectableQuotes = allQuotes.filter(q => !recentIds.includes(q.id));
159+
160+
// If all quotes are in the recent list, fall back to the full list.
161+
if (selectableQuotes.length === 0) {
162+
selectableQuotes = allQuotes;
163+
}
164+
153165
// Pick a random quote
154-
const quote = _.sample(quotes);
166+
const quote = _.sample(selectableQuotes);
167+
168+
// Update recent quotes cache
169+
let newRecentIds = [quote.id, ...recentIds];
170+
if (newRecentIds.length > this.RECENT_CACHE_SIZE) {
171+
newRecentIds = newRecentIds.slice(0, this.RECENT_CACHE_SIZE);
172+
}
173+
this.recentQuoteIdsByGroup[this.e.group_id] = newRecentIds;
174+
155175
try {
156176
const result = await this.e.reply(segment.image(`file://${Quotes.ImageStorage.path(quote.filename).replace(/\\/g, '/')}`));
157177

0 commit comments

Comments
 (0)