This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.d.ts
208 lines (196 loc) · 4.8 KB
/
main.d.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { Client, Collection, Message, PermissionResolvable } from "discord.js";
export type CommandMap = Collection<string, Command>;
export type Prefix = string | string[];
export type PrefixResolverFunc = <T>(ctx: Context<T>) => Promise<string> | string;
export type CheckFunc = <T>(ctx: Context<T>) => Promise<boolean> | boolean;
export type FailCallback = <T>(ctx: Context<T>, err: ParserError) => void;
export type EffectCallback = <T, K>(ctx: Context<T>) => (returnValue: K) => void;
export type CtxCallback = <T>(ctx: Context<T>) => void;
export interface HandlerOptions<T extends {}> {
/**
* The prefix or set of prefixes your bot
* should always be respond to
*
*/
prefix?: Prefix;
/**
* Dynamic prefix resolver. Although promises
* are allowed, you should probably be returning
* synchronous values from your cache as it is
* called on every command
*/
prefixResolver?: PrefixResolverFunc;
/**
* Whether or not mentioning your bot
* should trigger commands in addition
* to your prefixes
*/
mentionPrefix?: boolean;
/**
* The directory of folder containing
* your commands
*/
commandsDirectory: string;
/**
* The function to be running before and after
* every command execution. For command-specific
* effects, use the `useEffect` callback on
* commands instead
*
* The post function will be passed in
* whatever the `run` function returned
*
* @example
* ```ts
* useEffect: () => {
* console.log("this is running before the command")
* return () => {
* console.log("this is running after the command")
* }
* }
* ```
*
* @see The React's `useEffect`
* @param callback
*/
useEffect?: (callback: EffectCallback) => void;
/**
* Additional objects to be intersected onto the `ctx`
* callback of functions for ease of use
*/
ctx?: T;
/**
* Whether or not `.ts` files are checked instead of `.js`
*
* This is useful if you're on typescript and using ts-node
* instead of compiling to javascript
*/
checkTsFiles?: boolean;
}
export interface CommandHandler {
/**
* The collection of all registered commands
*/
commands: CommandMap;
}
export type Context<T> = {
message: Message;
client: Client;
commandHandler: CommandHandler
} & T;
export interface Check {
/**
* The condition of the check
*/
check: CheckFunc;
/**
* Called when the command condition fails
*/
onFail: FailCallback;
}
export interface Argument {
/**
* The type of the command that determines
* how the command will be parsed
*/
type: ArgType;
/**
* The name the argument will appear under
* in the run function
*/
name: string;
/**
* Whether or not the argument is optional
* optional arguments cannot be followed by
* non-optional arguments
*/
optional?: boolean;
/**
* Whether or not this argument is allowed to
* be repeated multiple times
*
* Repeat args must be the last argument in the command (for now)
*
* @example
* ```
* !ban @whamer @panda @jake
* ```
*/
repeat?: boolean;
/**
* The series of checks the command should
* be run against. Checks always short circuit
* commands
*/
checks?: Check[];
/**
* Called when an argument is not inputted
* properly
*/
onFail?: FailCallback;
/**
* Called when an argument is missing
*/
onMissing?: CtxCallback;
}
export interface CommandPermission {
needs: PermissionResolvable;
onMissing: <T>(ctx: Context<T>) => void;
}
export interface ArgObject {
[key: string]: any;
}
export interface Command {
/**
* name or name + aliases of the command
*/
name: string | string[];
/**
* Arguments that the command will expect to run
*/
args?: Argument[];
/**
* Permissions a user requires to be able to execute
* the command
*/
userPermissions?: CommandPermission | PermissionResolvable;
/**
* Permissions the bot needs to have to be able
* to execute the command
*/
clientPermissions?: CommandPermission | PermissionResolvable;
/**
* The execution of the command
* @param ctx
* @param args
*/
run: <T>(ctx: Context<T>, args: ArgObject) => void;
}
export interface ParserOptions {
prefix: Prefix;
mentionPrefix: boolean;
commands?: CommandMap;
}
export const enum ParserError {
MISSING_ARG = "missing_arg"
}
export const enum ArgType {
MEMBER_MENTION = "member_mention",
MEMBER = "member",
CHANNEL = "channel",
ROLE = "role",
MEMBER_NAME = "member_name",
CHANNEL_NAME = "channel_name",
ROLE_NAME = "role_name",
BOOLEAN = "boolean",
PREFIX = "prefix",
COMMAND = "command",
CHANNEL_MENTION = "channel_mention",
ROLE_MENTION = "role_mention",
NUMBER = "number",
STRING = "string",
FLAG = "flag",
WORD = "word",
QUOTED_STRING = "quoted_string",
TEXT = "text",
}