This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
135 lines (123 loc) · 3.45 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
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
import config from 'config';
import { CronJob } from 'cron';
import { Client, FriendlyError } from "discord.js-commando";
import admin from "firebase-admin";
import moment from 'moment';
import 'moment/locale/zh-tw';
import path from "path";
import requireAll from "require-all";
import { FirestoreProvider } from "./firestore-provider";
import { initHttp } from "./http";
import { Logger } from "./logger";
import { getVisitor } from './ua';
import { isDevMode } from "./utils";
// init moment locale
moment.locale('zh-tw');
// process event handle
process
.on('warning', Logger.warn)
.on('unhandledRejection', (error) => {
if (error instanceof Error && error['code'] && error['details'] && error['metadata']) {
// grpc-js Error
return;
}
Logger.error('Unhandled Promise Rejection:', error);
})
.on('uncaughtException', async (error) => {
await Logger.error('Uncaught Exception:', error);
process.exit(1);
});
// init discord client
const client = new Client({
owner: config.get("botOwners"),
commandPrefix: config.get("prefix"),
commandEditableDuration: 0,
nonCommandEditable: false,
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
presence: {
status: 'dnd',
},
});
// client event handle
client
.on('error', Logger.error)
.on('warn', Logger.warn)
.on('debug', Logger.debug)
.on('ready', async () => {
Logger.log(`Ready! ${isDevMode() ? "DEBUG MODE" : ""}`);
if (client.user) {
client.user.setPresence({
status: 'online',
activity: {
name: config.get("activity"),
type: 'PLAYING',
},
});
}
})
.on('disconnect', () => { Logger.warn('Disconnected!'); })
.on('commandError', (cmd, err, message, args, fromPattern) => {
const fatal = !(err instanceof FriendlyError);
getVisitor(message.author).exception(`CommandError: ${err}`, fatal).send();
if (!fatal) return;
Logger.error(`Error in command ${cmd.groupID}:${cmd.memberName}`, err);
})
.on('commandRun', (command, promise, message, args, fromPattern) => {
getVisitor(message.author).event("command_run", {
name: command.name,
args,
}).send();
});
// init provider
const app = admin.initializeApp({
credential: admin.credential.cert(config.get('firebaseAdmin.cert')),
databaseURL: config.get('firebaseAdmin.databaseURL')
});
client.setProvider(new FirestoreProvider(app)).catch(Logger.error);
// init registry
client.registry
.registerDefaultTypes()
.registerDefaultGroups()
.registerDefaultCommands({
unknownCommand: false,
})
.registerGroups([
['sdorica', 'Sdorica commands'],
['fun', 'Fun commands'],
['quote', 'Quote bot commands'],
['config', 'Config commands'],
['owner', 'Owner commands'],
['moderator', 'Moderator commands'],
])
.registerTypesIn({
dirname: path.join(__dirname, 'types'),
filter: /^([^\.].*)(?<!\.ignore)\.ts$/,
})
.registerCommandsIn({
dirname: path.join(__dirname, 'commands'),
filter: /^([^\.].*)(?<!\.ignore)\.ts$/,
});
// set default command cooldown
client.registry.commands.forEach(command => {
if (command.throttling === null) {
command.throttling = {
usages: 1,
duration: config.get('defaultCooldown'),
};
}
});
// login to Discord
client.login(config.get("token"));
// init express server
initHttp(client);
// init cron
const jobs: Record<string, (client: Client) => CronJob> = requireAll({
dirname: path.join(__dirname, 'crons'),
filter: /^([^\.].*)(?<!\.ignore)\.cron\.ts$/,
resolve: function (module) {
return module.default;
},
});
Object.values(jobs).forEach(job => {
job(client).start();
});