feat(command): 🎨 make botaddress prefix and regex more flexible and configurable

This commit is contained in:
jay 2021-03-23 14:39:07 +05:00
parent a0ffaf1654
commit 0757776d8b
2 changed files with 12 additions and 6 deletions

View File

@ -86,7 +86,9 @@ reloadplugin = (event, filename, pluginpath) => {
fs.watch('./lib/plugins', reloadplugin) fs.watch('./lib/plugins', reloadplugin)
cfg.bot = bot cfg.bot = bot
cfg.botAddress = new RegExp(`^${bot.username} (!.+)`) // TODO better name, or switch to array
cfg.botAddressPrefix = '!'
cfg.botAddressRegex = new RegExp(`^${bot.username} (${cfg.botAddressPrefix}.+)`)
cfg.quiet = true cfg.quiet = true

View File

@ -62,6 +62,9 @@ const events = {
const events_registered = [] const events_registered = []
function command(username, message) { function command(username, message) {
// TODO better name, maybe an array?
cfg.botAddressPrefix = cfg.botAddressPrefix || "!"
function fuzzyRespond(responses, probability = 1, timeout = 1) { function fuzzyRespond(responses, probability = 1, timeout = 1) {
if (Math.random() < probability) { if (Math.random() < probability) {
const response = responses[Math.floor(Math.random() * responses.length)] const response = responses[Math.floor(Math.random() * responses.length)]
@ -76,7 +79,7 @@ function command(username, message) {
} }
} }
if (username === bot.username && !message.startsWith("!")) return if (username === bot.username && !message.startsWith(cfg.botAddressPrefix)) return
const player = bot.players[username] ? bot.players[username].entity : null const player = bot.players[username] ? bot.players[username].entity : null
@ -88,11 +91,12 @@ function command(username, message) {
} }
if (message.startsWith("!") || cfg.botAddress.test(message)) { if (message.startsWith(cfg.botAddressPrefix) || cfg.botAddressRegex.test(message)) {
message = cfg.botAddress.test(message) ? cfg.botAddress.exec(message)[1] : message message = cfg.botAddressRegex.test(message) ? cfg.botAddressRegex.exec(message)[1] : message
console.log(message) console.log(message)
message = message.slice(1) // remove `!` // remove `!`
message = message.startsWith(cfg.botAddressPrefix) ? message.slice(cfg.botAddressPrefix.length) : message
// TODO command dispatchEvent, for aliases // TODO command dispatchEvent, for aliases
function subcommand(message) { function subcommand(message) {
const message_parts = message.split(/\s+/) const message_parts = message.split(/\s+/)