首页 > 解决方案 > “TypeError:无法读取未定义的属性‘我’”

问题描述

我正在为我的 discord.js v12 机器人创建两个命令,但是当第一个命令用于第一个命令的代码时,它会引发错误“TypeError: Cannot read property 'me' of undefined”:

function generateID() {
    let ticketGen = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("");
    let ticketStr = "";

    for(let i = 0; i < 3; i++) {
        ticketStr += ticketGen[Math.floor(Math.random() * ticketGen.length)];
    }

    return ticketStr;
}
const fsn = require("fs-nextra");

const colors = require("colors");
const Discord = require('discord.js');

module.exports = {
    name: 'order',
    description: 'Ordering something',
    args: 'true',
    usage: '<order desription>',
    aliases: ['o'],
    execute(message) {
        
        const client = message.client; //<- Line added right here
        
        let order = message.content.substring(7);
        let channel = message.guild.channels.cache.get("746423099871985755");
        let customer = message.author.id

        fsn.readJSON("./blacklist.json").then((blacklistDB) => {
            let entry = blacklistDB[message.guild.id];
            
            // Checks is server is blacklisted or not.
            if(entry === undefined) {
                // Gets ticket ID.
                const ticketID = generateID();
        
                // Sends ticket information to tickets channel.
                const exampleEmbed = new Discord.MessageEmbed()
                .setColor('#FFFF33')
                .setTitle(message.member.user.tag)
                .setAuthor(`Order ID: ${ticketID}`)
                .setDescription(order)
                .setThumbnail(message.author.avatarURL())
                .setTimestamp()
                .setFooter(`From ${message.guild.name} (${message.guild.id})`);
            
                channel.send(exampleEmbed);


        
                    // Sets ticket info.
                    fsn.readJSON("./orders.json").then(orderDB => {
                        // Set JSON information.
                        if (!orderDB[ticketID]) orderDB[ticketID] = {
                            "orderID": ticketID,
                            "userID": message.author.id,
                            "guildID": message.guild.id,
                            "channelID": message.channel.id,
                            "order": order,
                            "status": "Unclaimed",
                            "ticketChannelMessageID": "not set"
                        };
            
                        // Write JSON information.
                        fsn.writeJSON("./orders.json", orderDB, {
                            replacer: null,
                            spaces: 4
                        }).then(() => {
                            // Sends an embed to the customer.
                            message.channel.send("Your order has been sent and it will be delivered soon.")
            
                            // Logs in console.
                            console.log(colors.green(`${message.author.username} ordered "${order}" in ${message.guild.name} (${message.guild.id}) in ${message.channel.name} (${message.channel.id}).`));
                        }).catch((err) => {
                            if(err) {
                                message.reply(`There was a database error! Show the following message to a developer: \`\`\`${err}\`\`\``);
                                console.log(colors.red(`Error in order ${ticketID}: ${err}`));
                            }
                        });
                    });
                
            }else {
                message.reply("This server is currently blacklisted.");
            }
        });
    }
};

在这部分,我认为它如何保存信息有问题?

  // Sets ticket info.
                    fsn.readJSON("./orders.json").then(orderDB => {
                        // Set JSON information.
                        if (!orderDB[ticketID]) orderDB[ticketID] = {
                            "orderID": ticketID,
                            "userID": message.author.id,
                            "guildID": message.guild.id,
                            "channelID": message.channel.id,
                            "order": order,
                            "status": "Unclaimed",
                            "ticketChannelMessageID": "not set"
                        };

                        // Write JSON information.
                        fsn.writeJSON("./orders.json", orderDB, {
                            replacer: null,
                            spaces: 4
                        }).then(() => {
                            // Sends an embed to the customer.
                            message.channel.send("Your order has been sent and it will be delivered soon.")

使用返回错误“TypeError:无法读取未定义的属性'me'”的第二个命令时发生错误,我认为它与第一个命令第二个命令代码有关:

if (order.status === "Ready") {
                        // Delete ticket from database.
                        delete orderDB[ticketID];

                        // Writes data to JSON.
                        fsn.writeJSON("./orders.json", orderDB, {
                            replacer: null,
                            spaces: 4
                        }).then(() => {
                            // If bot has create instant invite permission.
                            
                            if(client.guilds.cache.get(order.guildID).me.hasPermission("CREATE_INSTANT_INVITE")) {
                                // Send message to cook.
                                client.channels.cache.get(order.channelID).send(`${client.users.cache.get(order.userID)}, Here is your taco that you ordered. Remember you can do \`.tip [Amount]\` to give us virtual tips, and \`.feedback [Feedback]\` to give us feedback on how we did. ${order.imageURL}`);

                                message.reply(`Order has been sent to ${client.guilds.cache.get(order.guildID).name}`);
                                
                                
                                
                                
                            }else {
                                // Bot delivers it's self.
                                client.channels.cache.get(order.channelID).send(`${client.users.cache.get(order.userID)}, Here is your taco that you ordered. Remember you can do \`.tip [Amount]\` to give us virtual tips, and \`.feedback [Feedback]\` to give us feedback on how we did. ${order.imageURL}`);

我只包括了我认为有错误的部分,因为order.guildid未定义但我该如何解决?

标签: node.jsdiscord.js

解决方案


正如错误消息所解释的,没有me未定义的属性,因此公会未定义/未找到。检查您使用的公会 ID 是正确的公会 ID,它是一个字符串而不是数字。可能值得console.log(client.guilds.cache.get(order.guildID).name));在其中放置一个以帮助确定问题。


推荐阅读