refactor: application structure (#11)
Reviewed-on: #11 Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #11.
This commit is contained in:
106
src/ts3gotify.ts
Normal file
106
src/ts3gotify.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import type { Gotify } from "gotify";
|
||||
import type { Logger } from "pino";
|
||||
import type { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library";
|
||||
import type { GotifyConfig, Mode } from "./types";
|
||||
import {
|
||||
ClientConnect,
|
||||
ClientDisconnect,
|
||||
ClientMoved,
|
||||
TextMessage,
|
||||
} from "ts3-nodejs-library/lib/types/Events";
|
||||
|
||||
function resolveMessageTarget(target: TextMessageTargetMode): string {
|
||||
if (target === 1) {
|
||||
return "Client";
|
||||
} else if (target === 2) {
|
||||
return "Channel";
|
||||
} else {
|
||||
return "Server";
|
||||
}
|
||||
}
|
||||
|
||||
export function getModes(mode: Mode[]): {
|
||||
[key in Mode]: boolean;
|
||||
} {
|
||||
const modes = mode
|
||||
.map((mode) => {
|
||||
return { [mode]: true };
|
||||
})
|
||||
.reduce((acc, cur) => {
|
||||
return { ...acc, ...cur };
|
||||
});
|
||||
|
||||
return {
|
||||
connect: false,
|
||||
disconnect: false,
|
||||
moved: false,
|
||||
message: false,
|
||||
...modes,
|
||||
};
|
||||
}
|
||||
|
||||
export function ts3gotifyFactory(
|
||||
ts3Client: TeamSpeak,
|
||||
gotifyClient: Gotify,
|
||||
gotifyConfig: GotifyConfig,
|
||||
logger: Logger
|
||||
) {
|
||||
function sendNotification(message: string) {
|
||||
gotifyClient
|
||||
.send({
|
||||
...gotifyConfig,
|
||||
message: message,
|
||||
})
|
||||
.catch((error: Error) => {
|
||||
logger.error(`Error sending message to gotify: ${error.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
function registerEventListenerForMode(mode: Mode) {
|
||||
switch (mode) {
|
||||
case "connect":
|
||||
ts3Client.on("clientconnect", (event: ClientConnect) =>
|
||||
sendNotification(`${event.client.nickname} connected`)
|
||||
);
|
||||
break;
|
||||
case "disconnect":
|
||||
ts3Client.on("clientdisconnect", (event: ClientDisconnect) =>
|
||||
sendNotification(`${event.client?.nickname} disconnected`)
|
||||
);
|
||||
break;
|
||||
case "moved":
|
||||
ts3Client.on("clientmoved", (event: ClientMoved) =>
|
||||
sendNotification(
|
||||
`${event.client.nickname} got moved to ${event.channel.name}`
|
||||
)
|
||||
);
|
||||
break;
|
||||
case "message":
|
||||
ts3Client.on("textmessage", (event: TextMessage) =>
|
||||
sendNotification(
|
||||
`${event.invoker.nickname} wrote ${
|
||||
event.msg
|
||||
} to a ${resolveMessageTarget(event.targetmode)}`
|
||||
)
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ts3Client.on("close", async () => {
|
||||
logger.info("disconnected, trying to reconnect...");
|
||||
await ts3Client.reconnect(5, 1000);
|
||||
logger.info("reconnected!");
|
||||
});
|
||||
|
||||
ts3Client.on("error", (error: Error) => {
|
||||
logger.error(`Error connecting to TS3 server: ${error.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
return {
|
||||
registerEventListenerForMode,
|
||||
};
|
||||
}
|
||||
|
||||
export type Ts3Gotify = ReturnType<typeof ts3gotifyFactory>;
|
||||
Reference in New Issue
Block a user