Skip to content

Commit ce32368

Browse files
committed
copypasta: fix: config loading
1 parent 530ee8b commit ce32368

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

apps/copypasta.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import PLUGIN_ID from '#gc.id';
44
import { Plugin, Logger, Config } from '#gc';
55
import { CopypastaRule, isCopypastaRule } from '#gc.model';
66

7-
export class CopypastaPlugin extends Plugin {
8-
rules: CopypastaRule[];
7+
let rules: CopypastaRule[] = null;
98

9+
export class CopypastaPlugin extends Plugin {
1010
constructor() {
1111
super({
1212
name: '复制粘贴插件',
@@ -15,27 +15,7 @@ export class CopypastaPlugin extends Plugin {
1515
priority: '98',
1616
rule: []
1717
});
18-
this.loadRules();
19-
}
20-
21-
loadRules() {
22-
const config = Config.get("copypasta");
23-
if (config.get("rules")) {
24-
const rules_raw = config.get("rules");
25-
if (_.isArray(rules_raw)) {
26-
if (rules_raw.every(isCopypastaRule)) {
27-
this.rules = rules_raw;
28-
Logger.info(`[${PLUGIN_ID}][copypasta] loaded ${this.rules.length} rules.`);
29-
} else {
30-
const rules_invalid = rules_raw.filter((r: any) => !isCopypastaRule(r));
31-
Logger.error(`[${PLUGIN_ID}][copypasta] invalid rule(s): ${rules_invalid}`);
32-
}
33-
} else {
34-
Logger.error(`[${PLUGIN_ID}][copypasta] rules must be an array: ${rules_raw}`);
35-
}
36-
} else {
37-
Logger.error(`[${PLUGIN_ID}][copypasta] no rules found in config!`);
38-
}
18+
if (!_.isArray(rules)) rules = getRulesFromConfig();
3919
}
4020

4121
async accept() {
@@ -44,7 +24,7 @@ export class CopypastaPlugin extends Plugin {
4424
if (!msg) return;
4525

4626
let msg_trim = msg.trim();
47-
const rule = this.rules.find(r => {
27+
const rule = rules.find(r => {
4828
if (r.trigger === msg_trim) {
4929
if (!group) return r.enable_pm;
5030
if (r.enabled_groups.length > 0 && !r.enabled_groups.includes(group)) return false;
@@ -60,3 +40,25 @@ export class CopypastaPlugin extends Plugin {
6040
}
6141
}
6242
}
43+
44+
function getRulesFromConfig(): CopypastaRule[] {
45+
let rules: CopypastaRule[] = [];
46+
const config = Config.get("copypasta");
47+
if (config.get("rules")) {
48+
const rules_raw = config.get("rules")?.toJSON();
49+
if (_.isArray(rules_raw)) {
50+
if (rules_raw.every(isCopypastaRule)) {
51+
this.rules = rules_raw;
52+
Logger.info(`[${PLUGIN_ID}][copypasta] loaded ${this.rules.length} rules.`);
53+
} else {
54+
const rules_invalid = rules_raw.filter((r: any) => !isCopypastaRule(r));
55+
Logger.error(`[${PLUGIN_ID}][copypasta] invalid rule(s): ${rules_invalid}`);
56+
}
57+
} else {
58+
Logger.error(`[${PLUGIN_ID}][copypasta] rules must be an array: ${rules_raw}`);
59+
}
60+
} else {
61+
Logger.error(`[${PLUGIN_ID}][copypasta] no rules found in config!`);
62+
}
63+
return rules;
64+
}

model/config_file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export default class ConfigFile {
3333
return this.document.toJSON();
3434
}
3535

36-
has(key: string) {
36+
has(key: string): boolean {
3737
return this.document.hasIn(key.split('.'));
3838
}
39-
get(key: string) {
39+
get(key: string): any {
4040
return this.document.getIn(key.split('.'));
4141
}
4242
set(key: string, value: any) {

0 commit comments

Comments
 (0)