Enhance bot and WebUI functionality with new features and environment updates
- Added support for new environment variables in `.env.example` for public site and legal operator information. - Integrated core commands into the bot's command structure for improved functionality. - Implemented a new `publishBotStatus` function to update bot status in Redis, enhancing monitoring capabilities. - Updated the landing page to include features, invite links, and support server access, improving user experience. - Enhanced localization with new keys for core commands and landing page elements in both English and German. - Improved error handling and logging for bot presence updates and status publishing.
This commit is contained in:
@@ -1,9 +1,87 @@
|
||||
import { ActivityType } from 'discord.js';
|
||||
import { PRESENCE_REDIS_KEY, BotPresenceConfigSchema, type BotPresenceConfig } from '@nexumi/shared';
|
||||
import { ActivityType, Status } from 'discord.js';
|
||||
import {
|
||||
BOT_STATUS_REDIS_KEY,
|
||||
PRESENCE_REDIS_KEY,
|
||||
BotPresenceConfigSchema,
|
||||
BotStatusPayloadSchema,
|
||||
type BotPresenceConfig
|
||||
} from '@nexumi/shared';
|
||||
import { redis } from './redis.js';
|
||||
import { logger } from './logger.js';
|
||||
import type { BotContext } from './types.js';
|
||||
|
||||
const BOT_STATUS_SHARDS_HASH = 'bot:status:shards';
|
||||
const BOT_STATUS_TTL_SECONDS = 120;
|
||||
|
||||
function shardStatusLabel(wsStatus: number): string {
|
||||
switch (wsStatus) {
|
||||
case Status.Ready:
|
||||
return 'ready';
|
||||
case Status.Connecting:
|
||||
return 'connecting';
|
||||
case Status.Reconnecting:
|
||||
return 'reconnecting';
|
||||
case Status.Idle:
|
||||
return 'idle';
|
||||
case Status.Nearly:
|
||||
return 'nearly';
|
||||
case Status.Disconnected:
|
||||
return 'disconnected';
|
||||
case Status.WaitingForGuilds:
|
||||
return 'waiting_for_guilds';
|
||||
case Status.Identifying:
|
||||
return 'identifying';
|
||||
case Status.Resuming:
|
||||
return 'resuming';
|
||||
default:
|
||||
return 'unknown';
|
||||
}
|
||||
}
|
||||
|
||||
export async function publishBotStatus(context: BotContext): Promise<void> {
|
||||
const shardId = context.client.shard?.ids[0] ?? 0;
|
||||
const shardEntry = {
|
||||
id: shardId,
|
||||
status: shardStatusLabel(context.client.ws.status),
|
||||
guildCount: context.client.guilds.cache.size,
|
||||
ping: context.client.ws.ping
|
||||
};
|
||||
|
||||
await redis.hset(BOT_STATUS_SHARDS_HASH, String(shardId), JSON.stringify(shardEntry));
|
||||
await redis.expire(BOT_STATUS_SHARDS_HASH, BOT_STATUS_TTL_SECONDS);
|
||||
|
||||
const all = await redis.hgetall(BOT_STATUS_SHARDS_HASH);
|
||||
const shards = Object.values(all)
|
||||
.map((raw) => {
|
||||
try {
|
||||
return JSON.parse(raw) as {
|
||||
id: number;
|
||||
status: string;
|
||||
guildCount: number;
|
||||
ping: number;
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter((entry): entry is NonNullable<typeof entry> => entry !== null)
|
||||
.sort((a, b) => a.id - b.id);
|
||||
|
||||
if (shards.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = BotStatusPayloadSchema.parse({
|
||||
shards,
|
||||
apiLatencyMs: Math.round(shards.reduce((sum, shard) => sum + shard.ping, 0) / shards.length),
|
||||
guildCount: shards.reduce((sum, shard) => sum + shard.guildCount, 0),
|
||||
uptimeSeconds: Math.floor(process.uptime()),
|
||||
updatedAt: new Date().toISOString()
|
||||
});
|
||||
|
||||
await redis.set(BOT_STATUS_REDIS_KEY, JSON.stringify(payload), 'EX', BOT_STATUS_TTL_SECONDS);
|
||||
}
|
||||
|
||||
const ACTIVITY_TYPE_MAP: Record<BotPresenceConfig['activityType'], ActivityType> = {
|
||||
Playing: ActivityType.Playing,
|
||||
Listening: ActivityType.Listening,
|
||||
@@ -75,4 +153,9 @@ export async function runPresenceRefreshJob(context: BotContext): Promise<void>
|
||||
} catch (error) {
|
||||
logger.warn({ error }, 'Failed to apply bot presence');
|
||||
}
|
||||
try {
|
||||
await publishBotStatus(context);
|
||||
} catch (error) {
|
||||
logger.warn({ error }, 'Failed to publish bot status');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user