簡體   English   中英

Discord.js v13 無法讀取屬性 o 未定義的讀取發送

[英]Discord.js v13 cannot read properties o undefined reading send

所以我遇到了這個問題,我想制作一個警告系統,但它不起作用! 我不知道如何解決它! 我已經嘗試了一些方法來修復它,但沒有任何幫助! 因此,當有人幫助我時,我真的很感激!

錯誤日志:

[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 文件:


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

我希望有人知道答案! 我會嘗試說它是否盡可能快地工作!

注意:這是為 Discord.js v13.4.0 編寫的


<Guild>.member不是屬性

被調用的.roles屬性的第一個實例在此行。

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

問題出在<Guild>.member 您正在嘗試調用不存在的.member 假設您正在請求機器人用戶,您應該使用<Guild>.me ,它將返回一個GuildMember代表服務器中的機器人。 更改后的版本如下所示:

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

// DO this
message.guild.me

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM