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

master
jay 3 years ago
parent a0ffaf1654
commit 0757776d8b
  1. 4
      lib/index.js
  2. 14
      lib/plugins/command.js

@ -86,7 +86,9 @@ reloadplugin = (event, filename, pluginpath) => {
fs.watch('./lib/plugins', reloadplugin)
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

@ -62,6 +62,9 @@ const events = {
const events_registered = []
function command(username, message) {
// TODO better name, maybe an array?
cfg.botAddressPrefix = cfg.botAddressPrefix || "!"
function fuzzyRespond(responses, probability = 1, timeout = 1) {
if (Math.random() < probability) {
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
@ -88,11 +91,12 @@ function command(username, message) {
}
if (message.startsWith("!") || cfg.botAddress.test(message)) {
message = cfg.botAddress.test(message) ? cfg.botAddress.exec(message)[1] : message
if (message.startsWith(cfg.botAddressPrefix) || cfg.botAddressRegex.test(message)) {
message = cfg.botAddressRegex.test(message) ? cfg.botAddressRegex.exec(message)[1] : 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
function subcommand(message) {
const message_parts = message.split(/\s+/)

Loading…
Cancel
Save