This repository was archived by the owner on Jun 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathST.js
More file actions
165 lines (142 loc) · 4.74 KB
/
Copy pathST.js
File metadata and controls
165 lines (142 loc) · 4.74 KB
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
159
160
161
162
163
164
165
const { Client, GatewayIntentBits, Partials, Collection } = require('discord.js');
const fs = require('fs');
const path = require('path');
const logger = require('./logger/logs');
const utils = require('./utils/utils');
const JSONProvider = require('./database/JSONProvider');
const MongoProvider = require('./database/MongoProvider');
const UsersData = require('./database/userdb');
const ThreadsData = require('./database/groupdb');
class GlobalContext {
constructor() {
this.config = null;
this.client = null;
this.ST = {
commands: new Collection(),
events: new Collection(),
onReply: new Map(),
onReaction: new Map(),
onChat: new Set(),
callbacks: new Map()
};
this.db = null;
this.usersData = null;
this.threadsData = null;
this.utils = utils;
this.logger = logger;
this.startTime = Date.now();
this.Callback = new Map();
}
async initialize() {
this.loadConfig();
await this.initializeDatabase();
this.createClient();
this.freezeGlobals();
}
loadConfig() {
try {
const configPath = path.join(__dirname, 'config.json');
const configData = fs.readFileSync(configPath, 'utf8');
this.config = JSON.parse(configData);
if (process.env.BOT_TOKEN) {
this.config.botToken = process.env.BOT_TOKEN;
}
if (process.env.BOT_CLIENT_ID) {
this.config.botClientId = process.env.BOT_CLIENT_ID;
}
logger.success('Configuration loaded');
} catch (error) {
logger.error('Failed to load config.json', error);
process.exit(1);
}
}
async initializeDatabase() {
try {
global.logger = logger;
if (this.config.databaseType === 'mongo') {
this.db = new MongoProvider();
} else {
this.db = new JSONProvider();
}
await this.db.init();
this.usersData = new UsersData(this.db);
this.threadsData = new ThreadsData(this.db);
logger.success('Database initialized');
} catch (error) {
logger.error('Failed to initialize database', error);
process.exit(1);
}
}
createClient() {
this.client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions
],
partials: [
Partials.Channel,
Partials.Message,
Partials.Reaction
]
});
logger.success('Discord client created');
}
freezeGlobals() {
global.config = this.config;
global.client = this.client;
global.ST = this.ST;
global.db = this.db;
global.usersData = this.usersData;
global.threadsData = this.threadsData;
global.utils = {
...this.utils.Utils,
STBotApis: this.utils.STBotApis,
getStreamFromURL: this.utils.Utils.getStreamFromURL.bind(this.utils.Utils),
downloadFile: this.utils.Utils.downloadFile.bind(this.utils.Utils),
getFileExtension: this.utils.Utils.getFileExtension.bind(this.utils.Utils),
getMimeType: this.utils.Utils.getMimeType.bind(this.utils.Utils),
isImage: this.utils.Utils.isImage.bind(this.utils.Utils),
isVideo: this.utils.Utils.isVideo.bind(this.utils.Utils),
isAudio: this.utils.Utils.isAudio.bind(this.utils.Utils),
formatBytes: this.utils.Utils.formatBytes.bind(this.utils.Utils),
randomInt: this.utils.Utils.randomInt.bind(this.utils.Utils),
sleep: this.utils.Utils.sleep.bind(this.utils.Utils),
getTime: this.utils.Utils.getTime.bind(this.utils.Utils),
getRandomElement: this.utils.Utils.getRandomElement.bind(this.utils.Utils)
};
global.logger = this.logger;
global.Callback = this.Callback;
Object.assign(global.ST, {
STBotApis: this.utils.stBotApisInstance
});
logger.success('Global context initialized and frozen');
}
getUptime() {
const uptime = Date.now() - this.startTime;
const seconds = Math.floor(uptime / 1000) % 60;
const minutes = Math.floor(uptime / (1000 * 60)) % 60;
const hours = Math.floor(uptime / (1000 * 60 * 60));
const days = Math.floor(uptime / (1000 * 60 * 60 * 24));
if (days > 0) return `${days}d ${hours}h ${minutes}m`;
if (hours > 0) return `${hours}h ${minutes}m ${seconds}s`;
return `${minutes}m ${seconds}s`;
}
}
const globalContext = new GlobalContext();
async function start() {
try {
await globalContext.initialize();
const login = require('./bot/login/login');
await login(globalContext.client);
} catch (error) {
logger.error('Fatal error during startup', error);
process.exit(1);
}
}
start();
module.exports = globalContext;