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