61 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { NodeInitializer } from "node-red"
 | |
| import { TeamSpeak } from "ts3-nodejs-library"
 | |
| import { Ts3ConfigNode } from "../TS3Config/types"
 | |
| import { Ts3CallConfig, Ts3CallNode } from "./types"
 | |
| 
 | |
| const nodeInit: NodeInitializer = (RED): void => {
 | |
| 	function Ts3Call(
 | |
| 		this: Ts3CallNode,
 | |
| 		config: Ts3CallConfig
 | |
| 	): void {
 | |
| 		RED.nodes.createNode(this, config)
 | |
| 
 | |
| 		const ts3Config = RED.nodes.getNode(config.configid) as Ts3ConfigNode
 | |
| 
 | |
| 		// HACK: ts3Config.on("connected") would not work because i dont know how to use typescript
 | |
| 		ts3Config.addListener("connected",()=>{
 | |
| 			this.status({fill:"green",shape:"dot",text:"Connected"})
 | |
| 		})
 | |
| 
 | |
| 		ts3Config.addListener("disconnect",()=>{
 | |
| 			this.status({fill:"grey",shape:"dot",text:"not conncted"})
 | |
| 		})
 | |
| 
 | |
| 		ts3Config.addListener("error",()=>{
 | |
| 			this.status({fill:"red",shape:"dot",text:"Error"})
 | |
| 		})
 | |
| 
 | |
| 		this.on("input", async (msg,send,done) => {
 | |
| 			const client = await ts3Config.getConnection()
 | |
| 
 | |
| 			const member: any = client[config.selection as keyof TeamSpeak]
 | |
| 			if (typeof member === "function"){
 | |
| 				let args: any[] = []
 | |
| 
 | |
| 				if (Array.isArray(msg.payload)){
 | |
| 					args = msg.payload
 | |
| 				}else{
 | |
| 					args = [msg.payload]
 | |
| 				}
 | |
| 
 | |
| 				try {
 | |
| 					msg.payload = await member.apply(client,args)
 | |
| 				} catch (err) {
 | |
| 					this.error(`Failed to call ${config.selection}: ${err}`)
 | |
| 				}
 | |
| 
 | |
| 				send(msg)
 | |
| 			}else{
 | |
| 				this.error(`Failed to call ${config.selection}. Not a function`)
 | |
| 			}
 | |
| 
 | |
| 			done()
 | |
| 		})
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	RED.nodes.registerType("TS3Call", Ts3Call)
 | |
| }
 | |
| 
 | |
| export = nodeInit;
 |