The rules lived in a channel nobody but a staff member with a shell could edit, and the welcome message pointed at it. They are now written in markdown under Settings and published as a single pinned embed, and a new publication deletes the message the bot posted last time instead of piling copies up in the channel.
113 lines
4.0 KiB
JavaScript
113 lines
4.0 KiB
JavaScript
import { COLORS } from '../../config/constants.js';
|
||
import { env } from '../../config/env.js';
|
||
import { createLogger } from '../../core/logger.js';
|
||
import { logEvent } from '../../database/repositories/event-log-repository.js';
|
||
import { getRulesMessage, setRulesMessage } from '../../database/repositories/rules-repository.js';
|
||
import { baseEmbed } from '../../ui/embeds.js';
|
||
|
||
/**
|
||
* Rules of the server, written from the dashboard and published as a single
|
||
* pinned message in `CHANNEL_RULES_ID` (M19).
|
||
*
|
||
* Publishing deletes the message the bot posted last time before sending the
|
||
* new one: the channel keeps a single set of rules, and members following the
|
||
* channel see the update rather than a second copy.
|
||
*/
|
||
|
||
const log = createLogger('rules');
|
||
|
||
/** Discord refuses an embed description longer than that. */
|
||
export const RULES_MAX_LENGTH = 4000;
|
||
|
||
const TITLE = '📜 Règlement';
|
||
|
||
/**
|
||
* Replaces the published rules with `markdown`.
|
||
* @param {import('discord.js').Client} client
|
||
* @param {{ guildId: string, markdown: string, actorId?: string | null, channelId?: string }} options
|
||
* @returns {Promise<{ messageId: string, url: string | null, channelId: string, pinned: boolean }>}
|
||
*/
|
||
export async function publishRules(
|
||
client,
|
||
{ guildId, markdown, actorId = null, channelId = env.channels.rules },
|
||
) {
|
||
if (!channelId) {
|
||
throw new Error('Aucun salon des règles configuré : renseigne CHANNEL_RULES_ID dans le .env.');
|
||
}
|
||
|
||
const content = String(markdown ?? '').trim();
|
||
if (content === '') throw new Error('Le règlement est vide.');
|
||
if (content.length > RULES_MAX_LENGTH) {
|
||
throw new Error(`${RULES_MAX_LENGTH} caractères maximum pour un règlement.`);
|
||
}
|
||
|
||
const channel = await client.channels.fetch(channelId).catch(() => null);
|
||
if (!channel?.isSendable()) {
|
||
throw new Error('Salon des règles introuvable, ou le bot n’y peut pas écrire.');
|
||
}
|
||
|
||
await deletePublishedRules(client, guildId);
|
||
|
||
const embed = baseEmbed(COLORS.primary)
|
||
.setTitle(TITLE)
|
||
.setDescription(content)
|
||
.setFooter({ text: 'Mis à jour depuis le tableau de bord FWS-Bot' })
|
||
.setTimestamp(new Date());
|
||
|
||
const message = await channel.send({ embeds: [embed] });
|
||
|
||
// Pinning is a convenience, not a requirement: a bot without “Gérer les
|
||
// messages” still publishes the rules.
|
||
const pinned = await message
|
||
.pin()
|
||
.then(() => true)
|
||
.catch((error) => {
|
||
log.warn('Could not pin the rules message', error);
|
||
return false;
|
||
});
|
||
|
||
setRulesMessage({ guildId, channelId, messageId: message.id, updatedBy: actorId });
|
||
logEvent({
|
||
guildId,
|
||
type: 'web.rules.published',
|
||
actorId,
|
||
payload: { channelId, messageId: message.id, pinned },
|
||
});
|
||
log.info('Rules published from the dashboard', { channelId, messageId: message.id, actor: actorId });
|
||
|
||
return { messageId: message.id, url: message.url ?? null, channelId, pinned };
|
||
}
|
||
|
||
/** Removes the message the bot published last time, if it is still there. */
|
||
export async function deletePublishedRules(client, guildId) {
|
||
const stored = getRulesMessage(guildId);
|
||
if (!stored) return false;
|
||
|
||
const channel = await client.channels.fetch(stored.channel_id).catch(() => null);
|
||
const message = await channel?.messages?.fetch(stored.message_id).catch(() => null);
|
||
const deleted = await message
|
||
?.delete()
|
||
.then(() => true)
|
||
.catch((error) => {
|
||
// A message deleted by hand, or a channel the bot no longer sees, must
|
||
// not stop the republication.
|
||
log.warn('Could not delete the previous rules message', error);
|
||
return false;
|
||
});
|
||
|
||
if (deleted !== false) return Boolean(deleted);
|
||
return false;
|
||
}
|
||
|
||
/** What the dashboard needs to show the current state of the rules. */
|
||
export function rulesStatus(guildId, channelId = env.channels.rules) {
|
||
const stored = getRulesMessage(guildId);
|
||
return {
|
||
channelId: channelId || null,
|
||
configured: Boolean(channelId),
|
||
messageId: stored?.message_id ?? null,
|
||
publishedAt: stored?.updated_at ?? null,
|
||
publishedBy: stored?.updated_by ?? null,
|
||
};
|
||
}
|