Home > other >  TypeError: Cannot read properties of undefined (reading 'edit')
TypeError: Cannot read properties of undefined (reading 'edit')

Time:01-25

I am trying to make it so if a admin uses the command /remove @user it will remove the users ability to see the channel the command was typed in

my code

module.exports = {
    data: new SlashCommandBuilder()
        .setName('remove')
        .setDescription('Removes a user to the ticket')
        .addStringOption(option => 
            option
            .setName("user")
            .setDescription("You must tag a user")
            .setAutocomplete(false)
            .setRequired(true)
            ),    
    async execute(interaction, client) {

        const channel = interaction.channel.id
        const guild = client.guilds.cache.get("MyGuildID");
        const user = interaction.options.getString('user');

        channel.permissionOverwrites.edit(user.id, { ViewChannel: true });
    },
};

I also am not sure if I have set up const channel correctly

I was expecting for the bot to remove a users permissions to see the channel

CodePudding user response:

You should be able to use this for the permissions

const user = interaction.options.getUser('user')


channel.permissionOverwrites.edit(user, { 
            'ViewChannel': false,
             'SendMessages': false
        });

and .addSubcommand in place of .addStringOption

.addSubcommand(subcommand =>
            subcommand
                .setName('user')
                .setDescription('Remove taged User')
                .addUserOption(option => option.setName('user').setDescription('The user'))), 
    async execute(interaction, client) {

Your Final code should look something like this

const {SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, BaseInteraction, 
    SelectMenuBuilder, StringStringSelectMenuOptionBuilder, ButtonStyle, messageLink, 
    EmbedBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, PermissionFlagsBits, PermissionsBitField} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('remove')
        .setDescription('Removes a user to the ticket')
        .addSubcommand(subcommand =>
            subcommand
                .setName('user')
                .setDescription('Remove taged User')
                .addUserOption(option => option.setName('user').setDescription('The user'))), 
    async execute(interaction, client) {

        const channel = interaction.channel;
        const user = interaction.options.getUser('user')
        
        channel.permissionOverwrites.edit(user, { 
            'ViewChannel': false,
             'SendMessages': false
        });
        channel.send({ content: `${user} Has been removed from the ticket!`});
    },
};
  • Related