Home > Net >  Not adding to the map, Staying the same name "t-1"
Not adding to the map, Staying the same name "t-1"

Time:12-11

I'm making it so if a member says ~new, it makes a new ticket for him, but the problem is, the channel name has to go in a unique number, like t-1 then t-2 ect...

But it's sticking with "t-1" only, to be clear, I'm using a command handler.

here is my code:

module.exports = {
  name: 'new',
  category: 'Ticket',
  description: 'Creates a new ticket.',
  aliases: ['newticket'],
  usage: 'new',
  userperms: [],
  botperms: [],
  run: async (client, message, args) => {

  let ticketCounter = 1;
  const userTickets = new Map()

        if(userTickets.has(message.author.tag)) {
            return message.channel.send('<@'   message.author.id   '> you already have a ticket, please close your existing ticket first before opening a new one!').then(m => m.delete({timeout: 3000}));
        }
        
        message.guild.channels.create(`t-${ticketCounter}`, {
            permissionOverwrites: [
                {
                    id: message.author.id,
                    allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                },
                {
                    id: "916785912267034674",
                    allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                },
                {
                    id: message.guild.roles.everyone,
                    deny: ['VIEW_CHANNEL'],
                },
            ],
            type: 'text',
        }).then(async channel => {
            channel.setParent('916778691672031293');
            userTickets.set(message.author.id, ticketCounter  );
            message.channel.send(`<@`   message.author.id   `>, you have successfully created a ticket! Please click on ${channel} to view your ticket.`).then(m => m.delete({timeout: 3000}));
            channel.send(`Hi <@`   message.author.id   `>, welcome to your ticket! Please be patient, we will be with you shortly.`);
            
            const logchannel = message.guild.channels.cache.find(channel => channel.name === 'test');
            
            if(logchannel) {
                logchannel.send(`Ticket-${ticketCounter} created. Click the following to veiw <#${channel.id}>`);
            }
        });
    }
}

Thanks!

CodePudding user response:

Use <Map>.size instead of an incrementer since the code is reexecuted after each command, the counter will be set to 1 each time.

if(!client.userTickets) client.userTickets = new Map();
client.userTickets.set(message.author.id, client.userTickets.size   1);
message.guild.channels.create(`t-${client.userTickets.size   1}`, { ... });

Example for your code:

module.exports = {
  name: 'new',
  category: 'Ticket',
  description: 'Creates a new ticket.',
  aliases: ['newticket'],
  usage: 'new',
  userperms: [],
  botperms: [],
  run: async (client, message, args) => {

  if(!client.userTickets) client.userTickets = new Map()

        if(client.userTickets.has(message.author.id)) {
            return message.channel.send('<@'   message.author.id   '> you already have a ticket, please close your existing ticket first before opening a new one!').then(m => m.delete({timeout: 3000}));
        }
        
        message.guild.channels.create(`t-${client.userTickets.size   1}`, {
            permissionOverwrites: [
                {
                    id: message.author.id,
                    allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                },
                {
                    id: "916785912267034674",
                    allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                },
                {
                    id: message.guild.roles.everyone,
                    deny: ['VIEW_CHANNEL'],
                },
            ],
            type: 'text',
        }).then(async channel => {
            channel.setParent('916778691672031293');
            client.userTickets.set(message.author.id, client.userTickets.size   1);
            message.channel.send(`<@`   message.author.id   `>, you have successfully created a ticket! Please click on ${channel} to view your ticket.`).then(m => m.delete({timeout: 3000}));
            channel.send(`Hi <@`   message.author.id   `>, welcome to your ticket! Please be patient, we will be with you shortly.`);
            
            const logchannel = message.guild.channels.cache.find(channel => channel.name === 'test');
            
            if(logchannel) {
                logchannel.send(`Ticket-${client.userTickets.size} created. Click the following to veiw <#${channel.id}>`);
            }
        });
    }
}
  • Related