問題描述
我遇到了上述錯誤,我只是在測試是否可以在特定公會的頻道中發送違規者的標簽,并且代碼看起來很像下面(忽略一些不一致的部分.下面是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模板網!