Files
ts3gotify/src/ts3gotify.ts
Renovate Bot 91dc361785
Some checks failed
CD / Check changes (push) Successful in 10s
CD / Build and push (amd64) (push) Has been cancelled
CD / Build and push (arm64) (push) Has been cancelled
CD / Create manifest (push) Has been cancelled
CD / Test (push) Has been cancelled
chore(deps): update docker/setup-buildx-action action to v3 (#28)
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) | action | major | `v2` -> `v3` |

---

### Release Notes

<details>
<summary>docker/setup-buildx-action (docker/setup-buildx-action)</summary>

### [`v3`](https://github.com/docker/setup-buildx-action/compare/v2...v3)

[Compare Source](https://github.com/docker/setup-buildx-action/compare/v2...v3)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yMzguMiIsInVwZGF0ZWRJblZlciI6IjM5LjIzOC4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: Timo Behrendt <t.behrendt@t00n.de>
Co-authored-by: t.behrendt <t.behrendt@noreply.localhost>
Reviewed-on: #28
Co-authored-by: Renovate Bot <renovate@t00n.de>
Co-committed-by: Renovate Bot <renovate@t00n.de>
2025-04-13 14:18:29 +02:00

107 lines
2.6 KiB
TypeScript

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>;