const { PermissionsBitField, ChannelType } = require('discord.js'); const config = require('../config.json'); const fs = require('fs'); const path = require('path'); const orderCounterPath = path.join(__dirname, 'orderCounter.json'); /** * Returns the role ID for the given order type. */ function getRoleId(type) { return config.roles[type]; } /** * Returns the category ID for the given order type. */ function getCategoryId(type) { return config.category[type]; } /** * Gets the next order number, persisting it in a JSON counter file. */ function getNextOrderNumber() { let data = { lastOrderNumber: 1000 }; if (fs.existsSync(orderCounterPath)) { data = JSON.parse(fs.readFileSync(orderCounterPath)); } data.lastOrderNumber += 1; fs.writeFileSync(orderCounterPath, JSON.stringify(data, null, 4)); return data.lastOrderNumber; } /** * Creates a new order channel with proper permissions. * Returns { channel, orderNumber } */ async function createOrderChannel(interaction, type) { const guild = interaction.guild; const roleId = getRoleId(type); const categoryId = getCategoryId(type); const orderNumber = getNextOrderNumber(); // Use the correct format for the channel name const channelName = `test-${type}-${orderNumber}-pending`; const channel = await guild.channels.create({ name: channelName, parent: categoryId, type: ChannelType.GuildText, permissionOverwrites: [ { id: guild.id, deny: [PermissionsBitField.Flags.ViewChannel] }, { id: interaction.user.id, allow: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.AttachFiles] }, { id: roleId, allow: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.AttachFiles] } ] }); return { channel, orderNumber }; } /** * Renames an order channel to the proper format: test-[type]-[orderNumber]-[status] */ async function renameOrderChannel(channel, type, orderNumber, status) { const validTypes = [ ChannelType.GuildText, ChannelType.GuildNews, ChannelType.PublicThread, ChannelType.PrivateThread, ChannelType.AnnouncementThread ]; if (!channel) { console.warn('renameOrderChannel: channel is undefined or null!'); throw new Error('renameOrderChannel: channel is undefined or null!'); } if (typeof channel.setName !== 'function' || !validTypes.includes(channel.type)) { throw new Error(`renameOrderChannel: Provided channel does not support setName! Type: ${channel?.type}, ID: ${channel?.id}`); } const newName = `test-${type}-${orderNumber}-${status}`; return await channel.setName(newName); } module.exports = { getRoleId, getCategoryId, createOrderChannel, renameOrderChannel };