Discord.js Ticket System | Problem With Spamming Tickets

4.8k views Asked by At

I am trying to make a ticket system, but there is a problem. I want to make it when somebody opens a ticket, wait until the ticket closes, and then let them react again.

Here is my code:

const Discord = require('discord.js');
const client = new Discord.Client({
 partials: ['MESSAGE', 'USER', 'REACTION'],
});
const enmap = require('enmap');
const { token, prefix } = require('./config.json');

const settings = new enmap({
 name: 'settings',
 autoFetch: true,
 cloneLevel: 'deep',
 fetchAll: true,
});

client.on('ready', () => {
 console.log('Ticket System');
 client.user.setActivity(`Community`, { type: 'WATCHING' });
});

client.on('message', async (message) => {
 if (message.author.bot) return;
 if (message.content.indexOf(prefix) !== 0) return;

 const args = message.content
  .slice(prefix.length)
  .trim()
  .split(/ +/g);
 const command = args.shift().toLowerCase();

 if (command == 'ticket-setup') {


  if (message.author == '409327435188928526') {
   let channel = message.mentions.channels.first();
   if (!channel) return message.reply('Usage: `!ticket-setup`');

   let sent = await channel.send(
    new Discord.MessageEmbed()
     .setTitle('Ticket System')
     .setDescription(
      'If you want to buy something from us,\n react with‏‏‎ ‎‏‏‎ ‎  ‏‏‎ ‎‏‏‎ ‎to open a ticket'
     )
     .setFooter(
      'Community',
      ''
     )
     .setColor('00f8ff')
   );

   sent.react('');
   settings.set(`${message.guild.id}-ticket`, sent.id);

   message.channel.send('Ticket System Setup Done!');
  }
 }

 if (command == 'close') {
  if (!message.channel.name.includes('ticket-'))
   return message.channel.send('You cannot use that here!');
  message.channel.delete();
 }
});

client.on('messageReactionAdd', async (reaction, user) => {
 if (user.partial) await user.fetch();
 if (reaction.partial) await reaction.fetch();
 if (reaction.message.partial) await reaction.message.fetch();

 if (user.bot) return;

 let ticketid = await settings.get(`${reaction.message.guild.id}-ticket`);

 if (!ticketid) return;

 if (reaction.message.id == ticketid && reaction.emoji.name == '') {
  reaction.users.remove(user);

  reaction.message.guild.channels
   .create(`ticket-${user.username}`, {
    permissionOverwrites: [
     {
      id: user.id,
      allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
     },
     {
      id: reaction.message.guild.roles.everyone,
      deny: ['VIEW_CHANNEL'],
     },
    ],
    type: 'text',
   })
   .then(async (channel) => {
    channel.send(
     `<@${user.id}> Welcome!`,
     new Discord.MessageEmbed()
      .setDescription(
       'Support will be with you shortly.\n \n !close to close the ticket.'
      )
      .setColor('00f8ff')
      .setFooter(
       'Community',
       ''
      )
      .setTimestamp()
    );
   });
 }
});

client.login(token);

Also, instead of having to use a command to close the ticket, I want the bot to add a yes and no reaction to the message, and ask me if I want to close it.

0

There are 0 answers