Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
How to make it give every ticket a unique number?, Discord.js

How it works:

I'm trying to make it so if someone reacts to the message that is sent by a staff member, it opens a ticket.

The problem:

I wanted to use the id of the member, but it's too big. I tried to use the name of the player, it doesn't find it as a channel for some reason, maybe a unique number is better?

I'm using a command handling system, so you won't see any of the basic things (E.g. client.login)

Here is my code:

const { Discord } = require('discord.js');
const axios = require("axios")

module.exports = {
    name: 'thing',
    category: 'Owner',
    aliases: ["t"],
    description: 'thing command.',
    usage: 'thing',
    userperms: [],
    botperms: [],
    run: async (client, message, args) => {
message.channel.send('Click "⚔" to open a ticket!').then(function(message) {
message.react('⚔');
const filter = (reaction, user) => {
    return ['⚔'].includes(reaction.emoji.name) && user.id === message.author.id;
}});

client.on('messageReactionAdd', (reaction, ruser) => {
if (!ruser.bot) {
if (reaction.emoji.name == '⚔') {
        if(message.guild.channels.cache.find(channel => channel.name == (`t-${ruser.id}`))) {
            return message.channel.send('<@' + ruser.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-${ruser.id}`, {
            permissionOverwrites: [
                {
                    id: message.author.id,
                    allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
                },
                {
                    id: message.guild.roles.everyone,
                    deny: ['VIEW_CHANNEL'],
                },
            ],
            type: 'text',
        }).then(async channel => {
            message.channel.send(`<@` + ruser.id + `>, you have successfully created a ticket! Please click on ${channel} to view your ticket.`)
      .then(m => m.delete({timeout: 3000}));
            channel.send(`Hi <@` + ruser.id + `>, welcome to your ticket! Please be patient, we will be with you shortly.`);
            const logchannel = message.guild.channels.cache.find(channel => channel.name === 'server-logs');
            if(logchannel) {
                logchannel.send(`Ticket-${ruser.username} created. Click the following to veiw <#${channel.id}>`);
            }
        });
    }
  }});

}}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are multiple ways of doing this. I would create a <Map> object whose keys should represent user ID's and values the ticket ID that belongs to that user. I would also declare a variable ticketID that would be incremented by 1 everytime a new ticket was created. On each reaction, the bot should check if the user ID is an existing key of the map, and if not, a new entry should created in it:

(...)
let ticketCounter = 0;
const userTickets = new Map();

client.on('messageReactionAdd', (reaction, ruser) => {
    if(!ruser.bot) {
        if(reaction.emoji.name == '⚔') {
            if(userTickets.has(ruser.id)) {
                return message.channel.send('<@' + ruser.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: message.guild.roles.everyone,
                        deny: ['VIEW_CHANNEL'],
                    },
                ],
                type: 'text',
            }).then(async channel => {
                userTickets.set(ruser.id, ticketCounter++); // This will create the map entry whose value is the previous ticketCounter value, the ++ increments afterwards.
                
                message.channel.send(`<@` + ruser.id + `>, you have successfully created a ticket! Please click on ${channel} to view your ticket.`).then(m => m.delete({timeout: 3000}));
                channel.send(`Hi <@` + ruser.id + `>, welcome to your ticket! Please be patient, we will be with you shortly.`);
                const logchannel = message.guild.channels.cache.find(channel => channel.name === 'server-logs');
                
                if(logchannel) {
                    logchannel.send(`Ticket-${ruser.username} created. Click the following to veiw <#${channel.id}>`);
                }
            });
        }
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!