home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / TeamSpeak / TeamSpeak3-Client-win32-3.0.0.exe / plugins / lua_plugin / testmodule / demo.lua next >
Text File  |  2011-02-08  |  8KB  |  205 lines

  1. --
  2. -- Some TeamSpeak 3 functions for testing and demonstration
  3. --
  4.  
  5. require("ts3defs")
  6. require("ts3errors")
  7.  
  8. --
  9. -- Call these function from the TeamSpeak 3 client console via: /lua run testmodule.<function>
  10. -- Note the serverConnectionHandlerID of the current server is always passed.
  11. --
  12. -- You might want to pass the "-console" option when starting the TeamSpeak 3 client to get a console where a lot
  13. -- of plugin related debug output will appear.
  14. --
  15.  
  16. -- Run with "/lua run testmodule.showClientLibVersion"
  17. local function showClientLibVersion(serverConnectionHandlerID)
  18.     local version, error = ts3.getClientLibVersion()
  19.     if error ~= ts3errors.ERROR_ok then
  20.         print("Error getting clientlib version: " .. error)
  21.         return
  22.     end
  23.     ts3.printMessageToCurrentTab("Clientlib version: " .. version)
  24. end
  25.  
  26. -- Run with "/lua run testmodule.test"
  27. local function test(serverConnectionHandlerID)
  28.     ts3.printMessageToCurrentTab("Test on serverConnectionHandlerID: " .. serverConnectionHandlerID)
  29.  
  30.     -- Get own client ID
  31.     local myClientID, error = ts3.getClientID(serverConnectionHandlerID)
  32.     if error ~= ts3errors.ERROR_ok then
  33.         print("Error getting own client ID: " .. error)
  34.         return
  35.     end
  36.     if myClientID == 0 then
  37.         ts3.printMessageToCurrentTab("Not connected")
  38.         return
  39.     end
  40.     ts3.printMessageToCurrentTab("My client ID: " .. myClientID)
  41.  
  42.     -- Get own nickname
  43.     local myNickname, error = ts3.getClientVariableAsString(serverConnectionHandlerID, myClientID, ts3defs.ClientProperties.CLIENT_NICKNAME)
  44.     if error ~= ts3errors.ERROR_ok then
  45.         print("Error getting own client nickname: " .. error)
  46.         return
  47.     end
  48.     ts3.printMessageToCurrentTab("My nickname: " .. myNickname)
  49.  
  50.     -- Get which channel we are in
  51.     local myChannelID, error = ts3.getChannelOfClient(serverConnectionHandlerID, myClientID)
  52.     if error ~= ts3errors.ERROR_ok then
  53.         print("Error getting own channel: " .. error)
  54.         return
  55.     end
  56.  
  57.     -- Get the name of my channel
  58.     local myChannelName, error = ts3.getChannelVariableAsString(serverConnectionHandlerID, myChannelID, ts3defs.ChannelProperties.CHANNEL_NAME)
  59.     if error ~= ts3errors.ERROR_ok then
  60.         print("Error getting channel name: " .. error)
  61.         return
  62.     end
  63.     ts3.printMessageToCurrentTab("I am in channel ID: " .. myChannelName .. " (" .. myChannelID .. ")")
  64. end
  65.  
  66. -- Run with "/lua run testmodule.argsTest <arg1> <arg2> <arg3>", args can be numbers or strings
  67. local function argsTest(serverConnectionHandlerID, arg1, arg2, arg3)
  68.     ts3.printMessageToCurrentTab("argsTest: " .. serverConnectionHandlerID .. " - " .. arg1 .. " " .. arg2 .. " " .. arg3)
  69. end
  70.  
  71. -- Run with "/lua run testmodule.showClients"
  72. local function showClients(serverConnectionHandlerID)
  73.     local clients, error = ts3.getClientList(serverConnectionHandlerID)
  74.     if error == ts3errors.ERROR_not_connected then
  75.         ts3.printMessageToCurrentTab("Not connected")
  76.         return
  77.     elseif error ~= ts3errors.ERROR_ok then
  78.         print("Error getting client list: " .. error)
  79.         return
  80.     end
  81.  
  82.     local msg = ("There are currently " .. #clients .. " visible clients:")
  83.     for i=1, #clients do
  84.         local clientName, error = ts3.getClientVariableAsString(serverConnectionHandlerID, clients[i], ts3defs.ClientProperties.CLIENT_NICKNAME)
  85.         if error == ts3errors.ERROR_ok then
  86.             msg = msg .. "\n " .. clients[i] .. " " .. clientName
  87.         else
  88.             clientName = "Error getting client name"
  89.         end
  90.     end
  91.     ts3.printMessageToCurrentTab(msg)
  92. end
  93.  
  94. -- Run with "/lua run testmodule.complainAboutMyself <complainReason>"
  95. local function complainAboutMyself(serverConnectionHandlerID, complainReason)
  96.     -- Add a complain about own client. Makes no sense, but hey, it's a demo. :-)
  97.     
  98.     -- Get own client ID
  99.     local myClientID, error = ts3.getClientID(serverConnectionHandlerID)
  100.     if error ~= ts3errors.ERROR_ok then
  101.         print("Error getting own client ID: " .. error)
  102.         return
  103.     end
  104.     if myClientID == 0 then
  105.         ts3.printMessageToCurrentTab("Not connected")
  106.         return
  107.     end
  108.     
  109.     -- As we need the databaseID for complains, get own database ID from own client ID
  110.     local myDatabaseID, error = ts3.getClientVariableAsUInt64(serverConnectionHandlerID, myClientID, ts3defs.ClientProperties.CLIENT_DATABASE_ID)
  111.     if error ~= ts3errors.ERROR_ok then
  112.         print("Error getting own client database ID: " .. error)
  113.         return
  114.     end
  115.     
  116.     -- Send complain
  117.     local error = ts3.requestComplainAdd(serverConnectionHandlerID, myDatabaseID, complainReason)
  118.     if error ~= ts3errors.ERROR_ok then
  119.         print("Error adding complain: " .. error)
  120.         return
  121.     end
  122.     ts3.printMessageToCurrentTab("Complain about myself added with reason: " .. complainReason)
  123. end
  124.  
  125. -- Run with "/lua run testmodule.showClientsInChannel <channelID>"
  126. local function showClientsInChannel(serverConnectionHandlerID, channelID)
  127.     -- Get list of clients in channelID passes as parameter
  128.     local clientList, error = ts3.getChannelClientList(serverConnectionHandlerID, channelID)
  129.     if error ~= ts3errors.ERROR_ok then
  130.         print("Error getting channel client list: " .. error)
  131.         return
  132.     end
  133.  
  134.     -- Get name of this channel
  135.     local channelName, error = ts3.getChannelVariableAsString(serverConnectionHandlerID, channelID, ts3defs.ChannelProperties.CHANNEL_NAME)
  136.     if error ~= ts3errors.ERROR_ok then
  137.         print("Error getting channel name: " .. error)
  138.         return
  139.     end
  140.  
  141.     -- Loop through all clients in list and assemble message from their clientID and nickname
  142.     msg = "Visible clients in channel " .. channelName
  143.     for i=1, #clientList do
  144.         local clientName, error = ts3.getClientVariableAsString(serverConnectionHandlerID, clientList[i], ts3defs.ClientProperties.CLIENT_NICKNAME)
  145.         if error == ts3errors.ERROR_ok then
  146.             msg = msg .. "\n " .. clientList[i] .. " " .. clientName
  147.         else
  148.             clientName = "Error getting client name"
  149.         end
  150.     end    
  151.     ts3.printMessageToCurrentTab(msg)
  152. end
  153.  
  154. -- Run with "/lua run testmodule.muteClient <clientID>"
  155. local function muteClient(serverConnectionHandlerID, clientID)
  156.     local clientIDs = { clientID }  -- Array of clientIDs to mute. You can define multiple clientIDs here, like: clientIds = { 1, 2, 3 }
  157.     local error = ts3.requestMuteClients(serverConnectionHandlerID, clientIDs)
  158.     if error == ts3errors.ERROR_ok then
  159.         ts3.printMessageToCurrentTab("Client " .. clientID .. " muted")
  160.     else
  161.         print("Error requesting client mute: " .. error)
  162.     end
  163. end
  164.  
  165. -- Run with "/lua run testmodule.unmuteClient <clientID>"
  166. local function unmuteClient(serverConnectionHandlerID, clientID)
  167.     local clientIDs = { clientID }  -- Array of clientIDs to unmute. You can define multiple clientIDs here, like: clientIds = { 1, 2, 3 }
  168.     local error = ts3.requestUnmuteClients(serverConnectionHandlerID, clientIDs)
  169.     if error == ts3errors.ERROR_ok then
  170.         ts3.printMessageToCurrentTab("Client " .. clientID .. " unmuted")
  171.     else
  172.         print("Error requesting client unmute: " .. error)
  173.     end
  174. end
  175.  
  176. -- Run with "/lua run testmodule.sendCommand <command>"
  177. local function sendCommand(serverConnectionHandlerID, command)
  178.     --[[
  179.     Target Mode: 
  180.     0 = send to all clients in current channel (targetIDs ignored)
  181.     1 = send to all clients on server (targetIDs ignored)
  182.     2 = send to all given client targetIDs
  183.     3 = send to all subscribed clients in current channel (targetIDs ignored)
  184.     ]]--
  185.     local targetMode = 0
  186.     local targetIDs = {}
  187.     local error = ts3.sendPluginCommand(serverConnectionHandlerID, command, targetMode, targetIDs)
  188.     if error ~= ts3errors.ERROR_ok then
  189.         print("Error sending plugin command: " .. error)
  190.     end
  191.     -- Monitor onPluginCommandEvent to see the incoming event
  192. end
  193.  
  194. testmodule = {
  195.     showClientLibVersion = showClientLibVersion,
  196.     test = test,
  197.     argsTest = argsTest,
  198.     showClients = showClients,
  199.     complainAboutMyself = complainAboutMyself,
  200.     showClientsInChannel = showClientsInChannel,
  201.     muteClient = muteClient,
  202.     unmuteClient = unmuteClient,
  203.     sendCommand = sendCommand
  204. }
  205.