-
-
Save HarHarLinks/8c823d267d44d8a803f55f60c957562c to your computer and use it in GitHub Desktop.
Teamspeak 3 Channel Join Monitor - Lua script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- written by https://github.com/thorwe aka philosound in the teamspeak forums | |
-- Installation: | |
-- Go to your Teamspeak program folder -> plugins -> lua_plugin | |
-- Create a new folder, rename it to "notifier" | |
-- Put this init.lua file in the "notifier" folder | |
-- Adjust user config below to your needs | |
-- Start Teamspeak, make sure the lua plugin is enabled in options->plugins | |
-- Enter the plugin settings, enable the notifier script | |
require("ts3init") | |
require("ts3defs") | |
require("ts3errors") | |
-- user config | |
local ChannelsToNotify = { | |
["Sosnowka Dub VoIP"] = { | |
["Lobby"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
}, | |
["CS:GO"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
}, | |
["StarCraft II"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
}, | |
["Borderlands"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
}, | |
["MoBa Shizzle"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
}, | |
["ARK"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
}, | |
["Evolve"] = { | |
["_anyServerGroup"] = "sound/default/stop_talking.wav" | |
} | |
} | |
} | |
-- /user config | |
-- Compatibility: Lua-5.1 | |
local function split(str, pat) | |
local t = {} -- NOTE: use {n = 0} in Lua-5.0 | |
local fpat = "(.-)" .. pat | |
local last_end = 1 | |
local s, e, cap = str:find(fpat, 1) | |
while s do | |
if s ~= 1 or cap ~= "" then | |
table.insert(t,cap) | |
end | |
last_end = e+1 | |
s, e, cap = str:find(fpat, last_end) | |
end | |
if last_end <= #str then | |
cap = str:sub(last_end) | |
table.insert(t, cap) | |
end | |
return t | |
end | |
--[[local function debugPrint(msg) | |
if type(msg) == "table" then | |
for k,v in pairs(msg) do | |
debugPrint(""..k.."="..v..";") | |
end | |
else | |
print(msg) | |
end | |
end]]-- | |
local monitorCache = {} | |
local serverGroupMap = {} | |
local MODULE_NAME = "notifier" | |
local function errorNotify(errorCode,serverConnectionHandlerID) | |
local errorMessage, errorCodeB = ts3.getErrorMessage(errorCode) | |
if (errorCodeB ~= ts3errors.ERROR_ok) then | |
errorMessage = "Error getting Error Message: " .. errorCodeB .. " for error code: " .. errorCode | |
end | |
local errorCodeC = ts3.logMessage(errorMessage, ts3defs.LogLevel.LogLevel_ERROR, MODULE_NAME, serverConnectionHandlerID) | |
ts3.printMessage(serverConnectionHandlerID, (MODULE_NAME .. errorMessage), 0) | |
end | |
local function checkNotify(serverConnectionHandlerID, clientID, channelID, visibility) | |
if (visibility == ts3defs.Visibility.LEAVE_VISIBILITY) then return end -- we don't care about leaves | |
local sMonitorCache = monitorCache[serverConnectionHandlerID] | |
if (not sMonitorCache) then return end -- we don't care about this server | |
if (clientID == sMonitorCache.myClientID) then return end -- we don't care about our own moves | |
local channelConfig = sMonitorCache.channels[channelID] | |
if (not channelConfig) then return end -- we don't care about this channel | |
local grps, errorCode = ts3.getClientVariableAsString(serverConnectionHandlerID, clientID, ts3defs.ClientProperties.CLIENT_SERVERGROUPS) | |
if (errorCode ~= ts3errors.ERROR_ok) then | |
errorNotify(errorCode,serverConnectionHandlerID) | |
return | |
end | |
grps = split(grps,",") | |
local sServerGroupMap = serverGroupMap[serverConnectionHandlerID] | |
local waveFile | |
for _,v in pairs(grps) do | |
waveFile = channelConfig[sServerGroupMap[v]] | |
if (waveFile) then | |
break | |
end | |
end | |
if (not waveFile) then | |
waveFile = channelConfig["_anyServerGroup"] | |
end | |
if (waveFile ~= nil) then | |
errorCode = ts3.playWaveFile(serverConnectionHandlerID, waveFile) | |
if (errorCode ~= ts3errors.ERROR_ok) then | |
errorNotify(errorCode,serverConnectionHandlerID) | |
return | |
end | |
end | |
end | |
local function onConnectStatusChangeEvent(serverConnectionHandlerID, status, errorNumber) | |
if (status == ts3defs.ConnectStatus.STATUS_CONNECTED) then -- at this point we can get server infos, which is before any onNewChannel Event gets triggered | |
-- now we check if we connected to a server we're actually want to monitor | |
local result, errorCode = ts3.getServerVariableAsString(serverConnectionHandlerID, ts3defs.VirtualServerProperties.VIRTUALSERVER_NAME) | |
if (errorCode ~= ts3errors.ERROR_ok) then | |
ts3.printMessage(serverConnectionHandlerID, (MODULE_NAME .. ": Error getting server name: " .. errorCode), 0) | |
return | |
end | |
if (ChannelsToNotify[result]) then | |
local myClientID, errorCode = ts3.getClientID(serverConnectionHandlerID) | |
if (errorCode ~= ts3errors.ERROR_ok) then | |
errorNotify(errorCode,serverConnectionHandlerID) | |
return | |
end | |
monitorCache[serverConnectionHandlerID] = { | |
["name"] = result, | |
["myClientID"] = myClientID | |
} -- using the existence of the object later onNewChannelEvent | |
ts3.printMessage(serverConnectionHandlerID, (MODULE_NAME .. ": Monitoring " .. result), 0) | |
end | |
elseif (status == ts3defs.ConnectStatus.STATUS_DISCONNECTED) then | |
monitorCache[serverConnectionHandlerID] = nil -- clean up | |
serverGroupMap[serverConnectionHandlerID] = nil | |
end | |
end | |
local function onNewChannelEvent(serverConnectionHandlerID, channelID, channelParentID) | |
local sMonitorCache = monitorCache[serverConnectionHandlerID] | |
if (not sMonitorCache) then return end -- since we tested the server name on connect we don't need to get the string everytime, which would be more expensive | |
local channelName, errorCode = ts3.getChannelVariableAsString(serverConnectionHandlerID, channelID, ts3defs.ChannelProperties.CHANNEL_NAME) | |
if (errorCode ~= ts3errors.ERROR_ok) then | |
errorNotify(errorCode,serverConnectionHandlerID) | |
return | |
end | |
local channelConfig = ChannelsToNotify[sMonitorCache.name][channelName] | |
if (not channelConfig) then return end | |
if (not sMonitorCache.channels) then sMonitorCache.channels = {} end | |
sMonitorCache.channels[channelID] = channelConfig | |
ts3.printMessage(serverConnectionHandlerID, (MODULE_NAME .. ": Monitoring " .. channelName), 0) | |
end | |
local function onUpdateChannelEditedEvent(serverConnectionHandlerID, channelID, invokerID, invokerName, invokerUniqueIdentifier) | |
local sMonitorCache = monitorCache[serverConnectionHandlerID] | |
if (not sMonitorCache) then return end | |
if sMonitorCache.channels[channelID] then sMonitorCache.channels[channelID] = nil end -- clear the channel since it was somehow edited | |
onNewChannelEvent(serverConnectionHandlerID, channelID, 0) -- check for (re)addition; more sophisticated solutions with subchannel sensitivity would need to get the parent channel here | |
end | |
local function onClientMoveEvent(serverConnectionHandlerID, clientID, oldChannelID, newChannelID, visibility, moveMessage) | |
checkNotify(serverConnectionHandlerID, clientID, newChannelID, visibility) | |
end | |
local function onClientMoveMovedEvent(serverConnectionHandlerID, clientID, oldChannelID, newChannelID, visibility, moverID, moverName, moverUniqueIdentifier, moveMessage) | |
checkNotify(serverConnectionHandlerID, clientID, newChannelID, visibility) | |
end | |
local function onServerGroupListEvent(serverConnectionHandlerID, serverGroupID, name, groupType, iconID, saveDB) | |
if (not serverGroupMap[serverConnectionHandlerID]) then serverGroupMap[serverConnectionHandlerID] = {} end | |
serverGroupMap[serverConnectionHandlerID][tostring(serverGroupID)] = name | |
end | |
--[[local function onServerGroupListFinishedEvent(serverConnectionHandlerID) | |
if (serverGroupMap[serverConnectionHandlerID]) then | |
debugPrint(serverGroupMap[serverConnectionHandlerID]) | |
end | |
end]]-- | |
local registeredEvents = { | |
["onNewChannelEvent"] = onNewChannelEvent, | |
["onConnectStatusChangeEvent"] = onConnectStatusChangeEvent, | |
["onClientMoveEvent"] = onClientMoveEvent, | |
["onClientMoveMovedEvent"] = onClientMoveMovedEvent, | |
["onServerGroupListEvent"] = onServerGroupListEvent --, | |
--onServerGroupListFinishedEvent = onServerGroupListFinishedEvent | |
} | |
ts3RegisterModule(MODULE_NAME, registeredEvents) | |
-- test | |
notifier = {} | |
notifier.test = function(serverConnectionHandlerID) | |
onClientMoveEvent(serverConnectionHandlerID,23,0,1,1,"Move It.") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment