-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
232 lines (204 loc) · 7.7 KB
/
bot.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
import { Client } from "@meower-media/meower";
// import { exec } from "child_process";
import { delay } from "./libs/delay.js"
// import * as dotenv from "dotenv";
import JSONdb from "simple-json-db";
import * as logs from "./libs/logs.js";
import fs from 'fs'
// import { events, activeEvents } from "./libs/events.js";
import { scan as scanCommands } from "./commands/commandManager.js";
import * as welcome from "./libs/start/welcome_message.js";
function getunix() {
return (Math.floor(Date.now() / 1000))
}
function checkFiles() {
function checkFile(file, content) {
if (!fs.existsSync(file)) {
fs.writeFileSync(file, content)
logs.log(`! File ${file} not found`)
}
}
function checkFolder(folder) {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder, { recursive: true })
logs.log(`! Folder ${folder} not found`)
}
}
checkFolder("stores")
checkFolder("config")
checkFolder("logs")
checkFolder("db")
checkFile("stores/ulist.txt", "")
checkFile("stores/lastStartup.txt", "")
// checkFile("logs.txt", "")
checkFile("db/db.json", "{}")
checkFile("db/invites.json", "{}")
checkFile("db/messages.json", "{}")
checkFile("db/shop.json", "{}")
checkFile("config/config.json", "{}")
}
// Check if files like logs.txt exist
checkFiles()
export let username
export let password
export let admins
export let server
export let uptime
export let db
export let shop
export let invites
export const bot = new Client("wss://api.meower.org/v0/cloudli", "https://api.meower.org/")
export const config = JSON.parse(fs.readFileSync("config/config.json"))
export const auth = JSON.parse(fs.readFileSync("config/auth.json"))
export let update
export async function runBot() {
const commands = await scanCommands()
const adminCommands = await scanCommands("./commands/admin", "admin/")
username = auth.bot.username
password = auth.bot.password
admins = config.bot.admins;
server = config.urls.server
uptime = new Date().getTime();
db = new JSONdb("./db/db.json");
shop = new JSONdb("./db/shop.json");
invites = new JSONdb("./commands/gcInvite/invites.json")
// Define bot stuff
bot.version = config.bot.update.version
bot.update = config.bot.update
update = config.bot.update
let prefix
// Create log files if some other code needs them (*cough* the dashboard)
logs.logMessage("---")
logs.log("---")
// const modules = []
// config.modules.forEach(async m => {
// try {
// let im = await import(`./modules/${m}.js`)
// console.log(im)
// modules.push(im.default)
// console.log(`Attempting to run module "${m}"`)
// im.default.run(config, auth)
// } catch(e) {
// console.log(`Error occurred while loading module "${m}"`, e)
// }
// })
// // UwU event mixin
// if (activeEvents.includes("uwu")) {
// bot.post = events.uwu.post
// }
// Actual bot code yippee
function doLogin(i) {
if (i > 20) return;
try {
if(server) bot.server = server
console.log(`: Trying to log in {${i}}`)
bot.login(username, password)
} catch (error) {
doLogin(i + 1)
}
}
bot.onPost((user, message, origin) => {
function respond(text, origin) {
if (text.split().length < 4000) {
bot.post(text, origin)
return;
}
bot.post(text.slice(3996) + "...")
}
const isAdmin = admins.includes(user)
// log post
logs.log(`${user}: ${message} (in ${origin ? origin : "home"})`)
logs.logMessage(JSON.stringify({
user: user,
message: message,
origin: origin
}))
if (user == "Server") return
if (!message) return
// Parse command
let splitMessage = message.split(" ")
let messagePrefix = message.split(" ")[0]
if (messagePrefix != (prefix) && messagePrefix != `${prefix}#0000`) return;
let command = (splitMessage[1])
if (command) command = command.toLowerCase()
let args = message.split(" ")
args = args.splice(2)
logs.log(`${command} | ${args} | ${message.split(" ")}`)
if (command == null) {
command = "mention"
}
message = message.split(" ")
if (!config.settings.consoleInput) console.log(`${user} is using the command ${command}`)
logs.log(`${user} used the command ${command}`)
if (db.get(`${user}-ban-end`) >= getunix()) {
let cooldown_left = db.get(`${user}-ban-end`) - getunix()
bot.post(`You are banned for ${db.get(`${user}-ban-reason`)} for ${cooldown_left} more second${cooldown_left = 1 ? "s" : ""}`, origin)
return;
}
// const commandParams = { user, message, origin, command, args, bot }
function checkForCommand(commandName) {
let command = null
// console.log("Checking for commands", Object.keys(allCommands).length)
for (const key in commands) {
if (Object.hasOwnProperty.call(commands, key)) {
const element = commands[key];
// console.log(`${commandName} == ${element.command}`)
if (commandName == element.command || element.aliases.includes(commandName)) {
command = element
break
}
}
}
let foundAdmin = false
for (const key in adminCommands) {
if (Object.hasOwnProperty.call(adminCommands, key)) {
const element = adminCommands[key];
// console.log(`${commandName} == ${element.command}`)
if (commandName == element.command || element.aliases.includes(commandName)) {
command = element
foundAdmin = true
break
}
}
}
if (foundAdmin && !isAdmin) return bot.post(`Command "${commandName}" is admin-only!`, origin)
if (!command) return bot.post(`Command "${commandName}" not found!`, origin)
console.log(`Found command that matches ${commandName} (${command.command})`)
command.func(
{ user, message, origin, commandName, args, bot }
)
}
checkForCommand(command)
});
bot.onPacket((messageData) => {
if (messageData.cmd == "pmsg") {
bot.send_packet({ cmd: "pmsg", val: "I:100 | Bot", id: messageData.origin })
}
if (config.settings.logCL == "true") { logs.log(`[CL] ${messageData} (${config.settings.logCL})`); }
let JSONdata = JSON.parse(messageData)
switch (JSONdata["cmd"]) {
case ("ulist"):
fs.writeFileSync("stores/ulist.txt", JSON.stringify(JSONdata["val"].split(";").slice(0, -1)))
case ("statuscode"):
if (JSONdata["val"].includes("E") && config.settings.logCL == "true") {
logs.log(`! ${JSONdata["val"]}`)
}
}
});
bot.onClose(() => {
console.log('bot is ded');
// rl.close()
logs.log(`: Bot died`)
welcome.setReloaded(true)
delay(1500);
doLogin(0)
});
bot.onLogin(/*async*/ () => {
prefix = "@" + bot.user.username
welcome.welcome()
// let a = await bot.post("helo this is a test")
// console.log(a)
logs.log(`: Logged on as user ${username} [${bot.user.username}]`)
});
doLogin(0)
}