call ts3 functions from node

This commit is contained in:
2021-04-25 23:26:09 +02:00
parent 6d0937813b
commit 1135dc91cc
2 changed files with 15 additions and 28 deletions

View File

@@ -1,23 +0,0 @@
import { TeamSpeak } from "ts3-nodejs-library";
export default function handleGet(func: string, client: TeamSpeak, options: any): Promise<any>{
const member: any = client[func as keyof TeamSpeak]
if (typeof member === "function"){
let rtn;
if (options instanceof Array){
rtn = member(...options)
}else{
rtn = member(options)
}
if (rtn instanceof Promise){
return rtn
}else{
return rtn
}
}else{
throw new Error(`"${func}" is not a valid function name`);
}
}

View File

@@ -1,6 +1,6 @@
import { NodeInitializer } from "node-red";
import { TeamSpeak } from "ts3-nodejs-library";
import { Ts3ConfigNode } from "../ts3-config/types";
import handleGet from "./handleGet";
import { Ts3GetConfig, Ts3GetNode } from "./types";
const nodeInit: NodeInitializer = (RED): void => {
@@ -28,12 +28,22 @@ const nodeInit: NodeInitializer = (RED): void => {
this.on("input", async (msg,send,done) => {
const client = await ts3Config.getConnection()
let payload = await handleGet(config.selection,client,msg.payload)
//let payload = await client.whoami()
const member: any = client[config.selection as keyof TeamSpeak]
if (typeof member === "function"){
let args: any[] = []
msg.payload = payload
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`)
}
send(msg)
done()
})
}