Implement Owner Panel features and maintenance mode handling
- Introduced the Owner Panel with new routes and UI components for managing guilds, user blacklists, feature flags, and bot presence. - Added maintenance mode functionality to prevent non-owner users from executing commands during maintenance periods. - Enhanced localization with new keys for Owner Panel features in both English and German. - Updated job processing to include a presence refresh job, ensuring the bot's status is regularly updated. - Improved environment configuration to support owner user IDs for access control.
This commit is contained in:
78
apps/bot/src/presence.ts
Normal file
78
apps/bot/src/presence.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { ActivityType } from 'discord.js';
|
||||
import { PRESENCE_REDIS_KEY, BotPresenceConfigSchema, type BotPresenceConfig } from '@nexumi/shared';
|
||||
import { redis } from './redis.js';
|
||||
import { logger } from './logger.js';
|
||||
import type { BotContext } from './types.js';
|
||||
|
||||
const ACTIVITY_TYPE_MAP: Record<BotPresenceConfig['activityType'], ActivityType> = {
|
||||
Playing: ActivityType.Playing,
|
||||
Listening: ActivityType.Listening,
|
||||
Watching: ActivityType.Watching,
|
||||
Competing: ActivityType.Competing
|
||||
};
|
||||
|
||||
export async function readPresenceConfig(context: BotContext): Promise<BotPresenceConfig> {
|
||||
const cached = await redis.get(PRESENCE_REDIS_KEY);
|
||||
if (cached) {
|
||||
const parsed = BotPresenceConfigSchema.safeParse(JSON.parse(cached));
|
||||
if (parsed.success) {
|
||||
return parsed.data;
|
||||
}
|
||||
}
|
||||
|
||||
const row = await context.prisma.botPresenceConfig.upsert({
|
||||
where: { id: 'singleton' },
|
||||
create: { id: 'singleton' },
|
||||
update: {}
|
||||
});
|
||||
const rotating = Array.isArray(row.rotatingMessages)
|
||||
? row.rotatingMessages.filter((item: unknown): item is string => typeof item === 'string')
|
||||
: [];
|
||||
const config = BotPresenceConfigSchema.parse({
|
||||
status: row.status,
|
||||
activityType: row.activityType,
|
||||
activityText: row.activityText,
|
||||
rotatingMessages: rotating,
|
||||
maintenanceMode: row.maintenanceMode,
|
||||
maintenanceMessage: row.maintenanceMessage
|
||||
});
|
||||
await redis.set(PRESENCE_REDIS_KEY, JSON.stringify(config));
|
||||
return config;
|
||||
}
|
||||
|
||||
export async function applyBotPresence(context: BotContext): Promise<void> {
|
||||
const config = await readPresenceConfig(context);
|
||||
const messages =
|
||||
config.rotatingMessages.length > 0 ? config.rotatingMessages : [config.activityText];
|
||||
const index = Math.floor(Date.now() / 60_000) % messages.length;
|
||||
const text = messages[index] ?? config.activityText;
|
||||
|
||||
await context.client.user?.setPresence({
|
||||
status: config.status,
|
||||
activities: [
|
||||
{
|
||||
name: text,
|
||||
type: ACTIVITY_TYPE_MAP[config.activityType]
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
export async function isMaintenanceMode(context: BotContext): Promise<{
|
||||
enabled: boolean;
|
||||
message: string | null;
|
||||
}> {
|
||||
const config = await readPresenceConfig(context);
|
||||
return {
|
||||
enabled: config.maintenanceMode,
|
||||
message: config.maintenanceMessage
|
||||
};
|
||||
}
|
||||
|
||||
export async function runPresenceRefreshJob(context: BotContext): Promise<void> {
|
||||
try {
|
||||
await applyBotPresence(context);
|
||||
} catch (error) {
|
||||
logger.warn({ error }, 'Failed to apply bot presence');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user