Files
NitoriBot/modules/command_manager.js
2024-02-20 16:58:22 +00:00

89 lines
3.2 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const CommandError = require('../CommandModules/command_error.js')
function inject (bot, options) {
bot.commandManager = {
prefix: options.commands?.prefix ?? 'default',
commands: {},
execute (source, commandName, args) {
const command = this.getCommand(commandName.toLowerCase())
//Unknown command. Type "/help" for help
try {
if (!command || !command.execute) throw new CommandError({ translate: 'Unknown command %s . Type "~help" for help', with: [commandName]})
//command.unknown.argument command.unknown.command
//command.context.here
if (command.consoleOnly && !source.sources.console) throw new CommandError({ translate: 'This command can only be executed via console', color: 'dark_red' })
if (command.hashOnly && args.length === 0 && source.hash) throw new CommandError({ translate: 'Please provide a hash', color: 'dark_red' })
if (command.hashOnly && source.hash) {
// debug
// console.log(`${args} args0 ${args[0]}`) //too sus
const hash = `${args[0]}` // this is undefined
console.log(hash) // input hash
console.log(bot.hash) // actual hash
//but look in hashing.js
if (hash !== bot.hash) throw new CommandError({ translate: 'Invalid hash', color: 'dark_red' }) // so true
//im guessing its smh in hashing.js
bot.updatehash
} else if (command.hashOnly && source.sources.discord) {
const event = source.discordMessageEvent
const roles = event.member?.roles?.cache
//1106399617635061810
const hasRole = roles.some(role => role.name === '1106399617635061810')
if (!hasRole) throw new CommandError({ translate: 'You are not trusted!' })
}
/*
for (const filename of fs.readdirSync(path.join(__dirname, '../commands'))) {
try {
const command = require(path.join(__dirname, '../commands', filename))
bot.commandManager.register(command)
} catch (error) {
console.error('Failed to load command', filename, ':', error)
}
*/
return command.execute({ bot, source, arguments: args })
} catch (error) {
console.error(error)//const filename of fs.readdirSync(path.join(__dirname, '../commands'
//console.error('Failed to load command', filename, ':', error)
if (error instanceof CommandError) source.sendError(error._message)
else source.sendError({ translate: 'command.failed', hoverEvent: { action: 'show_text', contents: String(error.stack) } })
}
},
//
executeString (source, command) {
const [commandName, ...args] = command.split(' ')
return this.execute(source, commandName, args)
},
register (command) {
this.commands[command.name] = command
},
getCommand (name) {
return this.commands[name]
},
getCommands () {
return Object.values(this.commands)
}
}
//
for (const filename of fs.readdirSync(path.join(__dirname, '../commands'))) {
try {
const command = require(path.join(__dirname, '../commands', filename))
bot.commandManager.register(command)
} catch (error) {
console.error('Failed to load command', filename, ':', error)
}
}
}
module.exports = inject