-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (99 loc) · 5.03 KB
/
index.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
// Platform check
if (require("os").platform() == "win32") console.warn("Windows is not supported by Discord-Bash, use WSL for full experience.");
const Discord = require('discord.js');
const { cmdRegistry } = require('./utils/commandParser');
const interpreter = require("./utils/interpreter");
const fs = require('fs');
const client = new Discord.Client();
client.on("ready", () => {
console.log('Logged in');
client.user.setActivity('Mention me');
});
// Check for system settings file
if (!fs.existsSync('./settings.json')) {
fs.copyFileSync('./settings.json.example', './settings.json');
console.log('settings.json file generated, please paste your bot token into it.');
process.exit(0);
}
// Load system settings
const settings = require('./settings.json');
client.on('message', async message => {
if (message.author.bot) return;
if (message.content.startsWith(`${settings.prefix}forcelogout`) && message.author.collector) {
message.author.collector.stop();
return message.channel.send('Force logged out');
}
// Filesystem dir shortcuts
const { sep } = require('path');
message.dirs = {
users: `${__dirname}${sep}home`,
packages: `${__dirname}${sep}packages`,
home: `${__dirname}${sep}home${sep}${message.author.id}`,
bin: `${__dirname}${sep}bin`,
user: {
local: {
bin: `${__dirname}${sep}home${sep}${message.author.id}${sep}local${sep}bin`,
autorun: `${__dirname}${sep}home${sep}${message.author.id}${sep}local${sep}autorun`
}
}
};
// Mention the bot to start system
const mention = new RegExp(`^<@!?${client.user.id}>( |)$`);
if (!message.content.match(mention)) return;
if (message.author.collector) return message.channel.send(`You cannot login multiple times at the same time,\nyou are already loged in here -> ${message.author.collector.channel instanceof Discord.DMChannel ? client.user : message.author.collector.channel}\nclick to jump to the channel and continue the usage of your account\nor do \`${settings.prefix}forcelogout\` to force logout`);
// Initiate system variables
global.windowsDrives = /^([a-z]):/i;
// Filesystem check
if (!fs.existsSync(`${__dirname}${sep}sudoers.txt`)) await fs.writeFileSync(`${__dirname}${sep}sudoers.txt`, '[YOUR ID HERE]');
if (!fs.existsSync(message.dirs.users)) await fs.mkdirSync(message.dirs.users);
if (!fs.existsSync(message.dirs.packages)) await fs.mkdirSync(message.dirs.packages);
if (!fs.existsSync(message.dirs.home)) await fs.mkdirSync(message.dirs.home);
if (!fs.existsSync(message.dirs.bin)) await fs.mkdirSync(message.dirs.bin);
// Load system commands
const jsFiles = fs.readdirSync(message.dirs.bin).filter(file => file.endsWith('.js'));
for (const file of jsFiles) cmdRegistry.register(require(`${message.dirs.bin}${sep}${file}`));
// Load packages commands
const packagesDir = fs.readdirSync(message.dirs.packages);
for (const package of packagesDir) {
const packageFiles = fs.readdirSync(`${message.dirs.packages}${sep}${package}${sep}commands`).filter(file => file.endsWith('.js'));
for (const file of packageFiles) cmdRegistry.register(require(`${message.dirs.packages}${sep}${package}${sep}commands${sep}${file}`));
}
message.author.collector = message.channel.createMessageCollector(m => m.author.id == message.author.id);
await message.react('✅');
message.author.currentdirectory = message.dirs.home;
// Launch user autorun scripts in /home/{user.id}/local/autorun
if (fs.existsSync(message.dirs.user.local.autorun)) {
try {
await fs.readdirSync(message.dirs.user.local.autorun).forEach(file => {
interpreter.file(message, client, `${message.dirs.user.local.autorun}${sep}${file}`);
});
} catch (e) {
return console.log(e);
};
};
message.author.collector.on("collect", async collected => {
const split = collected.content.trim().split(/ +/g);
const cmd = split[0];
await interpreter.cmd(collected, client, collected.content.trim());
// Look for commands in /home/{user.id}/local/bin
if (fs.existsSync(message.dirs.user.local.bin)) {
let files = await fs.readdirSync(message.dirs.user.local.bin);
if (files.includes(cmd)) {
let command;
try {
command = `${message.dirs.user.local.bin}${sep}${cmd}`;
} catch (e) {
return console.log(e);
};
try {
interpreter.file(message, client, command);
} catch (e) {
return console.log(e);
}};
};
});
message.author.collector.on("end", () => {
delete message.author.collector;
});
});
client.login(settings.token);