(节点:10388)UnhandledPromiseRejectionWarning:TypeError:无法

时间:2023-02-10
本文介绍了(节点:10388)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“缓存"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了上述错误,我只是在测试是否可以在特定公会的频道中发送违规者的标签,并且代码看起来很像下面(忽略一些不一致的部分.下面是cmd文件中的代码.

I have been getting the above error, where I am just testing to see if I can send the offender's tag in a specific guild's channel, and the code looks pretty much like below (ignore some parts that are inconsistent. Below is the code in the cmd file.

const { prefix } = require("../config.json");

module.exports = {
    name: "report",
    description: "This command allows you to report a user for smurfing.",
    catefory: "misc",
    usage: "To report a player, do $report <discord name> <reason>",
    async execute(message, client) {

        const args = message.content.slice(1).trim().split(/ +/);
        const offender = message.mentions.users.first();

        if (args.length < 2 || !offender.username) {
            return message.reply('Please mention the user you want to report and specify a reason.');
        }


        const reason = args.slice(2).join(' ');

        client.channels.cache.get('xxxxx').send(offender);

        message.reply("You reported"${offender} for reason: ${reason}`);
    }
}

这个命令在没有下面一行的情况下完全可以正常工作.

This command works without the below line completely fine.

client.channels.cache.get('xxxxx').send(offender);

但是一旦包含此行运行,我就会收到此错误.

but once run with this line included, I get this error.

(node:10388) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined

下面是我的索引文件.

const fs = require('fs');
const Discord = require("discord.js");
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.prefix = prefix;
client.commands = new Discord.Collection();


const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args,client));
    } else {
        client.on(event.name, (...args) => event.execute(...args,client));
    }
}

client.login(token);

事件:

message.js

module.exports = {
    name: "message",
    execute(message,client){
        if (!message.content.startsWith(client.prefix) || message.author.bot) return;
        const args = message.content.slice(client.prefix.length).trim().split(/ +/);
        const command = args.shift().toLowerCase();
        if (!client.commands.has(command)) return;
        try {
            client.commands.get(command).execute(message ,args, client);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }
    }
}

ready.js

module.exports = {
    name: "ready",
    once: true,
    execute(client){
        console.log("successfully logged in!");
        client.user.setActivity("VTB", {
            type: "STREAMING",
            url: "https://www.twitch.tv/monstercat"
      });
    }
}

推荐答案

你的问题可能来自这行:

Your issue likely comes from this line:

(...args) => event.execute(...args, client)

这基本上意味着获取所有参数并将它们传递给 event.execute";因此,您传递的参数数量不确定(取决于事件类型),并且 client 不能保证是您期望的第二个.

Which means basically "take all of the parameters and pass them to event.execute" so you are passing an undetermined amount of parameters (depending on the event type) and the client is not guaranteed to be the second one as you expect.

为了提供更多细节,Discord 客户端可以发送多种不同类型的事件,并且每种类型的事件在回调中提供不同数量的参数.换句话说,通过这样写: (...args) =>event.execute(...args,client) 您正在检索未知数量的参数,并将它们全部传递给 event.execute 函数,因此 的位置client 参数可能会有所不同,它不一定是函数签名中所期望的第二个:async execute(message, client) {

To provide some more details, the Discord client can send multiple different types of events, and every type of event provides a different amount of parameters in the callback. In other words by writing this: (...args) => event.execute(...args,client) you are retrieving an unknown amount of parameters, and passing them all to the event.execute function, so the position of the client parameter could vary, and it's not necessarily the second one as you expect in the function signature: async execute(message, client) {

如果不需要其他参数,您可以只检索第一个参数,如下所示:

You could either retrieve only the first parameter if you don't need others, like this:

client.once(event.name, arg => event.execute(arg, client));

如果您绝对需要所有参数,请先传递客户端,这样它就永远不会移动,如下所示:

And if you absolutely need all of the parameters, pass the client as first so it never moves, like this:

client.once(event.name, (...args) => event.execute(client, ...args));

同时修改 execute 签名:

async execute(client, ...args) {

这篇关于(节点:10388)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“缓存"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:从 Google Drive API 获取图像文件 URL 下一篇:Discord.js 错误提供的角色不是角色、雪花或数组或

相关文章

最新文章