chipmunkbot3/commands/cloop.js
2024-07-31 22:34:19 -04:00

92 lines
2.4 KiB
JavaScript

const { literal, argument, integer, greedyString } = require('brigadier-commands')
module.exports = {
register (dispatcher) {
const node = dispatcher.register(
literal('cloop')
.requires(source => source.permissionLevel >= node.permissionLevel)
.then(
literal('add')
.then(
argument('interval', integer())
.then(
argument('command', greedyString())
.executes(c => this.addCommand(c))
)
)
)
.then(
literal('remove')
.then(
argument('index', integer())
.executes(c => this.removeCommand(c))
)
)
.then(
literal('list')
.executes(c => this.listCommand(c))
)
.then(
literal('clear')
.executes(c => this.clearCommand(c))
)
)
node.description = 'Loops commands in the command core'
node.permissionLevel = 1
},
addCommand (context) {
const source = context.source
const bot = source.bot
const interval = context.getArgument('interval')
const command = context.getArgument('command')
bot.cloops.push({ command, interval })
source.sendFeedback([
{ text: 'Looping command ', ...bot.styles.primary },
{ text: command, ...bot.styles.secondary },
' at interval ',
{ text: String(interval), ...bot.styles.secondary }
], false)
},
removeCommand (context) {
const source = context.source
const bot = source.bot
const idx = context.getArgument('index')
bot.cloops.splice(idx, 1)
source.sendFeedback([
{ text: 'Removed cloop ', ...bot.styles.primary },
{ text: String(idx), ...bot.styles.secondary }
], false)
},
listCommand (context) {
const source = context.source
const bot = source.bot
const msg = [{ text: 'Cloops: \n', ...bot.styles.primary }]
for (let i = 0; i < bot.cloops.length; i++) {
if (i !== 0) msg.push('\n')
msg.push(
String(i),
': ',
{ text: String(bot.cloops[i].command), ...bot.styles.secondary }
)
}
source.sendFeedback(msg, false)
},
clearCommand (context) {
const source = context.source
const bot = source.bot
bot.cloops.splice(0, bot.cloops.length)
source.sendFeedback({ text: 'Cleared cloops', ...bot.styles.primary }, false)
}
}