Files
node-red-contrib-teamspeak/src/nodes/ts3-call/ts3-call.ts
2021-04-26 20:34:01 +02:00

55 lines
1.4 KiB
TypeScript

import { NodeInitializer } from "node-red";
import { TeamSpeak } from "ts3-nodejs-library";
import { Ts3ConfigNode } from "../ts3-config/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 (msg.payload instanceof Array){
args = msg.payload
}else{
args = [msg.payload]
}
msg.payload = await member.apply(client,args)
send(msg)
}else{
this.error(`Failed to call ${config.selection}. Not a function`)
}
done()
})
}
RED.nodes.registerType("ts3-call", Ts3Call);
};
export = nodeInit;