diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-15 13:56:24 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2026-07-15 13:56:24 +0100 |
| commit | a8cd84b8291990265dbdddcdaa079886234180a1 (patch) | |
| tree | 20dec2454dc7ee331f29d53b339f0f26a384b56a /174bg/discord-bot/utilities/createTicket.js | |
| parent | c488c4840588fd88fa840306c40ab99ebb3512b9 (diff) | |
| download | unnamed-group-a8cd84b8291990265dbdddcdaa079886234180a1.tar unnamed-group-a8cd84b8291990265dbdddcdaa079886234180a1.tar.gz unnamed-group-a8cd84b8291990265dbdddcdaa079886234180a1.tar.bz2 unnamed-group-a8cd84b8291990265dbdddcdaa079886234180a1.tar.xz unnamed-group-a8cd84b8291990265dbdddcdaa079886234180a1.zip | |
refactor codebase for dry
Diffstat (limited to '174bg/discord-bot/utilities/createTicket.js')
| -rw-r--r-- | 174bg/discord-bot/utilities/createTicket.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/174bg/discord-bot/utilities/createTicket.js b/174bg/discord-bot/utilities/createTicket.js new file mode 100644 index 0000000..9b573d6 --- /dev/null +++ b/174bg/discord-bot/utilities/createTicket.js @@ -0,0 +1,52 @@ +import { ChannelType, PermissionFlagsBits } from "discord.js"; + +export default async (interaction, roleNamesWithAccess) => { + const ticketsCategory = interaction.guild.channels.cache.find( + (channel) => + channel.name.toLowerCase() === "tickets" && + channel.type === ChannelType.GuildCategory, + ); + + if (!ticketsCategory) + return await interaction.reply("Tickets category not found."); + + // get the roles that should have access to this ticket + const rolesWithAccess = roleNamesWithAccess.map((roleName) => { + return interaction.guild.roles.cache.find( + (role) => role.name.toLowerCase() === roleName.toLowerCase(), + ); + }); + + // create a new text channel under the "tickets" category with the following format: ticket-<timestamp_short> + const timestamp = new Date() + .toISOString() + .replace(/[-:.T]/g, "") + .slice(2, 16); + + const channelName = `ticket-${timestamp}`; + + const newChannel = await interaction.guild.channels.create({ + name: channelName, + type: ChannelType.GuildText, + parent: ticketsCategory.id, + // set permissions so that only the user who created the ticket and the roles with access can view it + permissionOverwrites: [ + { + id: interaction.user.id, + allow: [ + PermissionFlagsBits.ViewChannel, + PermissionFlagsBits.SendMessages, + ], + }, + ...rolesWithAccess.map((role) => ({ + id: role.id, + allow: [ + PermissionFlagsBits.ViewChannel, + PermissionFlagsBits.SendMessages, + ], + })), + ], + }); + + return newChannel; +}; |
