2024-08-04 19:53:29 -04:00
|
|
|
const { createUuidSelector } = require('../util/command/utility')
|
|
|
|
|
2024-05-26 19:38:43 -04:00
|
|
|
function inject (bot) {
|
|
|
|
bot.sendMail = sendMail
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-05-26 19:38:43 -04:00
|
|
|
async function sendMail (sender, receiver, message) {
|
|
|
|
let playerData = Object.values(bot.playerData).find(playerData => playerData.data.username === receiver)
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-05-26 19:38:43 -04:00
|
|
|
let loadedManually = false
|
|
|
|
if (!playerData) {
|
|
|
|
playerData = await bot.loadPlayerData(receiver)
|
|
|
|
loadedManually = true
|
|
|
|
}
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-05-27 18:34:26 -04:00
|
|
|
if (loadedManually) playerData.data.username ??= receiver
|
|
|
|
|
2024-05-26 19:38:43 -04:00
|
|
|
playerData.data.mail ??= []
|
|
|
|
playerData.data.mail.push({ sender: sender, message, host: bot.host, port: bot.port })
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-09-30 20:56:05 -04:00
|
|
|
const previouslyUnread = playerData.data.mailUnread
|
|
|
|
playerData.data.mailUnread = true
|
|
|
|
|
|
|
|
if (!loadedManually && !previouslyUnread) {
|
|
|
|
for (const bot2 of bot.bots) {
|
|
|
|
const player = bot2.players.find(player => player.username === receiver)
|
|
|
|
if (player) sendMailNotification(player.uuid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 19:38:43 -04:00
|
|
|
if (loadedManually) await playerData.save()
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-08-04 19:53:29 -04:00
|
|
|
|
|
|
|
bot.on('player_data_loaded', (player, playerData) => {
|
2024-09-30 20:56:05 -04:00
|
|
|
if (!playerData.data.mailUnread) return
|
|
|
|
sendMailNotification(player.uuid)
|
2024-08-04 19:53:29 -04:00
|
|
|
})
|
2024-09-30 20:56:05 -04:00
|
|
|
|
|
|
|
function sendMailNotification (uuid) {
|
|
|
|
const msg = [{ text: "You've got mail!\nRun ", ...bot.styles.primary }, { text: bot.commands.prefixes[0] + 'mail list', ...bot.styles.secondary }, ' to read it']
|
|
|
|
bot.tellraw(msg, createUuidSelector(uuid))
|
|
|
|
}
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
|
|
|
|
2024-02-29 20:39:21 -05:00
|
|
|
module.exports = inject
|