/
PreconditionConditionOr.ts
73 lines (63 loc) · 2.41 KB
/
PreconditionConditionOr.ts
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
import { Result } from '@sapphire/result';
import type { PreconditionContainerResult } from '../IPreconditionContainer';
import type { IPreconditionCondition } from './IPreconditionCondition';
/**
* An {@link IPreconditionCondition} which runs all containers similarly to doing (V0 || V1 [|| V2 [|| V3 ...]]).
* @since 1.0.0
*/
export const PreconditionConditionOr: IPreconditionCondition = {
async messageSequential(message, command, entries, context) {
let error: PreconditionContainerResult | null = null;
for (const child of entries) {
const result = await child.messageRun(message, command, context);
if (result.isOk()) return result;
error = result;
}
return error ?? Result.ok();
},
async messageParallel(message, command, entries, context) {
const results = await Promise.all(entries.map((entry) => entry.messageRun(message, command, context)));
let error: PreconditionContainerResult | null = null;
for (const result of results) {
if (result.isOk()) return result;
error = result;
}
return error ?? Result.ok();
},
async chatInputSequential(interaction, command, entries, context) {
let error: PreconditionContainerResult | null = null;
for (const child of entries) {
const result = await child.chatInputRun(interaction, command, context);
if (result.isOk()) return result;
error = result;
}
return error ?? Result.ok();
},
async chatInputParallel(interaction, command, entries, context) {
const results = await Promise.all(entries.map((entry) => entry.chatInputRun(interaction, command, context)));
let error: PreconditionContainerResult | null = null;
for (const result of results) {
if (result.isOk()) return result;
error = result;
}
return error ?? Result.ok();
},
async contextMenuSequential(interaction, command, entries, context) {
let error: PreconditionContainerResult | null = null;
for (const child of entries) {
const result = await child.contextMenuRun(interaction, command, context);
if (result.isOk()) return result;
error = result;
}
return error ?? Result.ok();
},
async contextMenuParallel(interaction, command, entries, context) {
const results = await Promise.all(entries.map((entry) => entry.contextMenuRun(interaction, command, context)));
let error: PreconditionContainerResult | null = null;
for (const result of results) {
if (result.isOk()) return result;
error = result;
}
return error ?? Result.ok();
}
};