| const { PermissionFlagsBits } = require('discord.js'); | |
| /** | |
| * Build permission overwrite objects for a channel from the config shorthand. | |
| * @param {Object} overrides - { roleName: { view, send, connect, speak } } | |
| * @param {Map} roleMap - Map<roleName, Role> (includes 'everyone' β @everyone) | |
| * @returns {Array} Array of permission overwrite objects for channel creation | |
| */ | |
| function buildOverwrites(overrides, roleMap) { | |
| const result = []; | |
| for (const [roleName, perms] of Object.entries(overrides)) { | |
| const role = roleMap.get(roleName === 'everyone' ? 'everyone' : roleName); | |
| if (!role) continue; | |
| const allow = []; | |
| const deny = []; | |
| // View | |
| if (perms.view === true) allow.push(PermissionFlagsBits.ViewChannel); | |
| if (perms.view === false) deny.push(PermissionFlagsBits.ViewChannel); | |
| // Send | |
| if (perms.send === true) allow.push(PermissionFlagsBits.SendMessages); | |
| if (perms.send === false) deny.push(PermissionFlagsBits.SendMessages); | |
| // Connect (voice) | |
| if (perms.connect === true) allow.push(PermissionFlagsBits.Connect); | |
| if (perms.connect === false) deny.push(PermissionFlagsBits.Connect); | |
| // Speak (voice) | |
| if (perms.speak === true) allow.push(PermissionFlagsBits.Speak); | |
| if (perms.speak === false) deny.push(PermissionFlagsBits.Speak); | |
| // React | |
| if (perms.react === true) allow.push(PermissionFlagsBits.AddReactions); | |
| if (perms.react === false) deny.push(PermissionFlagsBits.AddReactions); | |
| // Read history | |
| if (perms.readHistory === true) allow.push(PermissionFlagsBits.ReadMessageHistory); | |
| if (perms.readHistory === false) deny.push(PermissionFlagsBits.ReadMessageHistory); | |
| result.push({ | |
| id: role.id || role, | |
| allow, | |
| deny, | |
| }); | |
| } | |
| return result; | |
| } | |
| module.exports = { buildOverwrites }; | |