smnmpp-server/protocol/userset.js

31 lines
1.2 KiB
JavaScript

module.exports.run = async (ws,user,db,msg,fun,users,connections) => {
if (!user.connected) return
if (!users[user._id].userset.try()) return
var olduser = JSON.parse(JSON.stringify(users[user._id].p))
if (typeof msg.set.name === "string" && msg.set.name.length <= 48) {
users[user._id].p.name = msg.set.name
}
if (typeof msg.set.color === "string" && msg.set.color.length == 7 && msg.set.color.startsWith('#')) {
users[user._id].p.color = msg.set.color
}
Object.values(channels).forEach(a => {if (a.ppl.includes(olduser)) a.ppl[a.ppl.indexOf(a.ppl.find(a => a._id === user._id))] = users[user._id].p })
var userp = JSON.parse(JSON.stringify(users[user._id].p))
userp.m ="p"
userp.p = user._id
if (users[user._id].p.name === olduser.name && users[user._id].p.color === olduser.color) return
try {
if (user.channel !== undefined) {Object.values(connections).filter(f => f.user.connected && channels[f.user.channel].ppl.find(a => a._id === user._id)).forEach(f => {if (users[user._id].p.vanished ? (users[f.user._id].rank >= 1) : true) f.sendData(userp)})}
} catch (error) {
//idk
}
//ws.send(JSON.stringify([userp]))
users[user._id].userset.spend(1)
await db.users.put(user._id, JSON.stringify(users[user._id]))
}
module.exports.name = "userset"