1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-11-16 16:30:09 +00:00
Tims-Chat/files_wcf/js/Bastelstu.be/Chat/CommandHandler.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
2024-01-13 20:03:36 +00:00
* Copyright (c) 2010-2024 Tim Düsterhus.
2018-08-16 22:30:59 +00:00
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2024-06-15 19:56:58 +00:00
* Change Date: 2028-06-15
2018-08-16 22:30:59 +00:00
*
* On the date above, in accordance with the Business Source
* License, use of this software will be governed by version 2
* or later of the General Public License.
*/
2020-11-01 16:41:19 +00:00
define(['./Parser', './ParseError'], function (Parser, ParseError) {
'use strict'
2018-08-16 22:30:59 +00:00
2020-11-01 16:41:19 +00:00
const DEPENDENCIES = ['Trigger', 'Command']
2018-08-16 22:30:59 +00:00
class CommandHandler {
constructor(triggers, commands) {
this.triggers = triggers
this.commands = commands
}
splitCommand(input) {
const result = Parser.Command.parse(Parser.Streams.ofString(input))
if (result.isAccepted()) {
return result.value
2020-11-01 16:41:19 +00:00
} else {
2018-08-16 22:30:59 +00:00
throw new ParseError('Empty trigger')
}
}
applyCommand(command, parameterString) {
2020-11-01 16:41:19 +00:00
const result = command
.getParameterParser()
.parse(Parser.Streams.ofString(parameterString))
2018-08-16 22:30:59 +00:00
if (result.isAccepted()) {
return result.value
2020-11-01 16:41:19 +00:00
} else {
2018-08-16 22:30:59 +00:00
throw new ParseError('Could not parse', { result, parameterString })
}
}
getTriggers() {
return this.triggers.keys()
}
getCommandByTrigger(trigger) {
const data = this.triggers.get(trigger)
if (data == null) return null
return this.getCommandByIdentifier(...data)
}
getCommandByIdentifier(packageName, identifier) {
return this.commands[`${packageName.replace(/\./g, '-')}:${identifier}`]
}
}
CommandHandler.DEPENDENCIES = DEPENDENCIES
return CommandHandler
2020-11-01 16:41:19 +00:00
})