Estoy tratando de crear un sistema de botones donde, si no tiene un rol específico, no puede usar un botón. Sin embargo, el código que he creado no parece funcionar.
collector.on('collect', async interaction => { const wikiStaffRole = interaction.user.guild.roles.cache.find(role => role.name == "Staff"); if (interaction.customId === "yes") { if (!wikiStaffRole) { return interaction.followUp({ content: `${interaction.user} dude you need to be a staff member`, ephmeral: true }); } newUserEmbed.edit({ embeds: [], content: 'This report has been marked as completed.', components: [] }); return; } else if (interaction.customId === "no") { if (!wikiStaffRole) { return interaction.followUp({ content: `${interaction.user} dude you need to be a staff member`, ephmeral: true }); } newUserEmbed.edit({ embeds: [], content: 'This report has been denied.', components: [] }); return; } else if (interaction.customId === "what") { if (!wikiStaffRole) { return interaction.followUp({ content: `${interaction.user} dude you need to be a staff member`, ephmeral: true }); } newUserEmbed.edit({ embeds: [], content: 'This report has been marked as inconclusive.', components: [] }); return; } });En cambio, recibo un error que indica que no puede leer la propiedad 'roles' de undefined. No puedo encontrar ninguna manera de arreglar esto.
Intentó acceder a la propiedad de guild inexistente de un objeto User . Esta propiedad no existe en los User s, pero sí en los objetos GuildMember e Interaction . Este código debería funcionar:
const wikiStaffRole = interaction.guild.roles.cache.find(role => role.name == "Staff") Tenga en cuenta que si no está en un gremio, esto arrojará un error. Puede usar el encadenamiento opcional ( ?. ) para evitar eso
const wikiStaffRole = interaction.guild?.roles.cache.find(role => role.name == "Staff")Sin embargo, creo que querías verificar si el miembro tenía el rol. En realidad, está comprobando si todo el servidor tiene el rol. Esto verificará solo los roles de los miembros.
const wikiStaffRole = interaction.member?.roles.cache.find(role => role.name == "Staff") //optional chaining because member would be null if it was sent in a DM