| 12 | |
| 13 | |
| 14 | class AutoDelPlugin extends Plugin { |
| 15 | cleanup(): void { |
| 16 | if (this.db) { |
| 17 | try { |
| 18 | this.db.close(); |
| 19 | } catch {} |
| 20 | } |
| 21 | this.db = null; |
| 22 | this.settings.clear(); |
| 23 | this.lifecycle = null; |
| 24 | } |
| 25 | |
| 26 | description: string = `🕒 <b>定时自动删除消息</b><br/><br/> |
| 27 | <b>命令</b><br/> |
| 28 | • <code>${mainPrefix}autodel [时间] [global]</code> 设置自动删除<br/> |
| 29 | • <code>${mainPrefix}autodel l</code> 查看当前设置<br/> |
| 30 | • <code>${mainPrefix}autodel cancel [global]</code> 取消设置<br/><br/> |
| 31 | <b>时间格式</b><br/> |
| 32 | • <code>30 seconds</code>、<code>5 minutes</code>、<code>2 hours</code>、<code>1 days</code><br/> |
| 33 | • 简写:<code>30s</code>、<code>5m</code>、<code>2h</code>、<code>1d</code><br/> |
| 34 | • 中文:<code>30秒</code>、<code>5分</code>/<code>5分钟</code>、<code>2小时</code>/<code>2时</code>、<code>1天</code><br/><br/> |
| 35 | <b>示例</b><br/> |
| 36 | • <code>${mainPrefix}autodel 30s</code><br/> |
| 37 | • <code>${mainPrefix}autodel 5 分钟 global</code>(设置全局)<br/><br/> |
| 38 | <b>⚠️ 安全说明</b><br/> |
| 39 | • 只会删除您自己发送的消息<br/> |
| 40 | • 最小删除时间为5秒`; |
| 41 | private db: Database.Database | null = null; |
| 42 | private settings: Map<string, number> = new Map(); |
| 43 | private lifecycle: GenerationContext | null = null; |
| 44 | |
| 45 | async setup(context: { lifecycle: GenerationContext }): Promise<void> { |
| 46 | this.lifecycle = context.lifecycle; |
| 47 | // Singleton plugin: the constructor's initDatabase() only runs once, but |
| 48 | // cleanup() nulls this.db and clears this.settings on every reload. Without |
| 49 | // re-initializing here, autodel silently stops working after .reload (db is |
| 50 | // null, settings empty) until a full process restart. Re-init on every load. |
| 51 | if (!this.db) { |
| 52 | await this.initDatabase(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | cmdHandlers: Record<string, (msg: Api.Message) => Promise<void>> = { |
| 57 | autodel: this.handleAutoDel.bind(this) |
| 58 | }; |
| 59 | |
| 60 | constructor() { |
| 61 | super(); |
| 62 | this.initDatabase(); |
| 63 | } |
| 64 | |
| 65 | private async initDatabase(): Promise<void> { |
| 66 | try { |
| 67 | const assetsDir = await createDirectoryInAssets("autodel"); |
| 68 | const dbPath = path.join(assetsDir, "autodel.db"); |
| 69 | |
| 70 | this.db = new Database(dbPath); |
| 71 | |