import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { GuildCommand } from '../entities/guild-command.entity.js'; // This node's guild id (one guild per node). function guildId(): string { return process.env.FABRIC_BACKEND_GUILD_NODE_ID ?? 'guild'; } @Injectable() export class CommandsService { constructor( @InjectRepository(GuildCommand) private readonly repo: Repository, ) {} // Replace the whole guild-global slash-command catalog (idempotent; // the plugin re-PUTs the full set on every gateway start). async sync(commands: unknown[]): Promise<{ status: string; count: number }> { const gid = guildId(); let row = await this.repo.findOne({ where: { guildId: gid } }); if (row) { row.commands = commands; } else { row = this.repo.create({ guildId: gid, commands }); } await this.repo.save(row); return { status: 'ok', count: commands.length }; } async list(): Promise<{ commands: unknown[]; updatedAt: string | null }> { const row = await this.repo.findOne({ where: { guildId: guildId() } }); return { commands: row?.commands ?? [], updatedAt: row?.updatedAt ? row.updatedAt.toISOString() : null, }; } }