简体   繁体   中英

Discord.js v13 cannot read properties o undefined reading send

So i have this problem where i wanna make a warn system but it doesnt work! I dont know how to fix it! I already tried some things to fix it but nothing helped! Because of that i really appreciate when someone helps me!

Error Log:

[FATAL] Possibly Unhandled Rejection at: Promise  Promise {
  <rejected> TypeError: Cannot read properties of undefined (reading 'roles')
      at Object.run (/home/runner/Bolt-Utilities/Commands/Mod/warn.js:75:48)
      at module.exports (/home/runner/Bolt-Utilities/events/guild/command.js:132:13)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
}  reason:  Cannot read properties of undefined (reading 'roles')

warn.js file:


const Discord = require("discord.js");
const ms = require("ms");


module.exports = {
    name: "warn",
    category: "Moderation",
    description: "Warns a user.",
    aliases: [" "],
    usage: "warn <mention> <reason>",
    run: async (client, message, args) => {
    let reason = args.slice(1).join(" ");
    //  const user = message.mentions.users.first();
    const warninguser = message.mentions.users.first();
    const user =
        warninguser ||
        (args[0]
            ? args[0].length == 18
                ? message.guild.members.cache.get(args[0]).user
                : false
            : false);

    const notice1 = new Discord.MessageEmbed()
        .setDescription(
            `<:No:888010623726784512> ${message.author.username}, Missing Permission`
        )
        .setColor("RED");

    const notice3 = new Discord.MessageEmbed()
        .setDescription(`<:No:888010623726784512> I don't have permission to warn people!`)
        .setColor("RED");

    const notice333 = new Discord.MessageEmbed()
        .setDescription(`<:No:888010623726784512> You must mention someone to warn him/her!`)
        .setColor("RED");
if(!message.member.permissions.has('BAN_MEMBERS', 'KICK_MEMBERS')) {
        return message.channel
            .send(notice3)
            .then(m => m.delete({ timeout: 15000 }));
    }
    if(!message.member.permissions.has('KICK_MEMBERS')) {
        return message.channel
            .send(notice1)
            .then(m => m.delete({ timeout: 15000 }));
    }

    if (!user) {
        return message.channel.send({ embeds: [notice333]}
                                )}

    const notice2 = new Discord.MessageEmbed()
        .setDescription(`<:No:888010623726784512> You cannot warn yourself`)
        .setColor("RED");

    if (user.id === message.author.id) {
        return message.channel
            .send(notice2)
            .then(m => m.delete({ timeout: 15000 }));
    }

    if (reason.length < 1) reason = "No reason given.";

    const key = `${message.guild.id}-${user.id}`;

    const dsfdsfsdf = new Discord.MessageEmbed()
        .setDescription(
            `<:No:888010623726784512> Access Denied, that member has roles higher or equal to you!`
        )
        .setColor("RED");
    const sdfsdfsdfsd = new Discord.MessageEmbed()
        .setDescription(
            `<:No:888010623726784512> Access Denied, **that member has roles higher or equal to me!`
        )
        .setColor("RED");
    const botRolePossition = message.guild.member.roles.highest
        .position;
    const rolePosition = message.guild.member.roles.highest.position;
    const userRolePossition = message.member.roles.highest.position;
    if (userRolePossition <= rolePosition) return message.channel.send(dsfdsfsdf);
    if (botRolePossition <= rolePosition)
        return message.channel.send(sdfsdfsdfsd);

    client.moderationdb.ensure(key, {
        guildid: message.guild.id,
        userid: user.id,
        warns: 0,
        isMuted: false,
        timeMuteEnd: 0,
    });
    client.moderationdb.inc(key, "warns");

    const test1 = new Discord.MessageEmbed()
        .setDescription(
            `${emojis.tick} Muted **${user.username}#${user.discriminator}** For 1 Hour | **Reached Two Warnings**`
        )
        .setColor("GREEN");
    const bsuembed = new Discord.MessageEmbed()
        .setDescription(
            `${emojis.tick} Warned **${user.username}#${user.discriminator}** | **${reason}**`
        )
        .setColor("GREEN");

    message.delete();
    message.channel.send(bsuembed);
    user.send(
        `You are warned in **${
            message.guild.name
        }** (Total Warning(s): \`${client.moderationdb.get(
            key,
            "warnings"
        )}\` ), **${reason}**`
    );

    const test2 = new Discord.MessageEmbed()
        .setDescription(
            `<a:yes:934051517986656256> Kicked **${user.username}#${user.discriminator}** | **Reached Warnings 3**`
        )
        .setColor("GREEN");

    const test3 = new Discord.MessageEmbed()
        .setDescription(
            `<a:yes:934051517986656256> Banned **${user.username}#${user.discriminator}** | **Reached 5 Warnings**`
        )
        .setColor("GREEN");

    if (client.moderationdb.get(key, "warns") == 2) {
        const muteRole = client.guilds.cache
            .get(message.guild.id)
            .roles.cache.find(val => val.name === "Muted");

        const mutetime = "60s";
        message.guild.members.cache.get(user.id).roles.add(muteRole.id);
        message.channel.send(test1);

        setTimeout(() => {
            message.guild.members.cache.get(user.id).roles.remove(muteRole.id);
        }, ms(mutetime));
    }

    if (client.moderationdb.get(key, "warns") == 3) {
        message.guild.member(user).kick(reason);
        message.channel.send(test2);
    }

    if (client.moderationdb.get(key, "warns") >= 5) {
        message.guild.member(user).ban(reason);
        message.channel.send(test3);
    }
}
};

I hope someone know the answer! I will try to say if it worked as fast as possible!

Note: This is written for Discord.js v13.4.0


<Guild>.member is not a property

The first instance of the .roles property being called is on this line.

const botRolePossition = message.guild.member.roles.highest

The issue is at <Guild>.member . You're trying to call .member , which does not exist. Assuming you're requesting the bot user, you should use <Guild>.me , which will return a GuildMember that represents the bot in the server. Here's what the altered version would look like:

// DON'T do this
message.guild.member

// DO this
message.guild.me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM