-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (128 loc) · 5.23 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
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
const mineflayer = require("mineflayer");
const pathfinder = require("mineflayer-pathfinder").pathfinder;
const collectBlock = require("mineflayer-collectblock").plugin;
const armorManager = require("mineflayer-armor-manager");
const pvp = require("mineflayer-pvp").plugin;
const bloodhoundPlugin = require("mineflayer-bloodhound")(mineflayer);
const mineflayerViewer = require('prismarine-viewer').mineflayer
const tool = require("mineflayer-tool").plugin;
const discord = require("discord.js");
const fs = require('fs');
try { //Scripting starts here
var usingDiscord;
var {botUsername, botPassword, server, serverPort, discordToken, discordPrefix} = require("./config.json");
if(!serverPort) serverPort = 25565;
const options = {
username : botUsername,
password : botPassword,
host : server,
port : serverPort
}
if(!discordToken) usingDiscord = false;
else usingDiscord = true;//If we don't have a token for the bot to login to, we don't use discord at all.
var bot = startBot(options);
var client = startDiscord();
client = loadCommands(client);
bot = loadEvents(client, bot);
if(usingDiscord) {
client.login(discordToken);
}
if(usingDiscord) { //If we're using discord, listen to the channel if not listen to the minecraft chat.
client.on("message", message => {
if (!message.content.startsWith(discordPrefix) || message.author.bot) return; //If its a message without our prefix or is a message sent by the bot, ignore.
var funcRet = commandHandler(usingDiscord, message, client, bot);
if(funcRet) message.channel.send(funcRet);
})
} else if (!usingDiscord) {
bot.on("chat", (username, message) => {
if(!message.includes(bot.username) || username == bot.username) return; //If the message either doesn't contain bot's name or is a message sent by the bot, ignore.
var funcRet = commandHandler(usingDiscord, message, client, bot);
if(funcRet) bot.chat(funcRet);
})
}
bot.once("spawn", () => {mineflayerViewer(bot, { port: 3007, firstPerson: false });});
bot.on("physicsTick", () => {
client.commands.get("attack").attack();
client.commands.get("defend").defend();
})
} catch (err) {
console.log(err);
}
//Functions down here.
function commandHandler(usingDiscord, message, client, bot) {
var args;
var commandName;
if(usingDiscord) { //If we are using discord to command the bot.
args = message.content.slice(discordPrefix.length).trim().split(/ +/);
commandName = args.shift().toLowerCase();
if(!client.commands.has(commandName)) {
message.channel.send("No such command");
return;
}
var funcRet = executeCommand(commandName, args, client, bot);
if(funcRet) return funcRet;
} else { //If we are using the minecraft chat instead.
args = message.slice(bot.username.length).trim().split(/ +/);
commandName = args.shift().toLowerCase();
console.log(args);
console.log(commandName);
if(!client.commands.has(commandName)) {
bot.chat("No such command");
return;
}
var funcRet = executeCommand(commandName, args, client, bot);
if(funcRet) return funcRet;
}
}
function startDiscord() { //If we are using discord, log in the bot, if not just initialize the command collection
const client = new discord.Client();
client.commands = new discord.Collection();
return client;
}
function startBot(options) {
const bot = mineflayer.createBot(options);
bot.loadPlugin(pathfinder);
bot.loadPlugin(collectBlock);
bot.loadPlugin(armorManager);
bot.loadPlugin(pvp);
bot.loadPlugin(tool);
bloodhoundPlugin(bot);
bot.bloodhound.yaw_correlation_enabled = true;
return bot;
}
function loadCommands(client) {
const commandFolders = fs.readdirSync("./commands");
for(const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith(".js"));
for(const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
return client;
}
function loadEvents(client, bot) {
const mcEvents = fs.readdirSync("./events");
for(const folder of mcEvents) {
const eventFiles = fs.readdirSync(`./events/${folder}`).filter(file => file.endsWith(".js"));
for(const file of eventFiles) {
const event = require(`./events/${folder}/${file}`);
if(event.once) {
bot.once(event.name, (...args) => event.start(...args, client, bot));
} else {
bot.on(event.name, (...args) => event.start(...args, client, bot));
}
}
}
return bot;
}
function executeCommand(commandName, args, client, bot) {
const command = client.commands.get(commandName);
if(command.args) { //If the command takes in arguments
var funcRet = command.start(args, client, bot);
if(funcRet) return funcRet;
} else {
var funcRet = command.start(client, bot);
if(funcRet) return funcRet;
}
}