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:
11 files changed
+452
-8
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { ChannelType, REST, Routes } from 'discord.js';
|
||||
import { ACCESS_LABELS, ACCESS_LEVELS } from '../../config/constants.js';
|
||||
import { env } from '../../config/env.js';
|
||||
import { readSetting, writeSetting } from '../../config/guild-settings.js';
|
||||
import { ExternalApiError } from '../../core/errors.js';
|
||||
import { createLogger } from '../../core/logger.js';
|
||||
import { canActOn, getAccessLevel } from '../../core/permissions.js';
|
||||
@@ -16,6 +17,7 @@ import {
|
||||
setNotificationChannel,
|
||||
} from '../../database/repositories/notification-repository.js';
|
||||
import { moderationLabel, sendModerationLog } from '../../services/moderation/moderation-service.js';
|
||||
import { publishRules, rulesStatus } from '../../services/rules/rules-service.js';
|
||||
import { badRequest, forbidden, notFound } from '../http.js';
|
||||
|
||||
/** Discord server management from the dashboard (M16-T05). */
|
||||
@@ -292,6 +294,39 @@ export function registerDiscordRoutes(router) {
|
||||
{ access: ACCESS_LEVELS.STAFF },
|
||||
);
|
||||
|
||||
// --- Server rules ----------------------------------------------------------
|
||||
|
||||
router.get(
|
||||
'/api/discord/rules',
|
||||
() => ({ markdown: readSetting('rules.markdown', guildId()), ...rulesStatus(guildId()) }),
|
||||
{ access: ACCESS_LEVELS.STAFF },
|
||||
);
|
||||
|
||||
/** Publishes the rules, replacing the message the bot posted last time. */
|
||||
router.post(
|
||||
'/api/discord/rules',
|
||||
async ({ context, body, actor }) => {
|
||||
const markdown = writeSetting('rules.markdown', body.markdown, {
|
||||
guildId: guildId(),
|
||||
updatedBy: actor.userId,
|
||||
});
|
||||
|
||||
try {
|
||||
const published = await publishRules(context.client, {
|
||||
guildId: guildId(),
|
||||
markdown,
|
||||
actorId: actor.userId,
|
||||
});
|
||||
return { ok: true, ...published, ...rulesStatus(guildId()) };
|
||||
} catch (error) {
|
||||
// The text is saved either way: a channel that is simply not configured
|
||||
// yet must not throw away what was written.
|
||||
throw badRequest(error.message);
|
||||
}
|
||||
},
|
||||
{ access: ACCESS_LEVELS.DEV },
|
||||
);
|
||||
|
||||
// --- Notification routing --------------------------------------------------
|
||||
router.get(
|
||||
'/api/discord/notifications',
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
stat,
|
||||
table,
|
||||
toast,
|
||||
textarea,
|
||||
toggle,
|
||||
} from '../core/ui.js';
|
||||
|
||||
@@ -76,7 +77,11 @@ function settingControl(setting, value, apply) {
|
||||
|
||||
function behaviourCard(data, level, reload) {
|
||||
const editable = level >= LEVELS.DEV;
|
||||
const schema = Array.isArray(data.schema) ? data.schema : [];
|
||||
// The rules get their own card: a plain text field would not carry the
|
||||
// markdown editor nor the publishing button.
|
||||
const schema = (Array.isArray(data.schema) ? data.schema : []).filter(
|
||||
(setting) => setting.key !== 'rules.markdown',
|
||||
);
|
||||
const values = data.values ?? {};
|
||||
|
||||
// Falling back to one section keeps the page useful against an API that does
|
||||
@@ -492,6 +497,88 @@ function referenceCard(data) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the rules in markdown and republishes them: the previous message is
|
||||
* deleted, the new one is sent and pinned in `CHANNEL_RULES_ID`.
|
||||
*/
|
||||
function rulesCard(level, reload) {
|
||||
const host = h('div', { class: 'stack' });
|
||||
const editable = level >= LEVELS.DEV;
|
||||
|
||||
renderAsync(
|
||||
host,
|
||||
() => api.get('/api/discord/rules'),
|
||||
(data) => {
|
||||
const editor = textarea({
|
||||
value: data.markdown ?? '',
|
||||
placeholder: '## Règlement\n\n1. …\n2. …',
|
||||
rows: 12,
|
||||
maxlength: 4000,
|
||||
disabled: !editable,
|
||||
});
|
||||
const counter = h('span', { class: 'field-help' });
|
||||
const refresh = () => {
|
||||
counter.textContent = `${editor.value.length} / 4000 caractères`;
|
||||
};
|
||||
editor.addEventListener('input', refresh);
|
||||
refresh();
|
||||
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'stack' },
|
||||
!data.configured &&
|
||||
notice(
|
||||
'Aucun salon des règles dans le .env : renseigne `CHANNEL_RULES_ID` puis redémarre le bot.',
|
||||
'error',
|
||||
),
|
||||
field('Règlement (markdown)', editor, {
|
||||
help: 'Markdown de Discord : **gras**, *italique*, `code`, listes, titres. Publié et épinglé dans le salon des règles.',
|
||||
}),
|
||||
counter,
|
||||
h(
|
||||
'div',
|
||||
{ class: 'row' },
|
||||
editable &&
|
||||
button(
|
||||
'Publier le règlement',
|
||||
async (event) => {
|
||||
// The button stays disabled until the answer arrives: a second
|
||||
// click would publish the rules twice.
|
||||
event.currentTarget.disabled = true;
|
||||
try {
|
||||
const result = await api.post('/api/discord/rules', { markdown: editor.value });
|
||||
toast(
|
||||
result.pinned
|
||||
? 'Règlement publié et épinglé.'
|
||||
: 'Règlement publié, mais l’épinglage a échoué.',
|
||||
result.pinned ? 'success' : 'info',
|
||||
);
|
||||
reload();
|
||||
} catch (error) {
|
||||
toast(error.message, 'error');
|
||||
event.currentTarget.disabled = false;
|
||||
}
|
||||
},
|
||||
{ tone: 'primary', disabled: !data.configured },
|
||||
),
|
||||
data.publishedAt
|
||||
? h('span', {
|
||||
class: 'muted',
|
||||
text: `Dernier règlement publié ${formatRelative(data.publishedAt)}.`,
|
||||
})
|
||||
: h('span', { class: 'muted', text: 'Aucun règlement publié pour l’instant.' }),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return card(
|
||||
'Règlement du serveur',
|
||||
{ subtitle: 'Un seul message, épinglé dans le salon des règles, remis à jour à chaque publication.' },
|
||||
host,
|
||||
);
|
||||
}
|
||||
|
||||
export function render(host, { state }) {
|
||||
const level = state.me.accessLevel;
|
||||
const reload = () => render(host, { state });
|
||||
@@ -532,6 +619,7 @@ export function render(host, { state }) {
|
||||
),
|
||||
|
||||
behaviourCard(data, level, reload),
|
||||
rulesCard(level, reload),
|
||||
connectionsCard(data),
|
||||
level >= LEVELS.OWNER ? sessionsCard(reload) : null,
|
||||
level >= LEVELS.OWNER ? tokensCard(reload) : null,
|
||||
|
||||
Reference in new issue
Block a user