Skip to content

Commit

Permalink
feat(button-ticket): create ticket, set permiss, and test msg (#2) (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
osiic authored Sep 21, 2023
1 parent 965e27e commit c0d6492
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 55 deletions.
21 changes: 2 additions & 19 deletions commands/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
SlashCommandBuilder,
PermissionFlagsBits,
} = require("discord.js");
const wait = require("node:timers/promises").setTimeout;

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -16,14 +15,10 @@ module.exports = {
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator),

async execute(interaction) {
console.log(interaction);
let channel;
await interaction
.deferReply
// { ephemeral: true }
();
await interaction.deferReply({ ephemeral: true });

try {
let channel;
const ticketEmbed = new EmbedBuilder()
.setColor(0x0099ff)
.setTitle("Ticket Menu")
Expand All @@ -47,25 +42,13 @@ module.exports = {
channel = await interaction.guild.channels.create({
name: `ticket`,
type: ChannelType.GuildText,
permissionOverwrites: [
// {
// id: interaction.user.id,
// allow: [PermissionFlagsBits.ViewChannel],
// },
{
id: interaction.guild.roles.everyone.id,
deny: [PermissionFlagsBits.ViewChannel],
},
],
});

channel.send({
embeds: [ticketEmbed],
components: [new ActionRowBuilder().addComponents(openTicket)],
});
await interaction.editReply(`Ticket is ready, move on <#${channel.id}>`);
await wait(1000 * 6);
channel.delete();
} catch (err) {
await interaction.editReply(
`Tickets cannot be created \`\`\`\n${err}\`\`\``,
Expand Down
129 changes: 93 additions & 36 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,107 @@
const { Collection, Events } = require("discord.js");
const {
Collection,
ChannelType,
PermissionFlagsBits,
Events,
} = require("discord.js");

module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isChatInputCommand()) return;
if (interaction.isChatInputCommand()) {
// respond to the Chat

const command = interaction.client.commands.get(interaction.commandName);
const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
);
return;
}
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
);
return;
}

const { cooldowns } = interaction.client;
const { cooldowns } = interaction.client;

if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}
if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 3;
const cooldownAmount =
(command.cooldown ?? defaultCooldownDuration) * 1000;

if (timestamps.has(interaction.user.id)) {
const expirationTime =
timestamps.get(interaction.user.id) + cooldownAmount;

const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 3;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

if (timestamps.has(interaction.user.id)) {
const expirationTime =
timestamps.get(interaction.user.id) + cooldownAmount;

if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({
content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`,
ephemeral: true,
});
if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({
content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`,
ephemeral: true,
});
}
}
}

try {
await command.execute(interaction);
timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
} catch (error) {
console.error(`Error executing ${interaction.commandName}`);
console.error(error);
try {
await command.execute(interaction);
timestamps.set(interaction.user.id, now);
setTimeout(
() => timestamps.delete(interaction.user.id),
cooldownAmount,
);
} catch (error) {
console.error(`Error executing ${interaction.commandName}`);
console.error(error);
}
} else if (interaction.isButton()) {
// respond to the button
try {
let channel;
await interaction.deferReply({ ephemeral: true });
switch (interaction.customId) {
case "open-ticket":
try {
channel = await interaction.guild.channels.create({
name: `t-${interaction.user.username}`,
type: ChannelType.GuildText,
permissionOverwrites: [
{
id: interaction.user.id,
allow: [PermissionFlagsBits.ViewChannel],
},
{
id: interaction.guild.roles.everyone.id,
deny: [PermissionFlagsBits.ViewChannel],
},
],
});

channel.send({
content: `<@${interaction.user.id}>`,
// embeds: [ticketEmbed],
// components: [new ActionRowBuilder().addComponents(openTicket)],
});

await interaction.editReply(
`Ticket is been created, move on <#${channel.id}>`,
);
} catch (error) {
console.error(error);
}
break;

default:
break;
}
} catch (error) {
console.error(`Error executing ${interaction.customId}`);
console.error(error);
}
} else if (interaction.isStringSelectMenu()) {
// respond to the select menu
}
},
};

0 comments on commit c0d6492

Please sign in to comment.