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}>`);
}
});
}
}});
}}
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}>`);
}
});
}
}
});