feat(tickets): open tickets from a panel button in any channel
CI / quality (push) Has been cancelled
CI / quality (push) Has been cancelled
Members had to know `/ticket create` and its category list to reach support, so the entry point is now a panel: `/ticket setup` publishes it in the current channel, the button opens a menu of the categories the member is allowed to open, and the choice creates the ticket. The panel channel and message are stored in `ticket_panels`, so a reinstall replaces the previous message instead of leaving the server two entry points, and the panel survives a restart.
This commit is contained in:
9 files changed
+492
-29
No files matched your search
+165
-3
@@ -15,7 +15,9 @@ process.env.ROLE_STAFF_ID = 'role-staff';
|
||||
|
||||
const { closeDatabase, getDatabase, initDatabase } = await import('../src/database/index.js');
|
||||
const { UserFacingError } = await import('../src/core/errors.js');
|
||||
const { createTicket } = await import('../src/services/tickets/ticket-service.js');
|
||||
const { getTicketPanel } = await import('../src/database/repositories/ticket-repository.js');
|
||||
const { createTicket, handleTicketComponent, removePanel, setupPanel } =
|
||||
await import('../src/services/tickets/ticket-service.js');
|
||||
|
||||
const CATEGORY_ID = process.env.CATEGORY_TICKETS_ID;
|
||||
const LOGS_CHANNEL = process.env.CHANNEL_LOGS_ID;
|
||||
@@ -29,12 +31,42 @@ const member = (roleIds = []) => ({
|
||||
|
||||
/** Discord reduced to what opening a ticket touches: the category and the new channel. */
|
||||
function harness({ parent = TICKET_CATEGORY } = {}) {
|
||||
const state = { created: [], channelMessages: [], journal: [], fetched: [] };
|
||||
const state = {
|
||||
channelMessages: [],
|
||||
created: [],
|
||||
deleted: [],
|
||||
fetched: [],
|
||||
journal: [],
|
||||
pinned: [],
|
||||
replies: [],
|
||||
};
|
||||
|
||||
const journalChannel = {
|
||||
isSendable: () => true,
|
||||
send: async (payload) => state.journal.push(payload),
|
||||
};
|
||||
const panelChannel = {
|
||||
id: 'panel-1',
|
||||
isTextBased: () => true,
|
||||
isThread: () => false,
|
||||
send: async (payload) => {
|
||||
const message = {
|
||||
id: `panel-message-${state.channelMessages.length + 1}`,
|
||||
pin: async () => {
|
||||
state.pinned.push('panel');
|
||||
},
|
||||
delete: async () => {
|
||||
state.deleted.push(message.id);
|
||||
},
|
||||
};
|
||||
state.channelMessages.push({ ...payload, id: message.id });
|
||||
return message;
|
||||
},
|
||||
messages: {
|
||||
fetch: async (id) => ({ id, delete: async () => state.deleted.push(id) }),
|
||||
},
|
||||
};
|
||||
|
||||
const channel = {
|
||||
id: 'channel-1',
|
||||
send: async (payload) => state.channelMessages.push(payload),
|
||||
@@ -60,12 +92,16 @@ function harness({ parent = TICKET_CATEGORY } = {}) {
|
||||
|
||||
const interaction = {
|
||||
guild,
|
||||
guildId: 'guild-1',
|
||||
client: { user: { id: 'bot-1' }, channels: guild.channels },
|
||||
user: { id: 'author-1', username: 'Léa', tag: 'Léa#0001' },
|
||||
member: member([process.env.ROLE_STAFF_ID]),
|
||||
reply: async (payload) => state.replies.push(payload),
|
||||
deferReply: async () => {},
|
||||
editReply: async (payload) => state.replies.push(payload),
|
||||
};
|
||||
|
||||
return { state, interaction, journalChannel };
|
||||
return { state, interaction, journalChannel, panelChannel };
|
||||
}
|
||||
|
||||
const open = (harness) => createTicket(harness.interaction, 'bug');
|
||||
@@ -137,3 +173,129 @@ describe('ticket creation', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ticket panel', () => {
|
||||
before(() => initDatabase(':memory:'));
|
||||
after(() => closeDatabase());
|
||||
beforeEach(() => getDatabase().exec('DELETE FROM tickets; DELETE FROM ticket_panels;'));
|
||||
|
||||
const panelInteraction = (test) => ({ ...test.interaction, channel: test.panelChannel });
|
||||
|
||||
const panelMessage = (test) => test.state.channelMessages.at(-1);
|
||||
|
||||
it('publishes and pins the panel, then remembers it', async () => {
|
||||
const test = harness();
|
||||
|
||||
await setupPanel(panelInteraction(test));
|
||||
|
||||
const message = panelMessage(test);
|
||||
assert.match(message.embeds[0].toJSON().title, /Besoin d’aide/);
|
||||
assert.deepEqual(test.state.pinned, ['panel']);
|
||||
const panel = getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' });
|
||||
assert.equal(panel.message_id, message.id);
|
||||
assert.equal(panel.created_by, 'author-1');
|
||||
assert.match(test.state.replies.at(-1).embeds[0].toJSON().description, /Panneau publié et épinglé/);
|
||||
});
|
||||
|
||||
it('replaces the previous panel instead of stacking a second one', async () => {
|
||||
const test = harness();
|
||||
|
||||
await setupPanel(panelInteraction(test));
|
||||
await setupPanel(panelInteraction(test));
|
||||
|
||||
assert.deepEqual(test.state.deleted, ['panel-message-1']);
|
||||
assert.equal(test.state.channelMessages.length, 2);
|
||||
assert.equal(getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }).message_id, 'panel-message-2');
|
||||
});
|
||||
|
||||
it('keeps the panel when the previous message was deleted by hand', async () => {
|
||||
const test = harness();
|
||||
|
||||
await setupPanel(panelInteraction(test));
|
||||
await setupPanel(panelInteraction(test));
|
||||
|
||||
assert.equal(getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }).message_id, 'panel-message-2');
|
||||
});
|
||||
|
||||
it('refuses to install a panel when the ticket category is unusable', async () => {
|
||||
const test = harness();
|
||||
test.interaction.guild.channels.fetch = async () => null;
|
||||
|
||||
await assert.rejects(
|
||||
() => setupPanel(panelInteraction(test)),
|
||||
(error) => {
|
||||
assert.ok(error instanceof UserFacingError);
|
||||
assert.match(error.message, /CATEGORY_TICKETS_ID/);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
assert.equal(test.state.channelMessages.length, 0);
|
||||
});
|
||||
|
||||
it('removes the panel and its message', async () => {
|
||||
const test = harness();
|
||||
await setupPanel(panelInteraction(test));
|
||||
|
||||
await removePanel(panelInteraction(test));
|
||||
|
||||
assert.deepEqual(test.state.deleted, ['panel-message-1']);
|
||||
assert.equal(getTicketPanel({ guildId: 'guild-1', channelId: 'panel-1' }), null);
|
||||
});
|
||||
|
||||
it('says so when the channel has no panel', async () => {
|
||||
const test = harness();
|
||||
|
||||
await assert.rejects(
|
||||
() => removePanel(panelInteraction(test)),
|
||||
(error) => {
|
||||
assert.ok(error instanceof UserFacingError);
|
||||
assert.match(error.message, /\/ticket setup/);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('offers only the categories the member may open', async () => {
|
||||
const test = harness();
|
||||
|
||||
await handleTicketComponent({
|
||||
...panelInteraction(test),
|
||||
customId: 'ticket:panel:open',
|
||||
member: member([process.env.ROLE_STAFF_ID]),
|
||||
});
|
||||
|
||||
const [row] = test.state.replies.at(-1).components;
|
||||
const { options } = row.toJSON().components[0];
|
||||
assert.deepEqual(
|
||||
options.map((option) => option.value),
|
||||
['support', 'bug', 'report'],
|
||||
);
|
||||
});
|
||||
|
||||
it('tells a member with no category to ask in the salon', async () => {
|
||||
const test = harness();
|
||||
|
||||
await handleTicketComponent({
|
||||
...panelInteraction(test),
|
||||
customId: 'ticket:panel:open',
|
||||
member: member([]),
|
||||
});
|
||||
|
||||
const reply = test.state.replies.at(-1);
|
||||
assert.equal(reply.components, undefined);
|
||||
assert.match(reply.embeds[0].toJSON().description, /Aucune catégorie/);
|
||||
});
|
||||
|
||||
it('opens the ticket from the chosen category', async () => {
|
||||
const test = harness();
|
||||
|
||||
await handleTicketComponent({
|
||||
...panelInteraction(test),
|
||||
customId: 'ticket:panel:pick',
|
||||
values: ['bug'],
|
||||
});
|
||||
|
||||
assert.equal(test.state.created[0].parent, CATEGORY_ID);
|
||||
assert.match(test.state.replies.at(-1).embeds[0].toJSON().description, /Ton ticket est ouvert/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user