feat(rules): write the rules from the dashboard and republish them

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.
This commit is contained in:
BlackAngelTV committed 2026-09-25 20:44:29 +02:00
1 parent 20ffbbacad
commit 31c0fc861a
11 files changed
+452 -8

No files matched your search

+28 -1
View File
@@ -11,6 +11,11 @@ import {
removeNotificationChannel,
setNotificationChannel,
} from '../src/database/repositories/notification-repository.js';
import {
clearRulesMessage,
getRulesMessage,
setRulesMessage,
} from '../src/database/repositories/rules-repository.js';
import {
castVote,
closePoll,
@@ -29,7 +34,7 @@ import {
const reset = () =>
getDatabase().exec(
'DELETE FROM tickets; DELETE FROM moderation_actions; DELETE FROM polls; DELETE FROM poll_votes; DELETE FROM notification_channels;',
'DELETE FROM tickets; DELETE FROM moderation_actions; DELETE FROM polls; DELETE FROM poll_votes; DELETE FROM notification_channels; DELETE FROM rules_messages;',
);
describe('ticket repository', () => {
@@ -187,3 +192,25 @@ describe('notification repository', () => {
assert.equal(removeNotificationChannel('g', 'release'), 0);
});
});
describe('rules repository', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(reset);
it('keeps one rules message per guild and replaces it', () => {
setRulesMessage({ guildId: 'g', channelId: 'c1', messageId: 'm1', updatedBy: 'mod' });
setRulesMessage({ guildId: 'g', channelId: 'c1', messageId: 'm2', updatedBy: 'mod' });
const stored = getRulesMessage('g');
assert.equal(stored.message_id, 'm2');
assert.equal(stored.updated_by, 'mod');
assert.equal(getRulesMessage('other'), null);
});
it('forgets the message when it is cleared', () => {
setRulesMessage({ guildId: 'g', channelId: 'c1', messageId: 'm1' });
assert.equal(clearRulesMessage('g'), 1);
assert.equal(getRulesMessage('g'), null);
});
});
+125
View File
@@ -0,0 +1,125 @@
import assert from 'node:assert/strict';
import { after, before, beforeEach, describe, it } from 'node:test';
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
import { publishRules, rulesStatus } from '../src/services/rules/rules-service.js';
// The dashboard relies on `CHANNEL_RULES_ID`, which no CI run declares, so the
// channel is passed explicitly here rather than read from the environment.
const RULES_CHANNEL = '1234567890123456789';
/** A Discord client reduced to what publishing the rules touches. */
function harness({ pinFails = false } = {}) {
const state = { sent: [], deleted: [], pinned: [], messages: new Map() };
const channel = {
isSendable: () => true,
async send(payload) {
const id = `message-${state.sent.length + 1}`;
state.sent.push({ id, payload });
state.messages.set(id, {
id,
delete: async () => {
state.deleted.push(id);
state.messages.delete(id);
},
});
return {
id,
url: `https://discord.test/${id}`,
pin: async () => {
if (pinFails) throw new Error('Missing Permissions');
state.pinned.push(id);
},
};
},
messages: { fetch: async (id) => state.messages.get(id) ?? null },
};
return { state, client: { channels: { fetch: async () => channel } } };
}
const publish = (client, markdown = '## Règlement\n\n1. Reste.bg') =>
publishRules(client, {
guildId: 'g',
markdown,
actorId: 'mod',
channelId: RULES_CHANNEL,
});
const description = (entry) => entry.payload.embeds[0].toJSON().description;
describe('rules service', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(() => getDatabase().exec('DELETE FROM rules_messages;'));
it('publishes the rules and remembers the message', async () => {
const { state, client } = harness();
const result = await publish(client, '**Pas de contenu illégal.**');
assert.equal(state.sent.length, 1);
assert.equal(description(state.sent[0]), '**Pas de contenu illégal.**');
assert.deepEqual(state.pinned, ['message-1']);
assert.equal(result.pinned, true);
assert.equal(rulesStatus('g').messageId, 'message-1');
});
it('deletes the previous message instead of piling copies up', async () => {
const { state, client } = harness();
await publish(client, 'Version 1');
await publish(client, 'Version 2');
assert.deepEqual(state.deleted, ['message-1']);
assert.deepEqual(state.sent.map(description), ['Version 1', 'Version 2']);
assert.equal(rulesStatus('g').messageId, 'message-2');
});
it('publishes even when the previous message was deleted by hand', async () => {
const { state, client } = harness();
await publish(client, 'Version 1');
state.messages.delete('message-1');
const result = await publish(client, 'Version 2');
assert.equal(result.messageId, 'message-2');
});
it('publishes even when the bot cannot pin', async () => {
const { state, client } = harness({ pinFails: true });
const result = await publish(client);
assert.deepEqual(state.pinned, []);
assert.equal(result.pinned, false);
assert.equal(rulesStatus('g').messageId, 'message-1');
});
it('refuses an empty or oversized regulation', async () => {
const { client } = harness();
await assert.rejects(() => publish(client, ' '), /vide/);
await assert.rejects(() => publish(client, 'a'.repeat(4001)), /4000 caractères/);
});
it('refuses to publish without a rules channel', async () => {
const { client } = harness();
await assert.rejects(
() => publishRules(client, { guildId: 'g', markdown: 'Règlement', channelId: '' }),
/CHANNEL_RULES_ID/,
);
});
it('reports the configured channel to the dashboard', () => {
assert.deepEqual(rulesStatus('g', RULES_CHANNEL), {
channelId: RULES_CHANNEL,
configured: true,
messageId: null,
publishedAt: null,
publishedBy: null,
});
});
});