From 740a3cdd00729d038293b6d31368825f8f5b31d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 13:56:16 +0100 Subject: [PATCH] Implement elo and register commands --- app.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/app.ts b/app.ts index 2804a91..a9c90c5 100644 --- a/app.ts +++ b/app.ts @@ -2,6 +2,8 @@ import { TeamSpeak, QueryProtocol } from "ts3-nodejs-library"; import { GraphQLClient, gql } from "graphql-request"; import dotenv from "dotenv"; +type Variables = { [key: string]: any }; + dotenv.config(); const teamspeak = new TeamSpeak({ @@ -34,6 +36,12 @@ teamspeak.on("textmessage", (ev) => { case "!hello": message(clid, "Hello " + ev.invoker.nickname); break; + case "!elo": + elo(clid, teamspeakID); + break; + case "!register": + register(clid, teamspeakID, argv[1]); + break; } }); @@ -41,12 +49,68 @@ function message(clid: string, msg: string) { teamspeak.sendTextMessage(clid, 1, msg); } -async function request(query: string): Promise { +async function request(query: string, variables: Variables): Promise { const endpoint = process.env.ENDPOINT!; const graphQLClient = new GraphQLClient(endpoint, { headers: {}, }); - const data = await graphQLClient.request(query); + const data = await graphQLClient.request(query, variables); return data; } + +async function elo(clid: string, teamspeakID: string) { + const query = gql` + query ($teamspeakID: String!) { + userByTS(teamspeakID: $teamspeakID) { + elo + } + } + `; + const variables = { teamspeakID: teamspeakID }; + + try { + const data = await request(query, variables); + message(clid, "Elo: " + data.userByTS.elo); + } catch (e) { + const error = JSON.parse(JSON.stringify(e)).response.errors[0].message; + switch (error) { + case "no result": + message(clid, "You are not registered with this teamspeakID."); + break; + default: + console.error(error); + break; + } + } +} + +async function register(clid: string, teamspeakID: string, steamID: string) { + const mutation = gql` + mutation ($steamID: String!, $teamspeakID: String!) { + createUser(input: { steamID: $steamID, teamspeakID: $teamspeakID }) + } + `; + const variables = { teamspeakID: teamspeakID, steamID: steamID }; + + try { + const data = await request(mutation, variables); + message(clid, "Registered successfully."); + } catch (e) { + const error = JSON.parse(JSON.stringify(e)).response.errors[0].message; + switch (error) { + case "not unique": + message( + clid, + "You are already registered with this teamspeakID or steamID. Please contact an administrator if you believe that this is an error." + ); + break; + case "wrong steamID64": + message(clid, "You entered a wrong steamID64."); + break; + default: + console.error(error); + break; + } + } +} -- 2.44.0