-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (85 loc) · 2.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {Webhook} from 'discord-microhook';
// #region Steps
import cleanup from './steps/conditional/cleanup.js';
import discover from './steps/discover.js';
import buildPayload from './steps/loop/build-payload.js';
import authorResolver from './plugins/author-resolver.js';
import handleMessage from './steps/loop/handle-message.js';
import fetchAll from './steps/fetch-all.js';
// #endregion
/**
* @typedef {Object} SequenceOptions
* @property {ConfigOptions} config
* @property {string} [mode='update']
* @property {string} [directory=process.cwd()]
* @property {boolean} [verbose=true]
* @property {string[]} [cache={}]
*/
/**
* @typedef {Object} ConfigOptions
* @property {{id:string,token:string}|string} [webhook]
* @property {string[]} files
* @property {string} [directory]
* @property {string} mode
* @property {boolean} [verbose]
* @property {string?} [threadID]
* @property {string?} [thread_name]
*/
/**
* @typedef {Object} SequenceContext
* @property {import("discord-microhook").Message[]} messages
* @property {Record<string, import("discord-microhook").Message>} history
* @property {import("discord-microhook").Webhook} webhook - Webhook instance (discord-microhook)
* @property {(msg:string,{force}:{force:boolean})=>void} log - Log function
*/
/** @typedef {SequenceOptions & SequenceContext} SequenceUnion */
/**
* @param {SequenceOptions} options
* @returns {Promise<SequenceUnion>}
*/
export async function sequence(options) {
/** @type {SequenceUnion} */
const context = {
...options,
messages: [],
log(message, {force = false} = {}) {
if (options.verbose || force) {
console.log(message);
}
},
webhook: new Webhook(options.config.webhook),
};
try {
await context.webhook.fetch();
} catch (error) {
context.log(`fail:sequence() Failed to fetch webhook: ${error}`, {force: true});
return;
}
if (context.cache.length > 0) {
context.history = await fetchAll(context);
} else {
context.log('warn:sequence() No messages to process', {force: true});
}
if (context.mode === 'replace') {
await cleanup(context);
}
const manifest = await discover(context);
let skippedFiles = 0;
for (const [index, filePath] of manifest.entries()) {
let payload = await buildPayload(context, filePath);
if (!payload) {
skippedFiles++;
context.log(`steps(build-payload): Skipping '${filePath}', content missing`);
continue; // Unknown file type catch
}
// Event hook: render (plugin)
payload = authorResolver(context, payload);
await handleMessage(context, payload, filePath, index - skippedFiles);
}
const remainingMessages = context.cache.slice(context.messages.length);
if (remainingMessages.length > 0) {
await cleanup(context, remainingMessages);
}
return context;
}
export default sequence;