Home > Software engineering >  Add a number to everyticket & link new made channel in message
Add a number to everyticket & link new made channel in message

Time:01-22

So I want to make it so that every time a new ticket is made it will add a number example: ticket-1 | ticket-2 | ticket-3, ect. And then I want the bot to send the channel in a chat

module.exports = {
    data: {
        name: `GT1`
    },
    async execute(interaction, client, message) {
    const guild = client.guilds.cache.get("1057116059750117426");
    const ticketId = Math.floor(Math.random() * 9000);


    
    await guild.channels.create({
        name: `TICKET-${ticketId}`,
        parent: '1057370813357109308',
    })

    interaction.reply({ephemeral: true, content: `Your ticket has been submited \n You can view it here -> ${guild.channels.id}` });   
    
    }
}

CodePudding user response:

What you need is a way to persist data after every command. This would require some sort of data storage. I've listed a few options below:

  1. Use a database, (On the discord.js guide they recommend using an ORM)
  2. Store the files in a JSON object on your file system.

Here is an example for 2:

module.exports = {
  data: {
    name: 'GT1',
  },
  async execute(interaction, client, message) {
    const guild = client.guilds.cache.get('1057116059750117426');

    const storageBuffer = fs.readFileSync('./storage.json'); // you will input your file path here.
    const storageData = JSON.parse(storageBuffer.toString());

    storageData.ticket_id  ; // adds one to the ticket number?
    const ticketId = storageData.ticket_id;

    await guild.channels.create({
      name: `TICKET-${ticketId}`,
      parent: '1057370813357109308',
    });

    interaction.reply({
      ephemeral: true,
      content: `Your ticket has been submited \n You can view it here -> ${guild.channels.id}`,
    });

    fs.writeFileSync('./storage.json', JSON.stringify(storageData)); // updates the data to your storage file
  },
};

You will need to create the json file before using it.

storage.json

{
  ticket_id: 0
}

As for sending to a channel you can take a look at this: https://discord.js.org/#/docs/main/stable/class/Interaction?scrollTo=channel

  • Related