This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.ts
158 lines (131 loc) · 5.57 KB
/
Client.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
import { Client, ClientOptions, Guild } from 'discord.js';
import { Command } from './structures/Command';
import { CommandPool } from './structures/Pools/CommandPool';
import { Event } from './structures/Event';
import { EventPool } from './structures/Pools/EventPool';
import { Monitor } from './structures/Monitor';
import { MonitorPool } from './structures/Pools/MonitorPool';
import { Task } from "./structures/Task";
import { TaskPool } from './structures/Pools/TaskPool';
import { Stopwatch } from './util';
import path from 'path';
import './extendables/MoonlightUser';
/**
* @external ClientOptions
* @see {@link https://discord.js.org/#/docs/main/stable/typedef/ClientOptions}
*/
/**
* @external Client
* @see {@link https://discord.js.org/#/docs/main/stable/class/Client}
*/
/**
* @typedef Map
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map}
*/
/**
* [[include:gettingStarted.md]]
* @license MIT
* @extends external:Client
*/
export class MoonlightClient extends Client {
// Directories
/** The user's directory */
public mainDir: string = path.dirname(require.main!.filename);
/** The core directory, where the Moonlight files are located */
public readonly coreDir: string = path.join(__dirname, '../')
// Pool
/** The command pool that stores all commands */
public readonly commands: CommandPool<string, Command> = new CommandPool(this);
/** A map which stores all command aliases */
public readonly aliases: Map<string, string> = new Map<string, string>();
/** The event pool that stores all events */
public readonly events: EventPool<string, Event> = new EventPool(this);
/** The monitor pool that stores all monitors */
public readonly monitors: MonitorPool<string, Monitor> = new MonitorPool(this);
/** The task pool that stores all tasks */
public readonly tasks: TaskPool<string, Task> = new TaskPool(this);
/** An array of owners */
public owners: string[] = new Array();
// Additional options
/** An array containing all the prefixes */
public readonly prefixes: string[] = new Array();
public options!: MoonlightClientOptions;
/** @param options The Moonlight Client Options */
constructor(options: MoonlightClientOptions = {}) {
super(options);
this.options.displayErrors = options.displayErrors ?? true;
this.options.useMentionPrefix = options.useMentionPrefix ?? true;
this.options.useUsernamePrefix = options.useUsernamePrefix ?? true;
this.options.useSweeper = options.useSweeper ?? true;
if (options?.prefix) {
if (Array.isArray(options.prefix)) this.prefixes.push(...options.prefix);
else this.prefixes.push(options.prefix);
}
this.on('error', error => {
if (this.options.displayErrors) console.error(`[Error] ${error}`);
});
if (options?.owners) this.owners.push(...options.owners);
}
/** Returns a Map containing every Moonlight pool */
get pools() {
return new Map<string, Command | Event | Monitor>([...this.commands, ...this.events, ...this.monitors]);
}
/**
* The function that initiates the bot and then logs the client in, establishing a websocket connection to Discord.
* @param token Token of the bot account to log in with
* @returns Token
* @example(client.login('token'))
*/
public async login(token?: string | undefined): Promise<string> {
const stopwatch = new Stopwatch();
// Initialize all the pools
await Promise.all([
this.events.init(),
this.commands.init(),
this.monitors.init(),
this.tasks.init()
]);
stopwatch.stop();
console.info(`Loaded everything in: ${stopwatch.getElapsedHuman}`);
this.events.forEach(event => {
if (event.disabled) return;
if (event.once) return this.once(event.event, (...arg) => event.run(...arg));
this.on(event.event, (...arg) => event.run(...arg));
});
// Finally login
return super.login(token);
}
}
/** @example { ..., prefix: ['p.', 'p!'], displayErrors: false, readyMessage: (client) => `Logged in as ${client.user.tag}` }, fetchGuildPrefix: guild => getPrefix(guild.id) */
export interface MoonlightClientOptions extends ClientOptions {
/** The prefix or an array of prefixes the bot will use */
prefix?: string | string[];
/** Whether or not to display error messages sent by the error event */
displayErrors?: boolean;
/** An array that contains the ID's of the owners */
owners?: string[];
/** Whether or not to use mention prefix */
useMentionPrefix?: boolean;
/** Whether or not to use username prefix - e.g. if the bot is called "Sensei" then the username prefix will be "Sensei, " */
useUsernamePrefix?: boolean;
/** Whether or not to use the built-in memory sweeper */
useSweeper?: boolean;
/**
* Set the ready message to display when the bot is ready -> should return a string
* @example readyMessage: (client) => `Logged in and serving in ${client.guilds.size}!`
*/
readyMessage?(client: MoonlightClient): string;
/**
* The function the framework will use to fetch a prefix for a guild
* @example fetchGuildPrefix: async (guild) => {
* const prefix = await fetchPrefix(guild.id);
* return prefix;
* }
*/
fetchGuildPrefix?(guild: Guild): string | Promise<string>;
};
declare module 'discord.js' {
interface ClientEvents {
moonlightReady: [];
}
}