【问题标题】:Uncaught TypeError: Cannot read properties of undefined (reading 'roles')未捕获的类型错误:无法读取未定义的属性(读取“角色”)
【发布时间】:2021-12-19 21:14:15
【问题描述】:

我想创建一个白名单角色来执行命令,我输入了这一行

if (!message.guild.cache.roles.find(role => role.id === '873595824469344256')) return;

我得到的错误:Uncaught TypeError: Cannot read properties of undefined (reading 'roles')

const Discord = require("discord.js");
const { Client, Intents } = require('discord.js');
const bot = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGE_TYPING", "GUILD_MEMBERS", "GUILD_PRESENCES"] });
const config = require('./config.json');
const prefix = "g!"

bot.on("ready", () => {
    console.log(`${bot.user.tag} bot is online`);
});

bot.on("messageCreate", message => {
    if(message.content === prefix + "help") {
        if (!message.guild.cache.roles.find(role => role.id === '873595824469344256')) return;
        let helpembed = new Discord.MessageEmbed()
        .setColor('#0099ff')
        .setTitle('Help Guide')
        .setAuthor('Request : ' + `${message.author.username}`)
        .addFields(
            { name: 'Ban Usage', value: `${prefix}ban **id** | **reason** | **time** \n**__Exemple__** : ${prefix}ban id ALT-F4+Troll (without espace) 32H`},
            { name: 'Warn Usage', value: `${prefix}warn **id** | **reason** | **time** \n**__Exemple__** : ${prefix}warn id Bunny-Hope (without espace) warn-1/2/3`},
        )
        .setFooter('© ©');
    
    message.reply({ embeds: [helpembed] });
    };
});

【问题讨论】:

  • message.guild.cache.roles.find 应该是message.guild.roles.cache.find

标签: javascript node.js discord discord.js


【解决方案1】:

改变你的

if (!message.guild.cache.roles.find(role => role.id === '873595824469344256')) return;

到:

if (!message.member.roles.cache.has("873595824469344256")) return`

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2021-12-25
    • 2021-11-24
    • 2021-10-31
    • 2021-11-07
    • 2022-01-17
    • 2023-03-13
    • 2022-01-01
    • 2022-01-10
    相关资源
    最近更新 更多