-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChatCommands.ttslua
executable file
·57 lines (42 loc) · 1.49 KB
/
ChatCommands.ttslua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require('ge_tts.License')
local EventManager = require('ge_tts.EventManager')
---@alias ge_tts__ChatCommand_Callback fun(value: string, player: tts__Player): void
---@type table<string, ge_tts__ChatCommand_Callback>
local commandCallbacks = {}
local chatCommandPattern = '^~(.+)~%s*(.*)'
---@class tts__ChatCommands
local ChatCommand = {}
--- Replaces the current command pattern (default '^~(.+)~%s*(.*)').
---
--- The pattern must have two (and only two) capture groups, the first being the command name, and
--- the second being the value, which once captured will be passed to command callbacks.
---@param pattern string
function ChatCommand.setPattern(pattern)
chatCommandPattern = pattern
end
---@param command string
---@param callback ge_tts__ChatCommand_Callback
function ChatCommand.addCommand(command, callback)
commandCallbacks[command] = callback
end
---@param command string
function ChatCommand.removeCommand(command)
commandCallbacks[command] = nil
end
---@param message string
---@param player tts__Player
local function onChat(message, player)
local _, _, command, value = message:find(chatCommandPattern)
if not command then
return true
end
local callback = commandCallbacks[--[[---@type string]] command]
if callback then
callback(--[[---@type string]] value, player)
else
broadcastToColor('Unknown command: ' .. command, player.color, 'Red')
end
return false
end
EventManager.addHandler('onChat', onChat)
return ChatCommand