From 76beea35c52f02f3080bce6f5585222d789a8d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 12:58:21 +0100 Subject: [PATCH] Refactor TeamSpeak.connect() -> TeamSpeak class Now the program structure can be better (e.g. message function). --- app.ts | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/app.ts b/app.ts index 76a84c9..2804a91 100644 --- a/app.ts +++ b/app.ts @@ -4,7 +4,7 @@ import dotenv from "dotenv"; dotenv.config(); -TeamSpeak.connect({ +const teamspeak = new TeamSpeak({ host: process.env.HOST, protocol: QueryProtocol.RAW, //optional queryport: 10011, //optional @@ -12,29 +12,34 @@ TeamSpeak.connect({ username: process.env.USERNAME, password: process.env.PASSWORD, nickname: "10Man", -}) - .then(async (teamspeak) => { - const whoami = await teamspeak.whoami(); - const channel = await teamspeak.channelFind("10 Man"); - teamspeak.clientMove(whoami.clientId, channel[0].cid); - - teamspeak.on("textmessage", (ev) => { - const argv = ev.msg.split(" "); - switch (argv[0]) { - case "!hello": - teamspeak.sendTextMessage( - ev.invoker.clid, - 1, - "Hello " + ev.invoker.nickname - ); - break; - } - }); - }) - .catch((e) => { - console.log("Catched an error!"); - console.error(e); - }); +}); + +teamspeak.on("ready", async () => { + const whoami = await teamspeak.whoami(); + const channel = await teamspeak.channelFind("10 Man"); + teamspeak.clientMove(whoami.clientId, channel[0].cid); +}); + +teamspeak.on("error", (e) => { + console.log("Caught an error!"); + console.error(e); +}); + +teamspeak.on("textmessage", (ev) => { + const argv = ev.msg.split(" "); + const clid = ev.invoker.clid; + const teamspeakID = ev.invoker.uniqueIdentifier; + + switch (argv[0]) { + case "!hello": + message(clid, "Hello " + ev.invoker.nickname); + break; + } +}); + +function message(clid: string, msg: string) { + teamspeak.sendTextMessage(clid, 1, msg); +} async function request(query: string): Promise { const endpoint = process.env.ENDPOINT!; -- 2.44.0