Initial Commit

This commit is contained in:
illyum 2024-07-15 14:24:50 -06:00
commit 32ef7ac0be
5 changed files with 1400 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Ignore Node Modules
node_modules/

61
eventTypes.js Normal file
View File

@ -0,0 +1,61 @@
function cleanFormatting(rawString) {
const formattingRegex = /§[0-9a-fklmnor]+|[\[\]:]+/g;
return rawString.replace(formattingRegex, '');
}
class GuildMessage {
constructor(message, sender) {
this.message = message;
this.sender = sender;
}
getContent() {
return this.message;
}
getSender() {
return this.sender;
}
}
class GuildMessageSender {
constructor(rawUsername, rawHypixelRank, rawGuildRank) {
this.rawUsername = rawUsername;
this.rawHypixelRank = rawHypixelRank;
this.rawGuildRank = rawGuildRank;
}
getCleanUsername() {
return cleanFormatting(this.rawUsername);
}
getCleanHypixelRank() {
return cleanFormatting(this.rawHypixelRank);
}
getCleanGuildRank() {
return cleanFormatting(this.rawGuildRank);
}
getRawUsername() {
return this.rawUsername;
}
getRawHypixelRank() {
return this.rawHypixelRank;
}
getRawGuildRank() {
return this.rawGuildRank;
}
}
class OfficerMessage extends GuildMessage {
constructor(message, sender) {
super(message, sender);
}
}
// Instead of using export, we assign the classes to module.exports
module.exports = { GuildMessage, GuildMessageSender, OfficerMessage };

145
main.js Normal file
View File

@ -0,0 +1,145 @@
const mineflayer = require('mineflayer');
const { EventEmitter } = require('events');
const { EmbedBuilder, hyperlink, WebhookClient } = require('discord.js');
const { GuildMessage, GuildMessageSender, OfficerMessage } = require('./eventTypes.js');
const bot = mineflayer.createBot({
host: 'play.hypixel.net',
username: 'proudcirclemc@gmail.com',
auth: 'microsoft',
version: '1.8.9'
});
const webhookUrl = ';https://discord.com/api/webhooks/1262482329671176232/yAHgxkP2JSBPpdjmz6mdABu5yRkXxatXcbzI91iAjy0jM0z1kRzE1HZqzK5OGXCmkqPk'
const webhookClient = new WebhookClient({ url: webhookUrl });
function cleanFormatting(rawString) {
const formattingRegex = /§[0-9a-fklmnor]+|[\[\]:]+/g;
return rawString.replace(formattingRegex, '');
}
function parseGuildMessage(jsonChatMessage) {
const extras = jsonChatMessage.json.extra;
let extra_prefix = extras[0].text;
if (extra_prefix.startsWith('§2Guild >') || extra_prefix.startsWith('§aO > ')) {
// Guild Chat Event
let hypixel_rank = null;
let username = null;
let guild_rank = null;
let splitter = extra_prefix.trim().split(' ');
if (splitter.length === 4) {
username = splitter[2];
guild_rank = splitter[3];
} else if (splitter.length === 5) {
hypixel_rank = splitter[2];
username = splitter[3];
guild_rank = splitter[4];
} else {
console.error('Something went wrong: ' + splitter);
return;
}
let message = extras[1].text;
const guildMessageSender = new GuildMessageSender(username, hypixel_rank, guild_rank);
if (extra_prefix.startsWith('§2Guild >')) {
let guildMessage = new GuildMessage(message, guildMessageSender);
chatEvents.emit('guildmsg', guildMessage);
} else if (extra_prefix.startsWith('§aO > ')) {
let officerMessage = new OfficerMessage(message, guildMessageSender);
chatEvents.emit('officermsg', officerMessage);
}
}
}
function parsePersonalMessage(message) {
let fromPortion = message.split(': ');
if (!(fromPortion[0].startsWith('From '))) {
return;
}
let fromSplit = fromPortion[0].split(' ');
if (fromSplit[fromSplit.length - 1] !== 'illyum') {
return;
}
let contents = fromPortion[1];
if (contents.startsWith('!joinparty')) {
bot.chat('/party accept illyum');
} else if (contents.startsWith('!say ')) {
bot.chat('/gc ' + contents.replace('!say ', ''));
} else if (contents.startsWith('!reply ')) {
bot.chat('/msg illyum ' + contents.replace('!reply ', ''));
}
}
bot.once('spawn', () => {
console.log('Bot Logged In!');
});
bot.on('message', async (jsonMsg, position) => {
try {
parseGuildMessage(jsonMsg);
} catch (err) {}
});
bot.on('messagestr', (message, position, jsonMsg) => {
try {
parsePersonalMessage(message);
} catch (err) {}
});
const chatEvents = new EventEmitter();
chatEvents.on('guildmsg', (guildMessage) => {
const guildRank = guildMessage.getSender().getCleanGuildRank();
const username = guildMessage.getSender().getCleanUsername();
const msg = guildMessage.getContent();
if (username === 'ProudCircle') {
return;
}
if (msg.startsWith('!')) {
if (guildMessage.getContent().startsWith('!joinparty')) {
bot.chat('/party accept ' + username);
} else if (guildMessage.getContent().startsWith('!say ')) {
bot.chat('/gc ' + guildMessage.getContent().replace('!say ', ''));
}
} else {
let content;
if (guildMessage.getSender().getCleanHypixelRank() === null) {
content = `[${username}] ${guildRank}: \`${msg}\``;
} else {
content = `[${guildMessage.getSender().getCleanHypixelRank()}] [${username}] ${guildRank}: \`${msg}\``;
}
const embed = new EmbedBuilder()
.setTitle(content)
.setColor(0x00AA00);
webhookClient.send({
embeds: [embed]
});
}
console.log(`Guild > [${guildRank}] [${username}]: ${guildMessage.getContent()}`);
if (guildRank === 'Staff') {
if (guildMessage.getContent().startsWith('!joinparty')) {
bot.chat('/party accept ' + username);
} else if (guildMessage.getContent().startsWith('!say ')) {
bot.chat('/gc ' + guildMessage.getContent().replace('!say ', ''));
}
}
});
chatEvents.on('officermsg', (guildMessage) => {
const guildRank = guildMessage.getSender().getCleanGuildRank();
const username = guildMessage.getSender().getCleanUsername();
const msg = guildMessage.getContent();
console.log(`Officer > [${username}]: ${guildMessage.getContent()}`);
if (guildMessage.getContent().startsWith('!joinparty')) {
bot.chat('/party accept ' + username);
} else if (guildMessage.getContent().startsWith('!say ')) {
bot.chat('/gc ' + guildMessage.getContent().replace('!say ', ''));
}
});

1185
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"dependencies": {
"discord.js": "^14.15.3",
"mineflayer": "^4.20.1"
}
}