Skip to content

Commit

Permalink
Added STATUS_THRESHOLD and POLLING_INTERVAL environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bl3rune committed Jan 16, 2025
1 parent 6036e98 commit c298cec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,24 @@ When running as a docker container provide the following as Docker environment v
When running as a nodejs app you can create an `.env` file in the root directory of the project and set the options there (see the `.env.example` file for an example).
You need to set the following configuration options.

| Required | Configuration option | Default | Description | Value |
| -------- | ---------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------- | -------- |
| TRUE | `GAME_URLS` | | Comma seperated list of GameUrl format entries [see GameUrl format section](#gameurl-format) | `string` |
| TRUE | `DISCORD_TOKEN` | | The bot token of your discord app from https://discord.com/developers/applications -> (Select your application) -> Bot -> Token | `string` |
| FALSE | `DISCORD_CHANNEL` | | The channel id of your discord chat to send server availability to | `string` |
| FALSE | `UP.####` | | Message to be sent on server available for game type `####` (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `DOWN.####` | | Message to be sent on server unavailable for game type `####` (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `UP` | | Message to be sent on server available for any game when `UP.####` is not available (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `DOWN` | | Message to be sent on server unavailable for any game when `DOWN.####` is not available (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `IDLE_STATUS` | `No servers running`| Override idle status messaging for the bot when no servers are available | `string` |
| FALSE | `SEPERATOR` | ` ///// ` | Override the seperator text that appears between multiple activities | `string` |
| FALSE | `NAME_OVERRIDE.####` | | Override the activity name for game type `####` with : variable string value | `string` |
| FALSE | `NAME_FIELD.####` | | Override the activity name for game type `####` with : field on the response object (response[NAME_FIELD]) | `string` |
| FALSE | `RAW_NAME_FIELD.####` | | Override the activity name for game type `####` with : field in the raw section of the response (response.raw[RAW_NAME_FIELD]) | `string` |
| FALSE | `UDP_PORT` | | Use a fixed UDP port see https://www.npmjs.com/package/gamedig/v/4.3.1#specifying-a-listen-udp-port-override | `string` |
| FALSE | `HTTP_ENABLED` | FALSE | Enable the HTTP server that handles direct requests in the format : `/server/protocol/port` or `/server/protocol` | `boolean`|
| FALSE | `HTTP_PORT` | 80 | HTTP port to host server on and handle requests in the format : `/server/protocol/port` or `/server/protocol` | `string` |
| FALSE | `HTTP_ALLOWED_SERVERS` | | List of allowed server addresses to filter HTTP request by, seperated by a `:` e.g.`example.com:test.net` Disabled by default. | `string` |
| Required | Configuration option | Default | Description | Value |
| -------- | ---------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | -------- |
| TRUE | `GAME_URLS` | | Comma seperated list of GameUrl format entries [see GameUrl format section](#gameurl-format) | `string` |
| TRUE | `DISCORD_TOKEN` | | The bot token of your discord app from https://discord.com/developers/applications -> (Select your application) -> Bot -> Token | `string` |
| FALSE | `DISCORD_CHANNEL` | | The channel id of your discord chat to send server availability to | `string` |
| FALSE | `POLLING_INTERVAL` | 10000 | The polling interval to poll the servers in milliseconds | `number` |
| FLASE | `STATUS_THRESHOLD` | 3 | The number of consecutive times a server has to be counted as UP / DOWN before doing an announcement | `number` |
| FALSE | `UP.####` | | Message to be sent on server available for game type `####` (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `DOWN.####` | | Message to be sent on server unavailable for game type `####` (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `UP` | | Default message to be sent on server available for any game when `UP.####` is not available (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `DOWN` | | Default message to be sent on server unavailable for any game when `DOWN.####` is not available (`DISCORD_CHANNEL` must be provided) | `string` |
| FALSE | `IDLE_STATUS` | `No servers running`| Override idle status messaging for the bot when no servers are available | `string` |
| FALSE | `SEPERATOR` | ` ///// ` | Override the seperator text that appears between multiple activities | `string` |
| FALSE | `NAME_OVERRIDE.####` | | Override the activity name for game type `####` with : variable string value | `string` |
| FALSE | `NAME_FIELD.####` | | Override the activity name for game type `####` with : field on the response object (response[NAME_FIELD]) | `string` |
| FALSE | `RAW_NAME_FIELD.####` | | Override the activity name for game type `####` with : field in the raw section of the response (response.raw[RAW_NAME_FIELD]) | `string` |
| FALSE | `UDP_PORT` | | Use a fixed UDP port see https://www.npmjs.com/package/gamedig/v/4.3.1#specifying-a-listen-udp-port-override | `string` |
| FALSE | `HTTP_ENABLED` | FALSE | Enable the HTTP server that handles direct requests in the format : `/server/protocol/port` or `/server/protocol` | `boolean`|
| FALSE | `HTTP_PORT` | 80 | HTTP port to host server on and handle requests in the format : `/server/protocol/port` or `/server/protocol` | `number` |
| FALSE | `HTTP_ALLOWED_SERVERS` | | List of allowed server addresses to filter HTTP request by, seperated by a `:` e.g.`example.com:test.net` Disabled by default. | `string` |
28 changes: 22 additions & 6 deletions src/services/discord-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export class DiscordPublisher {
private client: Client;
private ready: Promise<string>;
private serverUp: Map<string,boolean>;
private statusCount: Map<string,number>;
private consecutiveStatusThreshold = process.env.STATUS_THRESHOLD ?
Number(process.env.STATUS_THRESHOLD) : 3;

constructor() {
var client = new Client({intents: [GatewayIntentBits.Guilds]});
Expand All @@ -18,6 +21,7 @@ private serverUp: Map<string,boolean>;

this.client = client;
this.serverUp = new Map<string,boolean>();
this.statusCount = new Map<string,number>();
this.ready = client.login(process.env.DISCORD_TOKEN || '');
}

Expand All @@ -43,10 +47,10 @@ private serverUp: Map<string,boolean>;

if (!results || results.length == 0) {
this.serverUp.forEach((up, game) => {
if (up) {
if (up && this.successiveStatusThresholdMet(game, false)) {
this.announce(game, false);
up = false
}
up = false
});
return this.idlePresence();
}
Expand All @@ -58,10 +62,12 @@ private serverUp: Map<string,boolean>;
for(let s of results) {
let status = s.result;

if (!status) {
if (this.serverUp.get(s.game)) {
this.announce(s.game, false);
}
if (!this.serverUp.has(s.game)) {
this.serverUp.set(s.game, status ? true : false)
}

if (!status && this.serverUp.get(s.game) && this.successiveStatusThresholdMet(s.game, false)) {
this.announce(s.game, false);
this.serverUp.set(s.game,false);
continue;
}
Expand Down Expand Up @@ -111,6 +117,16 @@ private serverUp: Map<string,boolean>;
}
}

private successiveStatusThresholdMet(game: string, serverUp: boolean) : boolean {
let count = this.statusCount.get(game) || 0;
count += serverUp ? 1 : -1;
this.statusCount.set(game, count);
if (count > this.consecutiveStatusThreshold || count < -this.consecutiveStatusThreshold) {
return true;
}
return false;
}

private buildActivityText(response: ServerResponse) : String {
let status = response.result;

Expand Down
2 changes: 1 addition & 1 deletion src/services/polling-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Observable, Subject, timer } from 'rxjs';
import { ServerResponse } from '../models/server-response';

export abstract class PollingProvider {
private interval: number = 10000;
private interval: number = process.env.POLLING_INTERVAL ? Number(process.env.POLLING_INTERVAL) : 10000;
private resultSubject = new Subject<ServerResponse[] | undefined>();

protected constructor() {
Expand Down

0 comments on commit c298cec

Please sign in to comment.