NodeJS TypeError:无法读取未定义的属性(阅读“edit”)

b09cbbtk  于 2023-01-30  发布在  Node.js
关注(0)|答案(1)|浏览(165)

我正在尝试使它,所以如果管理员使用命令/remove@user,它将删除用户看到命令键入的频道的能力
我的代码

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 });
    },
};

我也不确定我是否正确设置了const通道
我以为机器人会删除用户查看频道的权限

ovfsdjhp

ovfsdjhp1#

您应该可以使用此权限

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

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

并用.addSubcommand代替.addStringOption

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

最终代码应该如下所示

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!`});
    },
};

相关问题