简体   繁体   English

类型错误:无法读取未定义的 discord js 的属性“first”

[英]TypeError: Cannot read property 'first' of undefined discord js

I know I'm not the best at this stuff and I'm still learning but can anyone help me with my error, I know it's with my " let role = " and it's something to do with the ' = ' sign, I'm just not sure what to change it to.我知道我在这方面不是最好的,我仍在学习,但谁能帮助我解决我的错误,我知道这与我的“ let role = ”有关,这与 ' = ' 符号有关,我'我只是不知道要把它改成什么。

if (!message.member.hasPermission('MANAGE_CHANNELS')) return message.reply(Embed3)
        if (!args[0]) return message.reply(Embed2)
        if (!message.mentions.channels.first()) return message.reply(Embed1)
        let role = message.guild.roles.cache.find(r => r.name = args[1]); //const role = message.mentions.roles.first
        if (!role) return message.reply(Embed4);

Current error is当前错误是

TypeError: Cannot read property 'first' of undefined

I was following this guide , still not sure what to do though.我正在遵循本指南,但仍然不确定该怎么做。

Any help is much appreciated, thanks and have a great day!非常感谢任何帮助,谢谢,祝你有美好的一天! :) :)

EDIT 1

In my index.js, I have this在我的 index.js 中,我有这个

-- ——


client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    let args = message.content.substring(prefix.length).split(" ");
    const command = args.shift().toLowerCase();
    if (command === 'lock') {
        client.commands.get('lock').execute(message, args)

    } else if (command === 'unlock') {
        client.commands.get('unlock').execute(message, args)
    }
})

EDIT 2

Stops printing after this line在此行之后停止打印

 if (channel.name.startsWith('🔒')) return message.reply(Embed5); console.log('All this works')

// STOPS PRINTING //
await message.mentions.channel.first.setName(`🔒${channel.name}`);  // If channel name is not changed it's being rate limited. 
            try {
                await channel.updateOverwrite(role, {
                    SEND_MESSAGES: false
                }); 
                message.channel.send(Embed6);
            } catch (err) {
                console.log(err);
                message.channel.send(Embed7); 
            }

        });
    }
}

So from what i read in the discord documentation所以从我在不和谐文档中读到的

https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=mentions https://discord.js.org/#/docs/main/stable/class/MessageMentions?scrollTo=channels https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=mentions https://discord.js.org/#/docs/main/stable/class/MessageMentions?scrollTo=channels

and what i have seen in your code:以及我在您的代码中看到的内容:

It may be that the mentions might not have a channels property set if there is no mention in differen channels.如果不同渠道中没有提及,则提及可能没有设置channels属性。 Therefore you might need to check for that:因此,您可能需要检查:

if (message.mentions.channels || !message.mentions.channels.first()) return message.reply(Embed1)

Also the find you use is strange:您使用的 find 也很奇怪:

let role = message.guild.roles.cache.find(r => r.name = args[1]);

This statement in the filter method will always set the name of the cache to args[1] and return the value. filter 方法中的这个语句将始终将缓存的名称设置为 args[1] 并返回值。 If args[1] is truthly, it will then return the first cache object.如果 args[1] 是真的,它将返回第一个缓存对象。 what you need is a compare in this place你需要的是在这个地方进行比较

let role = message.guild.roles.cache.find(r => r.name == args[1]); // does not check type
or
let role = message.guild.roles.cache.find(r => r.name === args[1]); // checks type

Edit 2:编辑2:

You might also need to check the way you access the channel.first() prop.您可能还需要检查访问channel.first()道具的方式。 channel.first() is a method. channel.first()是一种方法。 If you want the result of this method be shure to call it.如果您想要此方法的结果,请务必调用它。

And handle the promise correctly with the then method.并使用then方法正确处理承诺。 Since setName will teurn a Promise you need to embrace it!因为 setName 会产生一个 Promise,所以你需要接受它! https://discord.js.org/#/docs/main/stable/class/GuildChannel?scrollTo=setName https://discord.js.org/#/docs/main/stable/class/GuildChannel?scrollTo=setName

message.mentions.channels.first().setName(`🔒${channel.name}`).then((result) => { // If channel name is not changed it's being rate limited.
    try {
        await channel.updateOverwrite(role, {
            SEND_MESSAGES: false
        }); 
        message.channel.send(Embed6);
    } catch (err) {
        console.log(err);
        message.channel.send(Embed7); 
    }
});

Also be shure to coretly inline your code and to check your controll structure.还要确保正确地内联您的代码并检查您的控制结构。 If you are using VScode use the format method: How do you format code in Visual Studio Code (VSCode)如果您使用的是 VScode,请使用格式化方法: How do you format code in Visual Studio Code (VSCode)

Please try to read up on async / await statements in modern JS.请尝试阅读现代 JS 中的 async / await 语句。 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/async_function https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/async_function

If you want to use await statement here, wrap it in a async function or use the then method to continue exec after processing.这里如果要使用await语句,可以将其包裹在async函数中,或者使用then方法在处理后继续exec。

I believe your mistake might have been attempting to find the first mentioned user in the channel.我相信您的错误可能是试图在频道中找到第一个提到的用户。 You would need to have used .fetch() for this.您需要为此使用.fetch() Make sure you respect your promise if you're using async!如果您使用异步,请确保您尊重您的承诺!

If this isn't what you want and you simply want to find the first user that is mentioned in the message that the command is run on, you would need to use:如果这不是您想要的,并且您只想找到运行命令的消息中提到的第一个用户,则需要使用:

        if (!message.mentions.users.first()) return message.reply(Embed1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义 Discord.js node.js v12.16.3 的属性“第一个” - TypeError: Cannot read property 'first' of undefined Discord.js node.js v12.16.3 discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined Discord.JS:TypeError:无法读取未定义的属性“角色” - Discord.JS:TypeError: Cannot read property 'roles' of undefined discord.js TypeError:无法读取未定义的属性“名称” - discord.js TypeError: Cannot read property 'name' of undefined “ TypeError:无法读取未定义的属性'push'” discord.js - “TypeError: Cannot read property 'push' of undefined” discord.js 类型错误:无法使用 Discord.js 读取未定义的属性“集” - TypeError: Cannot read property 'set' of undefined with Discord.js 类型错误:无法读取未定义的 Discord.js 错误的属性“get” - TypeError: Cannot read property 'get' of undefined Discord.js Error 类型错误:无法读取未定义的属性“成员” - Discord.JS - TypeError: Cannot read property 'members' of undefined - Discord.JS 类型错误:无法读取未定义 discord.js 的属性“解决” - TypeError: Cannot read property 'resolve' of undefined discord.js 类型错误:无法读取未定义 discord.js 的属性“查找” - TypeError: Cannot read property 'find' of undefined discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM