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
/
discordbot.ts
executable file
·137 lines (123 loc) · 3.3 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
137
import {
Logger,
Config,
Schedules,
Activities,
CommandManager,
Feature,
LANG,
strFormat,
} from 'core';
//* Discord.js Bot - by ringoXD -
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '1';
import 'colors';
import { Client, GatewayIntentBits, ActivityType } from 'discord.js';
import fs from 'fs/promises';
import path from 'path';
const { token, syslogChannel } = Config;
process.env['FFMPEG_PATH'] = path.join(__dirname, 'ffmpeg');
import assert from 'assert';
const creset = '\x1b[0m';
const cgreen = '\x1b[32m';
//!LOGGER
Logger.teeWrite(process.stdout, 'discordbot.log');
Logger.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" }}
};
const client = new Client(options);
console.log(LANG.discordbot.main.setupActivityCalling);
Activities.setupActivity(client);
const featuresLoadPromise = fs
.readdir(path.join(__dirname, 'packages'))
.then((files) =>
Promise.all(
files.map(async (file) => {
const module = await import(file);
const feature: Feature = module.feature;
if (feature == null) {
console.log(module);
throw new TypeError(`${file} feature is undefined`);
}
return feature;
}),
),
)
.then((features) => features.filter((feature) => feature.enabled))
.then((features) =>
Promise.all(
features.map(async (feature) => {
await feature.load(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: readyClient.user.tag,
}),
);
readyClient.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(
cgreen +
strFormat(LANG.discordbot.main.commandsLoaded, [
CommandManager.default.size,
]) +
creset,
);
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);
});
Schedules.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.unload()));
await Promise.all([
client
.destroy()
.then(() =>
console.log(cgreen + LANG.discordbot.shutdown.loggedOut + creset),
),
]);
});
client.login(token);
//!EVENTS
process.on('uncaughtException', function (err) {
console.error(err);
});