diff --git a/joindata.json b/joindata.json new file mode 100644 index 00000000..e69de29b diff --git a/src/events/mineflayer/chat/guild-log-fetch.ts b/src/events/mineflayer/chat/guild-log-fetch.ts new file mode 100644 index 00000000..af639579 --- /dev/null +++ b/src/events/mineflayer/chat/guild-log-fetch.ts @@ -0,0 +1,12 @@ +export default { + name: 'chat:guildLog', + runOnce: false, + run: async (bot, ...fullMatches) => { + console.log(`[DEBUG] A guild log event has been triggered!`); + + const _fullMatch = fullMatches.filter(match => match !== undefined).join('\n'); + + console.log(`[DEBUG] Full Match: ${_fullMatch}`); + + } +} as Event; diff --git a/src/events/mineflayer/chat/member-join-leave.ts b/src/events/mineflayer/chat/member-join-leave.ts index 4ab2b5f0..6bb7e7f7 100644 --- a/src/events/mineflayer/chat/member-join-leave.ts +++ b/src/events/mineflayer/chat/member-join-leave.ts @@ -5,8 +5,8 @@ import isFetchError from '../../../requests/is-fetch-error'; import isUserBlacklisted from '../../../blacklist/is-user-blacklisted'; import { BotEvents } from 'mineflayer'; -import getRankColor from '@util/get-rank-color'; -import env from '@util/env'; +import getRankColor from '../../../util/get-rank-color'; +import env from '../../../util/env'; export default { name: 'chat:memberJoinLeave', @@ -33,7 +33,9 @@ export default { const mojangProfile = await fetchMojangProfile(playerName); if (isFetchError(mojangProfile)) { - bot.executeCommand(`/oc Failed to get Mojang profile of ${playerName}. Please try again later.`); + bot.executeCommand( + `/oc Failed to get Mojang profile of ${playerName}. Please try again later.` + ); return; } else { console.log(`[DEBUG] Found Mojang profile of ${playerName}. ID: ${mojangProfile.id}`); @@ -42,157 +44,203 @@ export default { if (type === 'joined') { // Join Date return new Promise((resolve, reject) => { - fetch(`https://api.hypixel.net/guild?key=${env.HYPIXEL_API_KEY}&name=${env.HYPIXEL_GUILD_NAME}`) - .then((response) => response.json()) - .then((data) => { - if (data.success === false) { - console.log(`[DEBUG] ${playerName} joined the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently joined member ${playerName}. Please try again later.`); - return reject('Guild data not found!'); - } else if (data.success === true && data.guild === null) { - console.log(`[DEBUG] ${playerName} joined the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently joined member ${playerName}. Please try again later.`); - return reject('Guild data not found!'); - }; - - if (isFetchError(mojangProfile)) { - console.log(`[DEBUG] ${playerName} joined the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently joined member ${playerName}. Please try again later. ()`); - return reject('Guild data not found!'); - } else { - - const guild = data.guild; - const member = guild.members.find((member: { uuid: string; }) => member.uuid === mojangProfile.id); - - if (!member) { - console.log(`[DEBUG] ${playerName} joined the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently joined member ${playerName}. Please try again later.`); - return reject('Guild data not found!'); - } - - const joinDatetoSend = new Date(member.joined).toLocaleString('en-US', { timeZone: 'Europe/Amsterdam' }); - const joinDate = new Date(member.joined); - - const fs = require('fs'); - const path = require('path'); - const filePath = path.resolve(__dirname, 'joindata.json'); - if (!fs.existsSync(filePath)) { - fs.writeFileSync(filePath, '{}'); - } - - const _joinData = require('./joindata.json'); - if (_joinData[mojangProfile.id]) { - delete _joinData[mojangProfile.id]; - _joinData[mojangProfile.id] = joinDate; - fs.writeFileSync(filePath, JSON.stringify(_joinData, null, 4)); - } else { - _joinData[mojangProfile.id] = joinDate; - fs.writeFileSync(filePath, JSON.stringify(_joinData, null, 4)); - console.log(`[DEBUG] ${playerName} joined the guild, wrote join data to file.`); - } - - bot.sendToDiscord( - 'oc', - `Player **${escapeMarkdown(playerName)}** joined the guild! Their join date is ||${joinDatetoSend}||. `, - ) - - // Second Part - - bot.executeCommand(`/g log ${playerName} 1`); - const logEntriesArray = [] as string[]; - let chatListener2: BotEvents["message"]; - bot.mineflayer.on( - "message", - (chatListener2 = async (message) => { - const messageContent = message.toString(); - let counter = 0; - const logEntries = messageContent.split("\n"); - - for (const logEntry of logEntries) { - const regex = new RegExp(/(([A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{4}) (([0-9]{2}):([0-9]{2})) ((EDT|EST))): ([A-Za-z-0-9-_]{2,27}) (joined|left|invited|kicked|muted|unmuted|set rank of|set MOTD|set guild tag|set guild tagcolor|set Discord|turned the chat throttle on|turned the chat throttle off)( ([A-Za-z-0-9-_]{2,27})?)?( for | to |: )?([ A-Za-z-0-9-!-_\\s]+)?/g + fetch( + `https://api.hypixel.net/guild?key=${env.HYPIXEL_API_KEY}&name=${env.HYPIXEL_GUILD_NAME}` + ) + .then((response) => response.json()) + .then((data) => { + if (data.success === false) { + console.log( + `[DEBUG] ${playerName} joined the guild, but failed to get guild data.` + ); + bot.executeCommand( + `/oc Failed to get guild data of recently joined member ${playerName}. Please try again later.` + ); + return reject('Guild data not found!'); + } else if (data.success === true && data.guild === null) { + console.log( + `[DEBUG] ${playerName} joined the guild, but failed to get guild data.` + ); + bot.executeCommand( + `/oc Failed to get guild data of recently joined member ${playerName}. Please try again later.` + ); + return reject('Guild data not found!'); + } + + if (isFetchError(mojangProfile)) { + console.log( + `[DEBUG] ${playerName} joined the guild, but failed to get guild data.` + ); + bot.executeCommand( + `/oc Failed to get guild data of recently joined member ${playerName}. Please try again later. ()` + ); + return reject('Guild data not found!'); + } else { + const guild = data.guild; + const member = guild.members.find( + (member: { uuid: string }) => member.uuid === mojangProfile.id + ); + + if (!member) { + console.log( + `[DEBUG] ${playerName} joined the guild, but failed to get guild data.` ); + bot.executeCommand( + `/oc Failed to get guild data of recently joined member ${playerName}. Please try again later.` + ); + return reject('Guild data not found!'); + } - let match; - - while ((match = regex.exec(logEntry)) !== null && counter <= 2) { - logEntriesArray.push(match[0]); - counter++; - - const [ - fullMatch, - date, - time, - hour, - minute, - timezone, - year, - primaryUsername, - action, - day, - secondUsername, - additionalInfo, - ] = match; - - if (counter === 2 && action === "invited") { - const invitedPlayer = secondUsername as string; - const userName = primaryUsername as string; - - bot.sendToDiscord( - 'oc', - `Player **${escapeMarkdown(userName)}** invited **${escapeMarkdown(invitedPlayer)}** to the guild!`, - ) - } else if (counter === 2) { - - - bot.sendToDiscord( - 'oc', - `Player **${escapeMarkdown(playerName)}** ${action} the guild! **They weren't invited by anyone.**`, - ) - } + const joinDatetoSend = new Date(member.joined).toLocaleString('en-US', { + timeZone: 'Europe/Amsterdam', + }); + const joinDate = new Date(member.joined); + + const fs = require('fs'); + const path = require('path'); + const filePath = path.resolve(__dirname, 'joindata.json'); + if (!fs.existsSync(filePath)) { + fs.writeFileSync(filePath, '{}'); } + + const _joinData = require('./joindata.json'); + if (_joinData[mojangProfile.id]) { + delete _joinData[mojangProfile.id]; + _joinData[mojangProfile.id] = joinDate; + fs.writeFileSync(filePath, JSON.stringify(_joinData, null, 4)); + } else { + _joinData[mojangProfile.id] = joinDate; + fs.writeFileSync(filePath, JSON.stringify(_joinData, null, 4)); + console.log( + `[DEBUG] ${playerName} joined the guild, wrote join data to file.` + ); } - })) - return resolve(); - } - }) - }); - } else if (type === 'left') { + + bot.executeCommand(`/g log ${playerName} 1`); + const logEntriesArray = [] as string[]; + let chatListener2: BotEvents['message']; + bot.mineflayer.on( + 'message', + (chatListener2 = async (message) => { + const messageContent = message.toString(); + let counter = 0; + const logEntries = messageContent.split('\n'); + + for (const logEntry of logEntries) { + if ( + logEntry.match( + /(([A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{4}) (([0-9]{2}):([0-9]{2})) ((EDT|EST))): ([A-Za-z-0-9-_]{2,27}) (joined|left|invited|kicked|muted|unmuted|set rank of|set MOTD|set guild tag|set guild tagcolor|set Discord|turned the chat throttle on|turned the chat throttle off)( ([A-Za-z-0-9-_]{2,27})?)?( for | to |: )?([ A-Za-z-0-9-!-_\\s]+)?/g + ) + ) { + logEntriesArray.push(logEntry); + counter++; + } + + if (logEntriesArray[1] && logEntriesArray[0]) { + if ( + logEntriesArray[1].includes('invited') && + counter <= 2 + ) { + bot.sendToDiscord( + 'oc', + `Player **${escapeMarkdown( + playerName + )}** joined the guild! Their join date is ||${joinDatetoSend}||.\n\n They were invited by **${ + logEntriesArray[1].split(' ')[5] + }**.` + ); + + bot.mineflayer.removeListener( + 'message', + chatListener2 + ); + break; + } else { + counter = 2; + bot.sendToDiscord( + 'oc', + `Player **${escapeMarkdown( + playerName + )}** joined the guild! Their join date is ||${joinDatetoSend}||.\n\n They weren't invited by anyone.` + ); + bot.mineflayer.removeListener( + 'message', + chatListener2 + ); + break; + } + } else { + console.log( + `[DEBUG] ${playerName} joined the guild, but failed to get invite data.` + ); + } + } + }) + ); + } + }); + }); + } else if (type === 'left') { return new Promise((resolve, reject) => { - fetch(`https://api.hypixel.net/guild?key=${env.HYPIXEL_API_KEY}&name=${env.HYPIXEL_GUILD_NAME}`) - .then((response) => response.json()) - .then((data) => { - if (data.success === false) { - console.log(`[DEBUG] ${playerName} left the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently left member ${playerName}. Please try again later.`); - return reject('Guild data not found!'); - } else if (data.success === true && data.guild === null) { - console.log(`[DEBUG] ${playerName} left the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently left member ${playerName}. Please try again later.`); - return reject('Guild data not found!'); - }; - - if (isFetchError(mojangProfile)) { - console.log(`[DEBUG] ${playerName} left the guild, but failed to get guild data.`); - bot.executeCommand(`/oc Failed to get guild data of recently left member ${playerName}. Please try again later. It's safe to assume they left the guild within 5 minutes.`); - return reject('Guild data not found!'); - } else { - - - const oldJoinData = require('./joindata.json'); - const joinDate = oldJoinData[mojangProfile.id]; - const leaveDate = new Date(); - - const timeDiff = Math.abs(new Date(leaveDate).getTime() - new Date(joinDate).getTime()); - const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); - const formattedLeaveDate = diffDays > 1 ? new Date(joinDate).toLocaleString('en-US', { timeZone: 'Europe/Amsterdam' }) : new Date(joinDate).toLocaleString('en-US', { timeZone: 'Europe/Amsterdam' }); - - bot.sendToDiscord( - 'oc', - `Player **${escapeMarkdown(playerName)}** left the guild! Their join date was ||${formattedLeaveDate}||. They stayed in the guild for **${diffDays}** days. `, - ) - return resolve(); - } - }) - })} - } + fetch( + `https://api.hypixel.net/guild?key=${env.HYPIXEL_API_KEY}&name=${env.HYPIXEL_GUILD_NAME}` + ) + .then((response) => response.json()) + .then((data) => { + if (data.success === false) { + console.log( + `[DEBUG] ${playerName} left the guild, but failed to get guild data.` + ); + bot.executeCommand( + `/oc Failed to get guild data of recently left member ${playerName}. Please try again later.` + ); + return reject('Guild data not found!'); + } else if (data.success === true && data.guild === null) { + console.log( + `[DEBUG] ${playerName} left the guild, but failed to get guild data.` + ); + bot.executeCommand( + `/oc Failed to get guild data of recently left member ${playerName}. Please try again later.` + ); + return reject('Guild data not found!'); + } + + if (isFetchError(mojangProfile)) { + console.log( + `[DEBUG] ${playerName} left the guild, but failed to get guild data.` + ); + bot.executeCommand( + `/oc Failed to get guild data of recently left member ${playerName}. Please try again later. It's safe to assume they left the guild within 5 minutes.` + ); + return reject('Guild data not found!'); + } else { + const oldJoinData = require('./joindata.json'); + const joinDate = oldJoinData[mojangProfile.id]; + const leaveDate = new Date(); + + const timeDiff = Math.abs( + new Date(leaveDate).getTime() - new Date(joinDate).getTime() + ); + const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); + const formattedLeaveDate = + diffDays > 1 + ? new Date(joinDate).toLocaleString('en-US', { + timeZone: 'Europe/Amsterdam', + }) + : new Date(joinDate).toLocaleString('en-US', { + timeZone: 'Europe/Amsterdam', + }); + + bot.sendToDiscord( + 'oc', + `Player **${escapeMarkdown( + playerName + )}** left the guild! Their join date was ||${formattedLeaveDate}||. They stayed in the guild for **${diffDays}** days. ` + ); + return resolve(); + } + }); + }); + } + }, } as Event; diff --git a/src/events/regex.ts b/src/events/regex.ts index 2783e57b..14fb838d 100644 --- a/src/events/regex.ts +++ b/src/events/regex.ts @@ -160,7 +160,8 @@ export default { * */ 'chat:guildLogHeader': /Guild Log \(Page \d+ of \d+\) >>$/, - 'chat:guildLog': /(([A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{4}) (([0-9]{2}):([0-9]{2})) ((EDT|EST))): ([A-Za-z-0-9-_]{2,27}) (joined|left|invited|kicked|muted|unmuted|set rank of|set MOTD|set guild tag|set guild tagcolor|set Discord|turned the chat throttle on|turned the chat throttle off)( ([A-Za-z-0-9-_]{2,27})?)?( for | to |: )?([ A-Za-z-0-9-!-_\\s]+)?/g, + 'chat:guildLog1': /(([A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{4}) (([0-9]{2}):([0-9]{2})) ((EDT|EST))): ([A-Za-z-0-9-_]{2,27}) (joined|left|invited|kicked|muted|unmuted|set rank of|set MOTD|set guild tag|set guild tagcolor|set Discord|turned the chat throttle on|turned the chat throttle off)( ([A-Za-z-0-9-_]{2,27})?)?( for | to |: )?([ A-Za-z-0-9-!-_\\s]+)?/g, + 'chat:guildLog': /([A-Za-z]{3}\s[0-9]{1,2}\s[0-9]{4})\s(([0-9]{2}):([0-9]{2}))\s(EDT|EST):\s([A-Za-z0-9-_]{2,27})\s(joined|left|invited|kicked|muted|unmuted|set rank of|set MOTD|set guild tag|set guild tagcolor|set Discord|turned the chat throttle on|turned the chat throttle off)(\s([A-Za-z0-9-_]{2,27}))?( for | to |: )?([ A-Za-z0-9-!_\\s]+)?/gm, /** *