owobot/plugins/command.js

85 lines
2.8 KiB
JavaScript
Executable file

const Command = require('../util/Command.js')
const hashcheck = require('../util/hashcheck.js')
const settings = require('../settings.json')
const { getMessage } = require('../util/lang.js')
const cmds = require('../util/commands.js')
const fs = require('fs')
if (!fs.readdirSync('.').includes('userPref') && !settings.userSettingsDisabled) fs.mkdirSync('userPref')
const loadSettings = function (uuid) {
try {
if (settings.userSettingsDisabled) {
return {}
} else {
return require(`../userPref/${uuid}.json`)
}
} catch (e) {
return {}
}
}
module.exports = {
load: (b) => {
b.prefix = settings.prefix
b.lastCmd = 0
b.on('chat', (data) => {
const fullCommand = data.message
for (const prefix of b.prefix) {
if (fullCommand.startsWith(prefix)) {
const command = fullCommand.slice(prefix.length)
b.runCommand(data.username, data.nickname, data.uuid, command, data.type, prefix)
}
}
})
b.runCommand = (name, nickname, uuid, text, msgType, prefix) => {
if (uuid === '00000000-0000-0000-0000-000000000000') return
if (Date.now() - b.lastCmd <= 1000) return
const userSettings = loadSettings(uuid)
b.lastCmd = Date.now()
const cmd = text.split(' ')
const lang = settings.defaultLang
const verify = hashcheck(cmd, uuid)
if (verify > 0) {
text = cmd.slice(0, cmd.length - 1).join(' ')
}
const commandClass = new Command(uuid, name, nickname, text, msgType, prefix, b, verify, userSettings);
b.emit("command",commandClass)
if(commandClass.cancel === true) return
if (cmds[cmd[0].toLowerCase()]) {
const command = cmds[cmd[0].toLowerCase()]
const permsN = getMessage(lang, 'command.help.permsNormal')
const permsT = getMessage(lang, 'command.help.permsTrusted')
const permsO = getMessage(lang, 'command.help.permsOwner')
if (command.level !== undefined && command.level > verify) {
b.tellraw(uuid, {
text: getMessage(lang, 'command.disallowed.perms')
})
b.tellraw(uuid, {
text: getMessage(lang, 'command.disallowed.perms.yourLevel', [[permsN, permsT, permsO][verify]])
})
b.tellraw(uuid, {
text: getMessage(lang, 'command.disallowed.perms.cmdLevel', [[permsN, permsT, permsO][command.level]])
})
return
}
try {
if(commandClass.cancel === false) cmds[cmd[0].toLowerCase()].execute(commandClass)
} catch (e) {
console.log(e)
b.tellraw(uuid, {
text: getMessage(lang, 'command.error'),
color: 'red',
hoverEvent: {
action: 'show_text',
value: {
text: e.stack
}
}
})
}
}
}
}
}