I'm trying to make a command where you can put a role inside an option. If the user mentions a role within the slashcommand option, this role becomes an ID to be later added to the channel to the channel to see the content.
If the user does not put any role, the permission is assigned in @everyone
I tried something like this as I've seen in some situations I've researched:
module.exports = {
data: new SlashCommandBuilder()
.setName("esconder")
.setDescription("Comando para desativar a permissão de visualização nos chats.")
.addStringOption(option => option.setName("cargo").setDescription("Informa uma quantidade de mensagens que queres apagar!")),
async execute(interaction, client) {
let cargo = " ";
if (interaction.options._hoistedOptions[0]) {
cargo = interaction.options._hoistedOptions[0].value;
} else {
cargo = "990039749362319380"; // Everyone ID
}
if (cargo) {
const role2 = interaction.guild.roles.cache.find(role => role.name === cargo);
const guild = client.guilds.cache.get(configGuids.servidores.principal);
const role = guild.roles.cache.get(role2.id)
interaction.channel.permissionOverwrites.edit(role, { VIEW_CHANNEL: true });
return await interaction.editReply({ content: `Added ${cargo} to the channel` });
}
}
};
But it's not working as it should. Before I had put it to diction with the ID and it worked perfectly.
Use addRoleOption instead.
You can get an option's value with this: interaction.options.getRole('name'). Parsing Options
module.exports = {
data: new SlashCommandBuilder()
.setName("esconder")
.setDescription("Comando para desativar a permissão de visualização nos chats.")
.addRoleOption(option => {
option.setName("cargo")
.setDescription("Informa uma quantidade de mensagens que queres apagar!")
.setRequired(true)
return option;
}),
async execute(interaction, client) {
const cargo = interaction.options.getRole("cargo");
interaction.channel.permissionOverwrites.edit(cargo.id, { VIEW_CHANNEL: true });
return await interaction.reply({ content: `Added ${cargo.name} to the channel` });
}
};