Ticket creation rate limiting + fixes

This commit is contained in:
Isaac 2022-08-06 22:50:28 +01:00
parent efe7ede041
commit ae72266b82
No known key found for this signature in database
GPG Key ID: F4EAABEB0FFCC06A
3 changed files with 44 additions and 15 deletions

View File

@ -163,6 +163,9 @@ misc:
no_categories: no_categories:
description: No ticket categories have been configured. description: No ticket categories have been configured.
title: ❌ There are no ticket categories title: ❌ There are no ticket categories
ratelimited:
description: Try again in a few seconds.
title: 🐢 Slow down
unknown_category: unknown_category:
description: Please try a different category. description: Please try a different category.
title: ❌ That ticket category doesn't exist title: ❌ That ticket category doesn't exist

View File

@ -27,7 +27,6 @@ module.exports = class TicketManager {
}) { }) {
const cacheKey = `cache/category+guild+questions:${categoryId}`; const cacheKey = `cache/category+guild+questions:${categoryId}`;
let category = await this.client.keyv.get(cacheKey); let category = await this.client.keyv.get(cacheKey);
if (!category) { if (!category) {
category = await this.client.prisma.category.findUnique({ category = await this.client.prisma.category.findUnique({
include: { include: {
@ -47,19 +46,47 @@ module.exports = class TicketManager {
}; };
} }
const getMessage = this.client.i18n.getLocale(settings.locale); const getMessage = this.client.i18n.getLocale(settings.locale);
return await interaction.reply({ const embed = new EmbedBuilder()
embeds: [
new EmbedBuilder()
.setColor(settings.errorColour) .setColor(settings.errorColour)
.setTitle(getMessage('misc.unknown_category.title')) .setTitle(getMessage('misc.unknown_category.title'))
.setDescription(getMessage('misc.unknown_category.description')) .setDescription(getMessage('misc.unknown_category.description'));
.setFooter(settings.footer), if (settings.footer) {
], embed.setFooter({
iconURL: interaction.guild?.iconURL(),
text: settings.footer,
});
}
return await interaction.reply({
embeds: [embed],
ephemeral: true,
}); });
} }
this.client.keyv.set(cacheKey, category, ms('5m')); this.client.keyv.set(cacheKey, category, ms('5m'));
} }
const getMessage = this.client.i18n.getLocale(category.guild.locale);
const rlKey = `ratelimits/guild-user:${interaction.guild.id}-${interaction.user.id}`;
const rl = await this.client.keyv.get(rlKey);
if (rl) {
const embed = new EmbedBuilder()
.setColor(category.guild.errorColour)
.setTitle(getMessage('misc.ratelimited.title'))
.setDescription(getMessage('misc.ratelimited.description'));
if (category.guild.footer) {
embed.setFooter({
iconURL: interaction.guild.iconURL(),
text: category.guild.footer,
});
}
return await interaction.reply({
embeds: [embed],
ephemeral: true,
});
} else {
this.client.keyv.set(rlKey, true, ms('10s'));
}
// TODO: if member !required roles -> stop // TODO: if member !required roles -> stop
// TODO: if discordCategory has 50 channels -> stop // TODO: if discordCategory has 50 channels -> stop
@ -70,11 +97,6 @@ module.exports = class TicketManager {
// TODO: if cooldown -> stop // TODO: if cooldown -> stop
// TODO: if 10s ratelimit -> stop
const getMessage = this.client.i18n.getLocale(category.guild.locale);
if (category.questions.length >= 1) { if (category.questions.length >= 1) {
await interaction.showModal( await interaction.showModal(
new ModalBuilder() new ModalBuilder()
@ -86,6 +108,7 @@ module.exports = class TicketManager {
.setTitle(category.name) .setTitle(category.name)
.setComponents( .setComponents(
category.questions category.questions
.filter(q => q.type === 'TEXT') // TODO: remove this when modals support select menus
.sort((a, b) => a.order - b.order) .sort((a, b) => a.order - b.order)
.map(q => { .map(q => {
if (q.type === 'TEXT') { if (q.type === 'TEXT') {

View File

@ -58,11 +58,14 @@ module.exports.post = fastify => ({
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor(settings.primaryColour) .setColor(settings.primaryColour)
.setTitle(data.title) .setTitle(data.title);
.setFooter({
if (settings.footer) {
embed.setFooter({
iconURL: guild.iconURL(), iconURL: guild.iconURL(),
text: settings.footer, text: settings.footer,
}); });
}
if (data.description) embed.setDescription(data.description); if (data.description) embed.setDescription(data.description);
if (data.image) embed.setImage(data.image); if (data.image) embed.setImage(data.image);