This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
96 lines (90 loc) · 2.51 KB
/
index.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
import { Erisa, MiddlewareHandler } from 'erisa';
import * as c from 'colorette';
type DefaultListener =
| 'ready'
| 'error'
| 'warn'
| 'guildCreate'
| 'guildDelete';
export interface LoggerLevel {
tagText: string;
textFunc(text: string): string;
}
export interface Logger {
dispatch(level: string, ...msgs: any[]);
levels: { [x: string]: LoggerLevel };
}
declare module 'erisa' {
interface Erisa {
logger: Logger;
}
}
/**
* Register's a logger under Erisa, and optionally makes default listeners.
*
* @param erisa An Erisa instance to define extensions for..
* @param defaultListeners Whether or not to register a listener for some default events. If an array, it is an array of events to log, with the values being a `DefaultListener`.
*/
export default function logger(
// TODO: fix fuckery in main package so I don't have to typeof
erisa: Erisa,
defaultListeners: boolean | DefaultListener[] = true
) {
if (erisa.logger) return;
const _logger = (erisa.logger = {
dispatch(level: string, ...msgs: any[]) {
if (!this.levels[level]) console.log(...msgs);
else {
const lvl: LoggerLevel = this.levels[level];
console.log(lvl.tagText, ...msgs.map(m => lvl.textFunc(m)));
}
},
levels: {
error: {
tagText: c.bgRed('[ERROR]'),
textFunc: str => c.red(c.bold(str))
},
warn: {
tagText: c.black(c.bgYellow('[WARN]')),
textFunc: str => c.yellow(c.bold(str))
},
info: {
tagText: c.black(c.bgGreen('[INFO]')),
textFunc: str => c.green(c.bold(str))
}
} as { [x: string]: LoggerLevel }
});
if (!defaultListeners) return;
else
return ([
[
'ready',
({ erisa: { user } }) =>
_logger.dispatch('info', `Logged in as ${user.username}`)
],
[
'error',
(_, err, id) =>
_logger.dispatch('error', `Discord error for shard ${id}: ${err}`)
],
[
'warn',
(_, warn, id) =>
_logger.dispatch('warn', `Discord warning for shard ${id}: ${warn}`)
],
[
'guildCreate',
(_, guild) =>
_logger.dispatch('info', `Joined guild ${guild.name} (${guild.id})`)
],
[
'guildDelete',
(_, guild) =>
_logger.dispatch('info', `Left guild ${guild.name} (${guild.id})`)
]
] as [string, MiddlewareHandler][]).filter(
([event]) =>
defaultListeners === true ||
defaultListeners.includes(event as DefaultListener)
);
}