Discord vs bot 13 无法分配角色给出错误无法读取未定义的属性(读取 'guild')

Discord vs bot 13 not able to assign roles giving error Cannot read properties of undefined (reading 'guild')

我正在使用 discord js v13.6。我在我的代码中编写了在使用斜杠命令后为用户分配了许多角色,这些角色存储在名为 rolenumber 的数组中。问题是我每次都收到这个错误

 await client.users.fetch(memberId).then((user,interaction) => {
            user.send("Give me a few moments while I verify your details");
            (async function(user){

            
            let RoleArray = rolenumber.split(","); 
            //let guild = client.guilds.cache.get(guildNumber);
            //let member = await client.users.fetch(msg);   
            
            for(let num of RoleArray) {
                //console.log("Roles Inside a for loop",num);
                let role = interaction.guild.roles.cache.get(num);
                let data = await user.roles.add(role).catch(console.error);
                //user.roles.add(num).catch(console.error);            
            }   
            
           
            let welcomeMessage = 'Welcome <@'+ user.id +'>, you are now officially part of channel.';
            const channelMessage = await client.channels.cache.get(channelNumber);
            channelMessage.send(welcomeMessage);
        })();

这里的 guildNumber 是一个公会编号,出于安全原因,它在问题中是隐藏的。我收到此错误消息:

let role = interaction.guild.roles.cache.get(num);
                                       ^

TypeError: Cannot read properties of undefined (reading 'guild')

at E:\OutscalGit\DiscordBot\index.js:200:44
at processTicksAndRejections (node:internal/process/task_queues:96:5)

互动未定义 您确定,交互传递到这里?

可以用但不完美

let role = interaction?.guild?.roles?.cache?.get(num);

Promise.prototype.then 的实现回调只需要 1 个参数。

await client.users.fetch(memberId).then((user) => {
            user.send("Give me a few moments while I verify your details")
            // ...
})

假设上面已经定义了interaction,它应该可以工作