So what is this?

Well my friend, this project is a discord ticket system. I made this so I can show everyone how they can create their own system. This is a hobby of mine to create different projects and share them for the public. I hope that my code can help others that might be stuck. So now I’ll get to how it works.

How it works!

For my code I used mySQL, I used it because it was the easiest method of storage and verification. It was also the only method that actually worked for me. For the create ticket command you’ll notice the detect query which sends a request to the database to see if a ticket under that user already exists.

  let b = connection.query("SELECT * FROM tickets WHERE userid = ?;", message.author.id, function (err, result, fields){

this query sends a request to the database and returns an array, if the array is empty it will create a new ticket but if the array brings any information, it will send a message saying that a ticket already exists. Now let’s get to the ticket creation

message.guild.channels.create(message.author.username, "text")
.then(m => {
m.overwritePermissions(
[
{
        id: message.guild.roles.everyone,
        deny: ['VIEW_CHANNEL'],
},
{
        id: message.author.id,
        allow: ['VIEW_CHANNEL'],
},
],
);

This part was a bit hard for me to get JUST right. The main part of the ticket channel creation is the beginning you’ll notice message.author.username and text. This was the easy part for me but the hardest part for me was the permissions, I kept trying different things to get the EVERYONE role but finally I got to message.guild.roles.everyone. When I first started this part I had the entire thing wrong and outdated but now it is JUST right. In the future I might make it like how some ticket bots do it with the numbers in the front.

let ticketcreate ="INSERT INTO tickets (username, channelid, userid) VALUES ('" + message.author.username + "', '" + m.id + "','" + message.author.id + "');";

This query sends a request to the database to create a new entry. Now your probably wondering why I put in more data than the code currently uses, this is because I plan on creating more functions like a web panel. Now we’ll get to the closeticket command, this one I plan to update heavily later on. So you’ll notice a query thats very similar, however, this uses channelid this is to detect if the channel is actually a ticket channel.

let delchan = message.guild.channels.cache.get(message.channel.id);
delchan.delete();
let ticketdel ="DELETE FROM tickets where channelid = " + message.channel.id + ";";

This part took a bit to understand but I was finally able to get a hang of it. the delchan part is to get the channel that the message is coming from after its shown as a ticket channel. Once it’s done that it sends a request to the database to remove the ticket with that channels id.

So what’s next?

I plan on improving on the style of the messages and the security. I am thinking about having the messages as embeds. I also plan to add more configuration and maybe even a web panel.

Check out the project