【发布时间】:2021-09-01 15:02:03
【问题描述】:
我遇到了上述错误,我只是在测试是否可以在特定公会的频道中发送违规者的标签,并且代码看起来很像下面(忽略一些不一致的部分。下面是cmd文件中的代码。
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}`);
}
}
这个命令在没有下面一行的情况下完全可以正常工作。
client.channels.cache.get('xxxxx').send(offender);
但是一旦包含此行运行,我就会收到此错误。
(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"
});
}
}
【问题讨论】:
-
你能用你的消息事件编辑帖子吗?似乎您没有从消息事件中传递客户端,并且像下面的答案一样,我认为这不是事件处理程序本身的问题,而是事件消息
-
您似乎将事件存储在事件文件夹中,因此使用消息事件文件代码编辑帖子您应该有类似
command.execute(message,.....something here) -
@TYPICALNINJA 好的会显示
-
哦,你找到了答案,我询问了消息事件,因为我需要查看你传递给命令的参数(你传递了
message ,args, client,但只在你的命令,导致client = args,因为args是第二个参数)
标签: javascript node.js discord discord.js