This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiscordbot.ts
executable file
·136 lines (119 loc) · 3.67 KB
/
discordbot.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
import { teeWrite } from './internal/logger';
import { ClientMessageHandler } from './internal/messages';
//* Discord.js Bot - by ringoXD -
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '1';
require('colors');
import { Client, GatewayIntentBits, ActivityType } from 'discord.js';
import fs from 'fs/promises';
import path from 'path';
import { token, syslogChannel } from './config.json';
process.env['FFMPEG_PATH'] = path.join(__dirname, 'ffmpeg');
//!Load Internal dir code
import { onShutdown } from './internal/schedules';
import activity from './internal/activity';
import mongodb from './internal/mongodb';
mongodb.connectMongoose();
import { LANG, strFormat } from './util/languages';
import { CommandManager } from './internal/commands';
import assert from 'assert';
import { Feature } from './util/types';
const creset = '\x1b[0m';
const cgreen = '\x1b[32m';
//!LOGGER
teeWrite(process.stdout, 'discordbot.log');
teeWrite(process.stderr, 'discordbot.log');
//!RUN=======================
console.log(LANG.discordbot.main.botStarting);
const options = {
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
],
// ws: { properties: { $browser: "Discord iOS" }}
};
console.log(
cgreen +
strFormat(LANG.discordbot.main.commandsLoaded, [
CommandManager.default.size,
]) +
creset,
);
const client = new Client(options);
console.log(LANG.discordbot.main.setupActivityCalling);
activity.setupActivity(client);
let messageHandler: ClientMessageHandler | undefined;
const featuresLoadPromise = fs
.readdir(path.join(__dirname, 'packages'))
.then((files) =>
Promise.all(
files.map(async (file) => {
console.log(`loading ${file} feature`);
const module = await import(file);
const feature: Feature = module.feature;
if (feature == null) {
throw new TypeError(`${file} feature is undefined`);
}
await feature.onLoad?.(client);
return feature;
}),
),
);
client.on('ready', async (readyClient) => {
const features = await featuresLoadPromise;
await Promise.all(
features.map((feature) => feature.onClientReady?.(readyClient)),
);
console.log(
strFormat(LANG.discordbot.ready.loggedIn, {
cgreen,
creset,
tag: client.user.tag,
}),
);
client.user.setPresence({
activities: [
{
name: LANG.discordbot.ready.presenceNameLoading,
state: LANG.discordbot.ready.presenceStateLoading,
type: ActivityType.Playing,
},
],
status: 'dnd',
});
console.log(LANG.discordbot.ready.commandsRegistering);
await CommandManager.default.setClient(readyClient);
console.log(
strFormat(LANG.discordbot.ready.readyAndTime, {
ready: cgreen + LANG.discordbot.ready.commandsReady + creset,
time: Math.round(performance.now()) + ' ms',
}),
);
const SyslogChannel = client.channels.cache.get(syslogChannel);
assert(SyslogChannel.isTextBased());
SyslogChannel.send(LANG.discordbot.ready.sysLog);
messageHandler = new ClientMessageHandler(readyClient);
});
onShutdown(async () => {
const SyslogChannel = client.channels.cache.get(syslogChannel);
assert(SyslogChannel.isTextBased());
await SyslogChannel.send(LANG.discordbot.shutdown.sysLog);
const features = await featuresLoadPromise;
await Promise.all(features.map((feature) => feature.onUnload?.()));
await Promise.all([
client
.destroy()
.then(() =>
console.log(cgreen + LANG.discordbot.shutdown.loggedOut + creset),
),
mongodb.connection.close(),
]);
});
client.login(token);
client.on('messageCreate', (message) => messageHandler?.handleMessage(message));
//!EVENTS
process.on('uncaughtException', function (err) {
console.error(err);
});