~xenrox/10man-ts3

0476622fa53ec6cafceb6a09644bdf9ac2527e1b — Thorben Günther 2 years ago c601888
Add function to manipulate user data

For now only elo is possible.
1 files changed, 80 insertions(+), 0 deletions(-)

M app.ts
M app.ts => app.ts +80 -0
@@ 31,6 31,7 @@ let commands = new Map([
let admin_commands = new Map([
    ["!cancelMatch", "Cancel the ongoing match"],
    ["!finish <WINNER>", "Finish the ongoing match with WINNER (TEAM1, TEAM2)"],
    ["!set <STEAMID64> <ATTRIBUTE> <VALUE>", "Set ATTRIBUTE (elo) to VALUE"],
]);

teamspeak.on("ready", async () => {


@@ 71,6 72,9 @@ teamspeak.on("textmessage", (ev) => {
        case "!finish":
            finish_match(clid, teamspeakID, argv[1]);
            break;
        case "!set":
            set(clid, teamspeakID, argv[1], argv[2], argv[3]);
            break;
        case "!help":
            help(clid, teamspeakID);
            break;


@@ 101,6 105,34 @@ async function request(query: string, variables?: Variables): Promise<any> {
    return data;
}

async function get_user_id(clid: string, steamID64: string): Promise<bigint> {
    const query = gql`
        query ($steamID: String!) {
            userBySteam(steamID: $steamID) {
                id
            }
        }
    `;
    const variables = { steamID: steamID64 };

    try {
        const data = await request(query, variables);
        const id = data.userBySteam.id;
        return id;
    } catch (e) {
        const error = get_error(e);
        switch (error) {
            case "no result":
                message(clid, "This user is not registered.");
                break;
            default:
                console.error(error);
                break;
        }
        return -1n;
    }
}

async function is_admin(clid: string, teamspeakID: string): Promise<boolean> {
    const query = gql`
        query ($teamspeakID: String!) {


@@ 421,3 453,51 @@ async function help(clid: string, teamspeakID: string) {

    teamspeak.sendTextMessage(clid, 1, msg);
}

async function set(
    clid: string,
    teamspeakID: string,
    steamID64: string,
    attribute: string,
    value: string
) {
    const admin = await is_admin(clid, teamspeakID);
    if (!admin) {
        message(clid, "Only an admin can do that.");
        return;
    }

    switch (attribute) {
        case "elo":
            set_elo(clid, steamID64, value);
            break;
        default:
            message(clid, "Attribute not found.");
            break;
    }
}

async function set_elo(clid: string, steamID64: string, elo: string) {
    const id = await get_user_id(clid, steamID64);
    if (id == -1n) {
        return;
    }

    const mutation = gql`
        mutation ($id: Int!, $elo: Int!) {
            updateUser(id: $id, input: { elo: $elo }) {
                elo
            }
        }
    `;

    try {
        // converting can fail, so put it in the try block
        const variables = { id: id, elo: Number(BigInt(elo)) };
        const data = await request(mutation, variables);
        message(clid, "Elo updated successfully.");
    } catch (e) {
        console.error(e);
        message(clid, "Elo update failed.");
    }
}