44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
const { createUuidSelector } = require('../util/command/utility')
|
|
|
|
function inject (bot) {
|
|
bot.sendMail = sendMail
|
|
|
|
async function sendMail (sender, receiver, message) {
|
|
let playerData = Object.values(bot.playerData).find(playerData => playerData.data.username === receiver)
|
|
|
|
let loadedManually = false
|
|
if (!playerData) {
|
|
playerData = await bot.loadPlayerData(receiver)
|
|
loadedManually = true
|
|
}
|
|
|
|
if (loadedManually) playerData.data.username ??= receiver
|
|
|
|
playerData.data.mail ??= []
|
|
playerData.data.mail.push({ sender: sender, message, host: bot.host, port: bot.port })
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
if (loadedManually) await playerData.save()
|
|
}
|
|
|
|
bot.on('player_data_loaded', (player, playerData) => {
|
|
if (!playerData.data.mailUnread) return
|
|
sendMailNotification(player.uuid)
|
|
})
|
|
|
|
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))
|
|
}
|
|
}
|
|
|
|
module.exports = inject
|