-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
249 lines (201 loc) · 8.27 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { MessageContent, GuildMessages, Guilds, DirectMessages } = GatewayIntentBits;
const mineflayer = require('mineflayer');
const token = 'TOKEN';
const logChannelId = 'LOG_CHANNEL';
const client = new Client({
intents: [Guilds, GuildMessages, MessageContent, DirectMessages],
partials: [Partials.Channel]
});
client.login(token);
const userSessions = {};
client.once('ready', () => {
console.log(`Discord bot logged in as ${client.user.tag}`);
registerCommands();
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const { commandName, options, user } = interaction;
const userId = user.id;
switch (commandName) {
case 'settings':
handleSettingsCommand(interaction, userId, options);
break;
case 'connect':
await handleConnectCommand(interaction, userId);
break;
case 'disconnect':
await handleDisconnectCommand(interaction, userId);
break;
case 'setcommand':
await handleSetCommand(interaction, userId, options);
break;
case 'setdelay':
await handleSetDelayCommand(interaction, userId, options);
break;
case 'help':
await handleHelpCommand(interaction);
break;
}
});
function handleSettingsCommand(interaction, userId, options) {
const host = options.getString('host');
const port = options.getInteger('port');
const username = options.getString('username');
userSessions[userId] = {
host,
port,
username,
connect: false,
delay: 5000
};
interaction.reply(`Saved connection settings. IP: \`${host}:${port}\`, username: \`${username}\``);
}
async function handleConnectCommand(interaction, userId) {
const session = userSessions[userId];
if (!session || !session.host || !session.port || !session.username) {
await interaction.reply('You need to set up the settings first with the `/settings` command.');
return;
}
await interaction.reply(`Trying to connect to the server...`);
session.connect = true;
session.bot = createBot(session, userId);
}
async function handleDisconnectCommand(interaction, userId) {
const session = userSessions[userId];
if (session && session.bot) {
session.connect = false;
session.bot.end();
delete session.bot;
await interaction.reply('Disconnected from the server.');
} else {
await interaction.reply('I was not connected to the server.');
}
}
async function handleSetCommand(interaction, userId, options) {
const command = options.getString('command');
if (!userSessions[userId]) {
userSessions[userId] = {};
}
userSessions[userId].commandOnConnect = command;
await interaction.reply({ content: `I will now execute the command \`${command}\` after each connection to the server.`, ephemeral: true });
}
async function handleSetDelayCommand(interaction, userId, options) {
const delay = options.getInteger('delay');
if (!userSessions[userId]) {
userSessions[userId] = {};
}
userSessions[userId].delay = delay * 1000;
await interaction.reply({ content: `I will now wait ${delay} seconds before reconnecting to the server.`});
}
async function handleHelpCommand(interaction) {
const helpMessage = `
**Hello!**
I can stand in for you on the server while you handle your tasks.
Before you start, set up the server connection settings:
\`/settings <host> <port> <username>\`
Please note that I only connect to servers **1.18 to 1.20.4 with cracked versions**.
To connect, send \`/connect\`, and to disconnect, send \`/disconnect\`.
To log in, **I can execute a command** when I enter the server. You can set it up with this command:
\`/setcommand <command>\`.
I won't show this command to anyone, so feel free to **enter your password there**.
If the server goes for a restart and I get kicked, I will **try to reconnect after 5 seconds**. If you want to set your own delay, use:
\`/setdelay <delay_in_seconds>\`.
Everything you set up is **only for you**.
`;
await interaction.reply({ content: helpMessage});
}
function createBot(session, userId) {
const bot = mineflayer.createBot({
host: session.host,
port: session.port,
username: session.username,
auth: 'offline'
});
let errorHandled = false;
bot.on('spawn', async () => {
console.log(`Mineflayer bot logged in as ${bot.username}`);
const logChannel = await client.channels.fetch(logChannelId);
if (logChannel && logChannel.isTextBased()) {
await logChannel.send(`Connected to the server \`${session.host}:${session.port}\` as \`${session.username}\`.`);
}
if (session.commandOnConnect) {
bot.chat(session.commandOnConnect);
}
});
bot.on('end', async () => {
if (!errorHandled) {
errorHandled = true;
await handleBotDisconnection(userId, session);
}
});
bot.on('error', async error => {
if (!errorHandled) {
errorHandled = true;
await handleBotError(userId, session, error);
}
});
return bot;
}
async function handleBotDisconnection(userId, session) {
console.log(`Bot disconnected from ${session.host}:${session.port}`);
if (userSessions[userId] && userSessions[userId].connect === true) {
const logChannel = await client.channels.fetch(logChannelId);
if (logChannel && logChannel.isTextBased()) {
await logChannel.send(`<@${userId}>, I was disconnected from the server \`${session.host}:${session.port}\`. I will try to reconnect in ${session.delay / 1000} seconds.`);
}
setTimeout(() => {
if (userSessions[userId].connect) {
userSessions[userId].bot = createBot(session, userId, {
reply: async (message) => {
if (logChannel && logChannel.isTextBased()) {
await logChannel.send(message);
}
}
});
}
}, session.delay);
}
}
async function handleBotError(userId, session, error) {
console.error('Bot error:', error);
if (userSessions[userId] && userSessions[userId].connect === true) {
const logChannel = await client.channels.fetch(logChannelId);
if (logChannel && logChannel.isTextBased()) {
await logChannel.send(`<@${userId}>, I could not connect to \`${session.host}:${session.port}\`. Ending session, send \`/connect\` to try again.`);
}
userSessions[userId].connect = false;
if (session && session.bot) {
session.bot.removeAllListeners();
session.bot.end();
delete session.bot;
}
}
}
function registerCommands() {
const commands = [
{
name: 'settings',
description: 'Set connection settings',
options: [
{ name: 'host', description: 'Server host', type: 3, required: true },
{ name: 'port', description: 'Server port', type: 4, required: true },
{ name: 'username', description: 'Username', type: 3, required: true }
]
},
{ name: 'connect', description: 'Connect to the server' },
{ name: 'disconnect', description: 'Disconnect from the server' },
{
name: 'setcommand',
description: 'Set command to execute upon connection',
options: [{ name: 'command', description: 'Command to execute', type: 3, required: true }]
},
{
name: 'setdelay',
description: 'Set delay before reconnecting',
options: [{ name: 'delay', description: 'Delay in seconds', type: 4, required: true }]
},
{ name: 'help', description: 'Get command help' }
];
client.application.commands.set(commands);
}