-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
237 lines (213 loc) · 6.94 KB
/
core.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const fca = require("chatbox-fca-remake");
const fs = require("fs");
// INFO: Middlware Imports
const command_middleware = require("./middlewares/command");
class core {
constructor() {
this.__admin = [];
this.__commands = [];
this.__objs = { listenEvents: true };
this.__adminId = [];
this.__prefix = ":";
// TODO: Prevent the full storage of the server or to clean the dumps every server refresh
const directory = `${__dirname}/temp`;
if (fs.existsSync(directory)) {
fs.rm(directory, { recursive: true }, (e) => {});
}
setTimeout(() => {
fs.mkdirSync(directory);
}, 1000);
console.log("Core was initiated");
}
setOptions(opts) {
if (Object.keys(opts).length <= 0) {
opts = {
listenEvents: true,
selfListen: false,
};
}
this.__opts = opts;
}
setPrefix(pref) {
this.__prefix = pref;
}
addAdmin(id) {
if (Array.isArray(id)) {
id.forEach((v) => {
this.__adminId.push(v);
});
return;
}
this.__adminId.push(id);
}
addAdminCommand(command) {
/*
* TODO: To create a compilation of all admin commands.
* Just place the path of the directory where the commands
* can be shown, as well as the name, description and the
* regular expression that will follow the format of the
* command.
*/
if (typeof command === "string") {
command = JSON.parse(command);
}
if (Array.isArray(command)) {
command.forEach((v) => {
this.__admin.push(v);
});
return console.log(
`INFO [Admin Commands]: The commands added successfully`,
);
}
if (Object.keys(command).length <= 0)
return console.error(
`ERR [Admin Command]: The command ${command} is invalid`,
);
this.__admin.push(command);
}
addCommand(command) {
/*
* TODO: To create a compilation of all user's commands.
* Just to place the path of the directory where the commands
* can be shown, as well as the name description and the
* regular expression that will follow the format of the command.
*/
if (typeof command === "string") {
command = JSON.parse(command);
}
if (Array.isArray()) {
command.forEach((v) => {
this.__commands.push(v);
});
return console.log(
`INFO [User command]: The commands added successfully.`,
);
}
if (Object.keys(command).length <= 0)
return console.error(
`ERR [User command]: The command ${command} is invalid`,
);
this.__commands.push(command);
}
getAllCommands() {
// TODO: To create a function to fetch all the commands from admin
// and normal users.
return this.__commands;
}
getPrefix() {
// TODO: To create a function to get the prefix of the bot
return this.__prefix;
}
start(state) {
/*
* NOTE: This state consists of JSON data of any of these information
* Username or Email
* Password
*
* NOTE: But if any of these are not included, it is required to use the FBstate
* or what we call the cookie to logged in to the server.
*/
console.log("Welcome to BhieBot console side.");
try {
fca(state, async (error, api) => {
if (error) return console.error(`ERR [Login]: ${error.error}`);
console.log("Initiating settings");
api.setOptions(this.__opts);
if (!this.__commands) {
return console.error(`ERR [Comamnds]: No comamnds existed`);
}
let isListen = true;
console.log("Initiating Listener");
api.listen(async (error, event) => {
if (isListen) {
// TODO: To execute the log once and avoid loops
console.log("Listener is now executed.");
isListen = false;
}
if (error) {
console.error(`ERR: [Listener]: ${error.error}`);
// api.logout();
return;
}
if (event.body) {
/*
* NOTE: The purpose of this funciton is to check if there's a message existed
* or new message arrived. It is to prevent the spamming caused by the account.
*/
// TODO: A boolean that will identify if there's any command executed to prevent the flooding of commands.
let executed = false;
if (event.body.startsWith(this.__prefix)) {
// TODO: TO check the message if this is command or not
// If this starts with a prefix, the code below will automatically eliminate the prefix
event.body = event.body.substring(this.__prefix.length);
if (
this.__admin.length > 0 &&
this.__adminId.includes(event.senderID)
) {
// INFO: Admin commands
let c = 0;
const admin_command = () => {
if (!this.__admin[c].type) {
this.__admin[c].type = ["message"];
}
const a = require(`./admin/${this.__admin[c].script}`);
const b = command_middleware(a);
if (!b(api, event, this.__admin[c])) {
if (c < this.__admin.length - 1) {
c++;
if (!executed) {
admin_command();
}
}
} else {
executed = true;
}
};
if (!executed) {
admin_command();
}
}
if (this.__commands.length <= 0 && !executed) {
return api.sendMessage(
"There's no registed command here",
event.threadID,
(e, m) => {},
);
}
let c = 0;
const _command = () => {
// INFO: Normal user's commands
if (!this.__commands[c].type) {
this.__commands[c].type = ["message"];
}
const a = require(`./users/${this.__commands[c].script}`);
const b = command_middleware(a);
if (!b(api, event, this.__commands[c])) {
if (c < this.__commands.length - 1) {
c++;
if (!executed) {
_command();
}
}
} else {
executed = true;
}
};
if (!executed) {
_command();
}
if (!executed) {
// NOTE: If the text doesn't matched with any of the commands, the ai will be the endpoint.
const ai = require("./auto/ai");
ai(api, event);
}
}
}
});
});
} catch (e) {
console.error(`Login [ERR]: ${e.message}`);
}
}
}
module.exports = core;