【问题标题】:How can I fix this error "Cannot set properties of undefined"如何解决此错误“无法设置未定义的属性”
【发布时间】:2021-12-04 19:58:59
【问题描述】:
const { Client, Intents, MessageEmbed } = require('discord.js');
let client = new Client({ intents: [Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES] });
const dotenv = require('dotenv');
const Keyv = require('keyv');
const keyv = new Keyv();
dotenv.config();

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', async (msg) => {
    let number = msg.content.split(' ')[1];
    if (msg.content === '!ping') {
        msg.channel.send('ping!')
    }
`
    const getGuildPrefix = async () => {
        const prefixMap = await keyv.get('prefix');
        return prefixMap ?. [msg.guild.id] || "!"
    }

// Sets the prefix to the current guild.
    const setGuildPrefix = async (prefix) => {
        const prefixMap = await keyv.get('prefix');
        prefixMap[msg.guild.id] = prefix;
        await keyv.set('prefix', `${prefixMap}`);
    }

    let prefix = await getGuildPrefix();
// Get prefix command.
    if ((msg.content === `${process.env.prefix}prefix`) || (msg.content === `${prefix}prefix`)) {
        msg.channel.send(`Your server prefix is ${prefix}`)
    }
})
client.login(process.env.token);

所以这段代码的作用是检索特定服务器的前缀,其中不同的服务器有不同的前缀,所以我使用 keyv 包来存储前缀,但是我收到一个错误,这是错误消息

    prefixMap[msg.guild.id] = prefix;
                                ^
TypeError: Cannot set properties of undefined (setting '857122654976606239')
    at setGuildPrefix

这里如果键中的值为空,那么它应该返回值“!”默认。所以这几乎是我昨天做的代码,但我稍微编辑了代码以修复 getGuildPrefix 中的错误,现在我收到了这个错误。这两个错误都是由于未定义引起的,但我使用可选链接来修复第一个错误,并且 getGuildPrefix 工作正常,但现在 setGuildPrefix 部分出现错误。问题是即使 PrefixMap 未定义,该值也应该是“!”,代码就是这样(我假设,但不是很确定)。我该如何解决这个错误?

【问题讨论】:

  • 公会定义正确,你可以看到它在日志中给出了公会ID
  • 看来prefixMap 是未定义的。 keyv 已初始化但未填充任何键值对。
  • 您需要编写一些代码来执行您所说的操作:“此代码的作用是为特定服务器存储一个前缀,以便不同的服务器可以有不同的前缀”我在您发布的示例。
  • 这段代码只是为了获取前缀而不是存储它,我没有在这里给出完整的代码,因为它太长了。顺便说一句,我通过使用可选链接 (?.) 修复了错误,因此如果该术语未定义,则它返回“!”

标签: discord.js


【解决方案1】:

我觉得你这里的逻辑语法可能有问题,试试

const getGuildPrefix = async () => {
        const prefixMap = await keyv.get('prefix');
        if(!prefixMap[msg.guild.id]) {
            return prefixMap[msg.guild.id];
        } 
        else {
            return '!';
        }
    };

【讨论】:

  • 还是同样的错误,错误信息也和上一个代码一样
  • 我不是逻辑语法错误,我设法通过使用可选链接来修复该错误,这与这里使用的方法几乎相同,但有点不同。所以现在getGuildPrefix没有错误
【解决方案2】:

应该返回的值是 !,但我们只在 getGuildPrefix 中给出了它,但我们不能在 setGuildPrefix 中进行可选链接,因为它会给出像左手一样的错误,所以如果 keyql 没有从db 再次,然后我们可以在 if 语句中再次启动它,但使用另一个 if 语句。为什么我们不能在 getGuildPrefix idk 中使用这种技术,但我们可以在 setGuildPrefix 中使用它,我们可以在 getGuildPrefix 中使用可选链接,现在错误已修复,代码正常工作

client.on('messageCreate', async (msg) => {
    let number = msg.content.split(' ')[1];
    if (msg.content === '!ping') {
        msg.channel.send('ping!')
    }


// Use this function to get the prefix in other files.
// Use like `const prefix = await getGuildPrefix();`
    const getGuildPrefix = async () => {
        const prefixMap = await keyv.get('prefix');
        return prefixMap ?. [msg.guild.id] || "!"
    }

// Sets the prefix to the current guild.
    const setGuildPrefix = async (prefix) => {
        let prefixMap = await keyv.get('prefix');
        if (!prefixMap)
    {
     prefixMap = "!";
    }
        prefixMap[msg.guild.id] = prefix;
        await keyv.set('prefix', `${prefixMap}`);
    }

    let prefix = await getGuildPrefix();
// Get prefix command.
    if ((msg.content === `${process.env.prefix}prefix`) || (msg.content === `${prefix}prefix`)) {
        msg.channel.send(`Your server prefix is ${prefix}`)
    }
})
client.login(token)

【讨论】:

    猜你喜欢
    • 2019-11-18
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2020-10-21
    • 2015-09-13
    • 2019-09-21
    相关资源
    最近更新 更多