-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
184 lines (162 loc) · 7.23 KB
/
app.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
/*
- Welcome! This is a Discord bot used to connect Minecraft chat to
Discord and vice versa. Open source, with love from xMdb. ❤
- This is mainly for the Hypixel Knights Discord server,
but you can also easily adapt the code to work in your own server,
or use it in your own project.
- Read more about this bot in the README.md!
! WARNING
| This application will login to Hypixel using Mineflayer which is not a
| normal Minecraft client, this could result in your Minecraft account
| getting banned from Hypixel, so use this application at your own risk.
| I am not liable for any damages and no warranty is provided as outlined
| in GPL-3.0 License. */
// ██████ Integrations █████████████████████████████████████████████████████████
require('dotenv').config();
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const chalk = require('chalk');
const { Client, Intents, Collection, Util, WebhookClient } = require('discord.js');
const bot = new Client({
allowedMentions: { parse: ['users', 'roles'], repliedUser: true },
intents: [Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS],
});
const mineflayer = require('mineflayer');
const config = require('./config');
const regex = require('./func/regex');
async function toDiscordChat(msg) {
return bot.guilds.cache.get(config.ids.server).channels.cache.get(config.ids.guildChannel).send({
content: msg,
});
}
// ██████ Discord Bot: Events Init ██████████████████████████████████████████
const discordEvents = fs.readdirSync('./events/discord').filter((file) => file.endsWith('.js'));
for (const file of discordEvents) {
const event = require(`./events/discord/${file}`);
if (event.runOnce) {
bot.once(event.name, (...args) => event.execute(...args));
} else {
bot.on(event.name, (...args) => event.execute(...args));
}
}
// ██████ Minecraft Bot: Initialization ████████████████████████████████████████
function spawnBot() {
const minebot = mineflayer.createBot({
username: process.env.MC_USER,
password: process.env.MC_PASS,
host: 'hypixel.net',
version: '1.16.4',
auth: 'microsoft',
defaultChatPatterns: false,
checkTimeoutInterval: 30000,
interval: 5000,
});
module.exports = { minebot, toDiscordChat, bot };
// ██████ Discord Bot: Command Init ██████████████████████████████████████████
bot.commands = new Collection();
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}`);
bot.commands.set(command.name, {
name: command.name,
category: folder,
description: command.description,
execute: command.execute,
});
}
}
// ██████ Minecraft Bot: Handler ██████████████████████████████████████████████
rl.on('line', async (input) => {
minebot.chat(input);
});
const chatEvents = fs.readdirSync('./events/minecraft/chat').filter((file) => file.endsWith('.js'));
const handleEvents = fs.readdirSync('./events/minecraft/handler').filter((file) => file.endsWith('.js'));
for (const file of chatEvents) {
const event = require(`./events/minecraft/chat/${file}`);
minebot.on(event.name, (...args) => event.execute(...args));
minebot.chatAddPattern(regex[event.name], `${event.name}`);
}
for (const file of handleEvents) {
const event = require(`./events/minecraft/handler/${file}`);
minebot.on(event.name, (...args) => event.execute(...args));
}
// —— Bot reconnection message
setTimeout(() => {
minebot.chat('/g online');
}, 10000);
// ██████ Discord -> Minecraft ███████████████████████████████████████████████
bot.on('messageCreate', async (message) => {
try {
if (
message.author.id === bot.user.id ||
message.channel.id !== config.ids.guildChannel ||
message.author.bot ||
message.content.startsWith(config.bot.prefix) ||
!message.content.trim().length
) {
return;
}
const newCleanMessage = message.content.replace(/\r?\n|\r/g, ' ');
minebot.chat(`/gc ${message.author.username} > ${newCleanMessage}`);
await toDiscordChat(
`${config.emotes.fromDiscord} **${message.author.username}: ${Util.escapeMarkdown(message.content)}**`
);
await message.delete();
} catch (err) {
console.error(err);
await message.channel.send({
content: `**:warning: ${message.author}, there was an error while performing that task.**`,
});
}
if (message.content.startsWith(`/`)) {
await toDiscordChat(`https://media.tenor.com/images/e6cd56fc29e429ff89fef2fd2bdfaae2/tenor.gif`);
}
});
}
if (process.env.ENVIRONMENT === 'prod') {
setTimeout(() => {
spawnBot();
}, 5000);
}
if (process.env.ENVIRONMENT === 'dev') {
setTimeout(() => {
console.log(
chalk.yellowBright(
'This is where the bot should login to Hypixel.\nYou are seeing this because you have the environment value in .env set to dev.'
)
);
}, 5000);
}
if (process.env.ENVIRONMENT === undefined) {
throw new Error(
'Please set the ENVIRONMENT value in your .env file to either prod or dev to enable/disable the Minecraft bot.'
);
}
// ██████ Discord Bot: Command Handler █████████████████████████████████████████
bot.on('interactionCreate', async (interaction) => {
// —— Run command
if (!bot.commands.has(interaction.commandName)) return;
if (!interaction.isCommand() && !interaction.isContextMenu()) return;
try {
await bot.commands.get(interaction.commandName).execute(interaction, bot);
} catch (err) {
const webhook = new WebhookClient({
url: process.env.ERROR_WEBHOOK,
});
console.error(err);
await interaction.reply({
content: config.messages.errorDev,
ephemeral: true,
});
await webhook.send(`**General command error:** \`\`\`${err}\`\`\``);
}
});
process.on('uncaughtException', (err) => console.error(err)).on('unhandledRejection', (err) => console.error(err));
// —— Login the bot
bot.login(process.env.BOT_TOKEN);