This commit is contained in:
179
src/app.ts
179
src/app.ts
@@ -1,107 +1,136 @@
|
||||
import { Gotify } from "gotify";
|
||||
import { QueryProtocol, TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library"
|
||||
import {
|
||||
QueryProtocol,
|
||||
TeamSpeak,
|
||||
TextMessageTargetMode,
|
||||
} from "ts3-nodejs-library";
|
||||
import { createLogger, transports, format } from "winston";
|
||||
|
||||
import { GOTIFY_TITLE, GOTIFY_TOKEN, GOTIFY_URL, LOG_LEVEL, MODE, TS3_HOST, TS3_NICKNAME, TS3_PASSWORD, TS3_QUERY_PORT, TS3_SERVER_PORT, TS3_USERNAME } from "./env";
|
||||
import {
|
||||
GOTIFY_TITLE,
|
||||
GOTIFY_TOKEN,
|
||||
GOTIFY_URL,
|
||||
LOG_LEVEL,
|
||||
MODE,
|
||||
TS3_HOST,
|
||||
TS3_NICKNAME,
|
||||
TS3_PASSWORD,
|
||||
TS3_QUERY_PORT,
|
||||
TS3_SERVER_PORT,
|
||||
TS3_USERNAME,
|
||||
} from "./env";
|
||||
import type { Mode } from "./types";
|
||||
|
||||
const logger = createLogger({
|
||||
level: LOG_LEVEL,
|
||||
transports: [new transports.Console()],
|
||||
format: format.combine(
|
||||
format.colorize(),
|
||||
format.timestamp(),
|
||||
),
|
||||
})
|
||||
level: LOG_LEVEL,
|
||||
transports: [new transports.Console()],
|
||||
format: format.combine(format.colorize(), format.timestamp()),
|
||||
});
|
||||
|
||||
const gotify = new Gotify({
|
||||
server: GOTIFY_URL,
|
||||
})
|
||||
server: GOTIFY_URL,
|
||||
});
|
||||
|
||||
const gotifyConfig = {
|
||||
app: GOTIFY_TOKEN,
|
||||
title: GOTIFY_TITLE
|
||||
}
|
||||
app: GOTIFY_TOKEN,
|
||||
title: GOTIFY_TITLE,
|
||||
};
|
||||
|
||||
function getModes() {
|
||||
const modeIsProvided = process.env.MODE != undefined
|
||||
function getModes(): {
|
||||
[key in Mode]: boolean;
|
||||
} {
|
||||
const modes = MODE.map((mode) => {
|
||||
return { [mode]: true };
|
||||
});
|
||||
|
||||
return {
|
||||
connect: MODE?.includes("connect") || false,
|
||||
disconnect: MODE?.includes("disconnect") || false,
|
||||
moved: MODE?.includes("moved") || false,
|
||||
message: MODE?.includes("message") || false
|
||||
}
|
||||
return Object.assign(
|
||||
{
|
||||
connect: false,
|
||||
disconnect: false,
|
||||
moved: false,
|
||||
message: false,
|
||||
},
|
||||
...modes
|
||||
);
|
||||
}
|
||||
|
||||
function sendNotification(message: string) {
|
||||
gotify.send({
|
||||
...gotifyConfig,
|
||||
message: message,
|
||||
}).catch((error: Error) => {
|
||||
logger.error(`Error sending message to gotify: ${error.message}`)
|
||||
gotify
|
||||
.send({
|
||||
...gotifyConfig,
|
||||
message: message,
|
||||
})
|
||||
.catch((error: Error) => {
|
||||
logger.error(`Error sending message to gotify: ${error.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
function resolveMessageTarget(target: TextMessageTargetMode): string {
|
||||
if (target === 1) {
|
||||
return "Client"
|
||||
} else if (target === 2) {
|
||||
return "Channel"
|
||||
} else {
|
||||
return "Server"
|
||||
}
|
||||
if (target === 1) {
|
||||
return "Client";
|
||||
} else if (target === 2) {
|
||||
return "Channel";
|
||||
} else {
|
||||
return "Server";
|
||||
}
|
||||
}
|
||||
|
||||
function handleMessage(message: string) {
|
||||
logger.debug(message)
|
||||
sendNotification(message)
|
||||
logger.debug(message);
|
||||
sendNotification(message);
|
||||
}
|
||||
|
||||
TeamSpeak.connect({
|
||||
host: TS3_HOST,
|
||||
queryport: TS3_QUERY_PORT,
|
||||
serverport: TS3_SERVER_PORT,
|
||||
protocol: QueryProtocol.RAW,
|
||||
username: TS3_USERNAME,
|
||||
password: TS3_PASSWORD,
|
||||
nickname: TS3_NICKNAME,
|
||||
host: TS3_HOST,
|
||||
queryport: TS3_QUERY_PORT,
|
||||
serverport: TS3_SERVER_PORT,
|
||||
protocol: QueryProtocol.RAW,
|
||||
username: TS3_USERNAME,
|
||||
password: TS3_PASSWORD,
|
||||
nickname: TS3_NICKNAME,
|
||||
}).then((teamspeak) => {
|
||||
const mode = getModes()
|
||||
const mode = getModes();
|
||||
|
||||
logger.info("connected to TS3")
|
||||
logger.info("connected to TS3");
|
||||
|
||||
if (mode.connect) {
|
||||
teamspeak.on("clientconnect", (event) => {
|
||||
handleMessage(`${event.client.nickname} connected`)
|
||||
})
|
||||
}
|
||||
if (mode.connect) {
|
||||
teamspeak.on("clientconnect", (event) => {
|
||||
handleMessage(`${event.client.nickname} connected`);
|
||||
});
|
||||
}
|
||||
|
||||
if (mode.disconnect) {
|
||||
teamspeak.on("clientdisconnect", (event) => {
|
||||
handleMessage(`${event.client?.nickname} disconnected`)
|
||||
})
|
||||
}
|
||||
if (mode.disconnect) {
|
||||
teamspeak.on("clientdisconnect", (event) => {
|
||||
handleMessage(`${event.client?.nickname} disconnected`);
|
||||
});
|
||||
}
|
||||
|
||||
if (mode.message) {
|
||||
teamspeak.on("textmessage", (event) => {
|
||||
handleMessage(`${event.invoker.nickname} wrote ${event.msg} to a ${resolveMessageTarget(event.targetmode)}`)
|
||||
})
|
||||
}
|
||||
if (mode.message) {
|
||||
teamspeak.on("textmessage", (event) => {
|
||||
handleMessage(
|
||||
`${event.invoker.nickname} wrote ${
|
||||
event.msg
|
||||
} to a ${resolveMessageTarget(event.targetmode)}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (mode.moved) {
|
||||
teamspeak.on("clientmoved", (event) => {
|
||||
handleMessage(`${event.client.nickname} got moved to ${event.channel.name}`)
|
||||
})
|
||||
}
|
||||
if (mode.moved) {
|
||||
teamspeak.on("clientmoved", (event) => {
|
||||
handleMessage(
|
||||
`${event.client.nickname} got moved to ${event.channel.name}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
teamspeak.on("close", async () => {
|
||||
logger.debug("disconnected, trying to reconnect...")
|
||||
await teamspeak.reconnect(5, 1000)
|
||||
logger.info("reconnected!")
|
||||
})
|
||||
teamspeak.on("close", async () => {
|
||||
logger.debug("disconnected, trying to reconnect...");
|
||||
await teamspeak.reconnect(5, 1000);
|
||||
logger.info("reconnected!");
|
||||
});
|
||||
|
||||
teamspeak.on("error", (error: Error) => {
|
||||
logger.error(`Error connecting to TS3 server: ${error.message}`)
|
||||
process.exit(1)
|
||||
})
|
||||
})
|
||||
teamspeak.on("error", (error: Error) => {
|
||||
logger.error(`Error connecting to TS3 server: ${error.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
|
||||
10
src/env.ts
10
src/env.ts
@@ -1,18 +1,20 @@
|
||||
import { from } from "env-var";
|
||||
|
||||
import type { LogLevel, Mode } from "./types";
|
||||
|
||||
const envVar = from(process.env, {
|
||||
asLogLevel: (value) => {
|
||||
asLogLevel: (value): LogLevel => {
|
||||
const logLevels = ["error", "info", "debug"];
|
||||
if (logLevels.includes(value)) {
|
||||
return value;
|
||||
return value as LogLevel;
|
||||
} else {
|
||||
throw new Error("Invalid log level");
|
||||
}
|
||||
},
|
||||
asTs3GotifyMode: (value) => {
|
||||
asTs3GotifyMode: (value): Mode => {
|
||||
const modes = ["connect", "disconnect", "moved", "message"];
|
||||
if (modes.includes(value)) {
|
||||
return value;
|
||||
return value as Mode;
|
||||
} else {
|
||||
throw new Error("Invalid mode");
|
||||
}
|
||||
|
||||
3
src/types.ts
Normal file
3
src/types.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export type Mode = "connect" | "disconnect" | "moved" | "message";
|
||||
|
||||
export type LogLevel = "error" | "info" | "debug";
|
||||
Reference in New Issue
Block a user