javascript - (节点:4044)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“缓存”
问题描述
我在这里有点需要帮助,老实说,我不确定我哪里出错了,这是完整的代码。我是个新手,只是想在消息中恢复提及用户和原因,而不是对这些信息做任何事情。
const { client, MessageEmbed } = require('discord.js');
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){
function getUserFromMention(mention) {
if (!mention) return;
if (mention.startsWith('<@') && mention.endsWith('>')) {
mention = mention.slice(2, -1);
if (mention.startsWith('!')) {
mention = mention.slice(1);
}
return client.users.cache.get(mention);
}
}
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
const offender = getUserFromMention(args[0]);
if (args.length < 2) {
return message.reply('Please mention the user you want to report and specify a reason.');
}
const reason = args.slice(1).join(' ');
message.reply("You reported",offender,"for reason:", reason)
}
}
如果我不提,我最终会得到这个
如果我确实提到了 这一点 ,我会收到上述错误而没有回应。
(node:4044) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined
索引.js:
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.mentions 属性来获取提及,查看文档以获取有关它的其他信息。这应该可以解决您的问题。
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();
// users is a collection, so we use the first method to get the first element
// Docs: https://discord.js.org/#/docs/collection/master/class/Collection
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(1).join(' ');
message.reply(`You reported ${offender} for reason: ${reason}`);
}
}
推荐阅读
- javascript - 为什么 TypedArrays 有一个值为 3 的静态长度属性
- python - Python中有哪些可散列的数据类型?
- linux - Bash Linux - 在 if 语句中调用函数
- c# - Azure Key Vault - 编程创建
- c# - 如何在 WPF 中更改 TextBox 的焦点样式?
- .net - .NET 框架和“Visual Basic”编译器的角色有什么区别?
- asp.net - 如何为 c# 获取 ASP.NET Web 应用程序(.NET Framework)?
- html - 为什么溢出:隐藏会破坏 Firefox 中的多列渲染,而不是 Chrome?
- angular - 在 Angular 中返回 Observable 时出错
- sql-server - 如何在 sql server 中仅显示来自用户定义的数据类型的数据?