smnmpp-server/protocol/a.js

289 lines
12 KiB
JavaScript

module.exports.run = async (ws,user,db,msg,fun,users,connections) => {
if (typeof msg.message !== "string") return;
if (!user.channel) return;
if (!user.connected) return
if (!users[user._id].chatbypass && !user.quotas.chat.try()) return;
Object.values(connections).filter(f => f.user.channel === user.channel).forEach(f => f.sendData({m: "a", a: msg.message.substr(0,1023), p: users[user._id].p, t: Date.now()}))
chat[user.channel].push({m: "a", a: msg.message.substr(0,1023), p: users[user._id].p, t: Date.now()})
if (chat[user.channel].length > 32) chat[user.channel].splice(0,1)
user.quotas.chat.spend(1)
var cmd = msg.message.trim().split(' ')[0]
var args = msg.message.trim().substr(cmd.length).trim()
var rank = users[user._id].rank
var say = (message) => ws.sendData({m: "a", p: {name: "Server", _id: "server", id: "server", color: "#5555ff"}, a: message, t: Date.now()})
if (cmd === "~help") {
var cmds = {"~help":0, "~gentoken": 2, "~setrank": 2, "~clearchat":1, "~settag": 1, "~removetag":1, "~endprocess": 2, "~notebypass":2, "~custombypass": 2, "~chatbypass": 2, "~info": 1, "~js": 3, "~unban": 2, "~token": 3, "~baninfo": 2, "~usersetothers": 3, "~announce": 2}
var availablecmds = Object.keys(cmds).filter(a => cmds[a] <= users[user._id].rank)
return say(`Commands: ${availablecmds.join(', ')}`)
}
if (cmd === "~gentoken" && rank >= 2) {
var token = ""
for (var i = 0; i < 48; i++) token+= ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"][Math.floor(Math.random() * 16)]
var _id = ""
for (var i = 0; i < 24; i++) _id+= ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"][Math.floor(Math.random() * 16)]
var color = "#"
for (var i = 0; i < 6; i++) color+= ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"][Math.floor(Math.random() * 16)]
var part = {rank: 0,p: {m:"p",name: "Anonymous", _id: _id, id: _id, color: color}}
db.tokens.put(token, _id)
db.users.put(_id, JSON.stringify(part))
say(`Token successfully generated! \`${token} , ${_id}\``)
}
if (cmd === "~settag" && rank >= 1) {
if (args.length == 0) return say('Usage: ~settag (_id) (Hex Color) (Tag)')
var argsplit = args.split(' ')
try {
var user = await db.users.get(argsplit[0])
user = JSON.parse(user)
} catch (error) {
return say(`There was an error getting info about \`${argsplit[0]}\`.`)
}
var color = argsplit[1]
if (!(typeof color === "string" && color.length == 7 && color.startsWith('#'))) return say(`\`${color}\` is not a valid color.`)
argsplit.splice(0,2)
user.p.tag = {color: color, text: argsplit.join(' ')}
await db.users.put(user.p._id, JSON.stringify(user))
connections.filter(a => a.user._id === user.p._id).forEach(a => a.close())
say('Success.')
}
if (cmd === "~setrank" && rank >= 2) {
if (args.length == 0) return say("Usage: ~setrank (_id) (number)")
var argsplit = args.split(' ')
try {
var user = await db.users.get(argsplit[0])
user = JSON.parse(user)
} catch (error) {
return say(`There was an error getting info about \`${argsplit[0]}\`.`)
}
if (!(argsplit[1] === "0" || argsplit[1] === "1" || argsplit[1] === "2" || argsplit[1] === "3")) return say(`\`${argsplit[1]}\` is not a valid rank.`)
if (user.rank >= rank) return say(`This user has a similar or higher rank than you: \`You: ${rank}\`, \`Them: ${user.rank}\``)
if (Number(argsplit[1]) >= rank) return say(`This rank is higher than yours! Your rank: \`${rank}\``)
user.rank = Number(argsplit[1])
if (users[argsplit[0]]) users[argsplit[0]].rank = user.rank
await db.users.put(user.p._id, JSON.stringify(user))
say("Success.")
}
if (cmd === "~removetag" && rank >= 1) {
if (args.length == 0) return say('Usage: ~removetag (_id)')
try {
var user = await db.users.get(args)
user = JSON.parse(user)
} catch (error) {
return say(`There was an error getting info about \`${args}\`.`)
}
if (!user.p.tag) return say('This user already has no tag!')
delete user.p.tag
await db.users.put(user.p._id, JSON.stringify(user))
connections.filter(a => a.user._id === user.p._id).forEach(a => a.close())
say('Success.')
}
if (cmd === "~clearchat" && rank >= 1) {
chat[user.channel] = []
connections.filter(a => a.user.channel === user.channel).forEach(a => a.sendData({m:"c", c: []}))
}
if (cmd === "~endprocess" && rank >= 2) {
connections.filter(a => a.user.connected).forEach(a => a.sendData({"m": "notification",title: "Notice", text: "The server is restarting, you will be right back!", duration: 10000}))
setTimeout(() => process.exit(),1000)
}
if (cmd === "~muhe") say('MUHE!!!')
if (cmd === "~mure") say('MURE!!!')
if (cmd === "~smoki") say('SMOKI!!!')
if (cmd === "~notebypass" && rank >= 2) {
if (args.length == 0) return say('Usage: ~notebypass (_ID) (boolean)')
var argsplit = args.split(' ')
if (argsplit[1] === "true") var boo = true
if (argsplit[1] === "false") var boo = false
if (boo === undefined) return say('Invalid Boolean')
try {
var info = await db.users.get(argsplit[0])
info = JSON.parse(info)
} catch (error) {
return say (`There was an error getting info about \`${argsplit[0]}\`.`)
}
info.notebypass = boo
if (users[info.p._id]) users[info.p._id].notebypass = boo
await db.users.put(info.p._id, JSON.stringify(info))
say(`Set note bypass for \`${info.p._id}\` to \`${boo}\``)
}
if (cmd === "~custombypass" && rank >= 2) {
if (args.length == 0) return say('Usage: ~custombypass (_ID) (boolean)')
var argsplit = args.split(' ')
if (argsplit[1] === "true") var boo = true
if (argsplit[1] === "false") var boo = false
if (boo === undefined) return say('Invalid Boolean')
try {
var info = await db.users.get(argsplit[0])
info = JSON.parse(info)
} catch (error) {
return say (`There was an error getting info about \`${argsplit[0]}\`.`)
}
info.custombypass = boo
if (users[info.p._id]) users[info.p._id].custombypass = boo
await db.users.put(info.p._id, JSON.stringify(info))
say(`Set custom bypass for \`${info.p._id}\` to \`${boo}\``)
}
if (cmd === "~info" && rank >= 1) {
if (args.length == 0 ) return say('Usage: ~info (_id)')
try {
var info = await db.users.get(args)
info = JSON.parse(info)
} catch (error) {
return say(`There was an error getting info about \`${args}\`.`)
}
if (connections.find(a => a.user._id === info.p._id)) {
var rooms = []
connections.filter(a => a.user._id === info.p._id && a.user.channel).forEach(a => {
if (rooms.includes(a.user.channel)) return
rooms.push(a.user.channel)
})
}
return say(`_ID: \`${info.p._id}\` | Rank: \`${info.rank}\` | Chat Bypass: \`${!!info.chatbypass}\` | Note Bypass: \`${!!info.notebypass}\` | Custom Bypass: \`${!!info.custombypass}\` | Userset Others: \`${!!info.usersetothers}\` | Name: \`\`\`${info.p.name}\`\`\` | Color: \`${info.p.color}\` | ` + (info.p.tag ? (`Tag: (Text: \`${info.p.tag.text}\` | Color: \`${info.p.tag.color}\`) | `) : "") + ((rooms !== undefined) ? ("Online: " + rooms.join(' -|- ')) : ("Offline")))
}
if (cmd === "~js" && rank >= 3) {
try {
var result = await eval(args)
say("✅=> " + JSON.stringify(result))
} catch (error) {
say("❎=>" + error.toString())
}
}
if (cmd === "~token" && rank >= 3) {
var argsplit = args.split(' ')
var tokentype = ['get','reset']
if (!tokentype.includes(argsplit[0])) return say(`Types: ${tokentype.join(', ')} | Usage: ~token (type) (args)`)
var jsondb = {}
jsondb.tokens = await db.tokens.iterator().all()
jsondb.ips = await db.ips.iterator().all()
//for await (var [key, value] of db.tokens.iterator().all()) jsondb.tokens[key] = value
//for await (var [key, value] of db.ips.iterator().all()) jsondb.ips[key] = value
if (argsplit[0] === "get") {
try {
var info = await db.users.get(argsplit[1])
var info = JSON.parse(info)
} catch (error) {
return say(`There was an error getting info about \`${argsplit[1]}\`.`)
}
var founditems = {}
founditems.tokens = jsondb.tokens.filter(token => token[1] === argsplit[1]).map(a => a[0])
//founditems.ips = Object.keys(jsondb.ips).filter(ip => founditems.tokens.includes(jsondb.ips[ip]))
return say(`Tokens: ` + founditems.tokens.map(a => `\`${a}\``).join(', '))
} else if (argsplit[0] === "reset") {
try {
var info = await db.users.get(argsplit[1])
var info = JSON.parse(info)
} catch (error) {
return say(`There was an error getting info about \`${argsplit[1]}\`.`)
}
var founditems = {}
founditems.tokens = jsondb.tokens.filter(token => token[1] === argsplit[1]).map(a => a[0])
founditems.ips = jsondb.ips.filter(ip => founditems.tokens.includes(ip[1])).map(a => a[0])
var token = ""
for (var i = 0; i < 48; i++) token+= ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"][Math.floor(Math.random() * 16)]
founditems.ips.forEach(a => db.ips.put(a,token))
founditems.tokens.forEach(a => db.tokens.del(a))
db.tokens.put(token, argsplit[1])
connections.filter(a => a.user._id === argsplit[1]).forEach(a => a.close())
return say(`Token(s) reset, new token: \`${token}\``)
}
}
if (cmd === "~unban" && rank >= 2) {
if (args.length == 0) return say('Usage: ~unban (_id)')
try {
var info = await db.users.get(args)
var info = JSON.parse(info)
} catch (error) {
return say(`There was an error getting info about \`${args}\`.`)
}
if (!info.siteban) return say('This user isn\'t site-banned.')
delete info.siteban
await db.users.put(args, JSON.stringify(info))
say('Done.')
}
if (cmd === "~baninfo" && rank >= 2) {
if (args.length == 0 ) return say('Usage: ~baninfo (_id)')
try {
var info = await db.users.get(args)
info = JSON.parse(info)
} catch (error) {
return say(`There was an error getting info about \`${args}\`.`)
}
if (!info.siteban) return say('This user isn\'t site-banned.')
say(`User ${args} banned by \`${info.siteban._id}\`, ban ends on ${new Date(info.siteban.ends)} for \`\`${info.siteban.reason}\`\`` + ((info.siteban.note === undefined) ? "" : ` | Note: \`\`\`${info.siteban.note}\`\`\``))
}
if (cmd === "~chatbypass" && rank >= 2) {
if (args.length == 0) return say('Usage: ~chatbypass (_ID) (boolean)')
var argsplit = args.split(' ')
if (argsplit[1] === "true") var boo = true
if (argsplit[1] === "false") var boo = false
if (boo === undefined) return say('Invalid Boolean')
try {
var info = await db.users.get(argsplit[0])
info = JSON.parse(info)
} catch (error) {
return say (`There was an error getting info about \`${argsplit[0]}\`.`)
}
info.chatbypass = boo
if (users[info.p._id]) users[info.p._id].chatbypass = boo
await db.users.put(info.p._id, JSON.stringify(info))
say(`Set chat bypass for \`${info.p._id}\` to \`${boo}\``)
}
if (cmd === "~usersetothers" && rank >= 3) {
if (args.length == 0) return say('Usage: ~usersetothers (_ID) (boolean)')
var argsplit = args.split(' ')
if (argsplit[1] === "true") var boo = true
if (argsplit[1] === "false") var boo = false
if (boo === undefined) return say('Invalid Boolean')
try {
var info = await db.users.get(argsplit[0])
info = JSON.parse(info)
} catch (error) {
return say (`There was an error getting info about \`${argsplit[0]}\`.`)
}
info.usersetothers = boo
if (users[info.p._id]) users[info.p._id].usersetothers = boo
await db.users.put(info.p._id, JSON.stringify(info))
say(`Set userset others for \`${info.p._id}\` to \`${boo}\``)
}
if (cmd === "~announce" && rank >= 2) {
if (args.length == 0) return say(`Usage: ~announce (msg)`)
connections.filter(a => a.user.connected).forEach(a => a.sendData({"m": "notification",title: "Notice", text: args, duration: 60000}))
}
}
module.exports.name = "a"