【发布时间】:2021-08-31 14:05:42
【问题描述】:
我在这里有点需要帮助,老实说,我不确定我哪里出错了,这是完整的代码。我是个新手,只是想在消息中带回提及用户和原因,而不是对这些信息做任何事情。
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)
}
}
如果我不提,我最终会得到this
如果我确实提到 this 我收到上述错误但没有响应。
(node:4044) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined
Index.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);
【问题讨论】:
-
你能显示你调用它的代码吗?
-
@MrMythical 编辑了它。
-
调用,我的意思是你把
<something>.execute()放在哪里 -
另外,要将字符串放在一起,您必须使用
+,而不是,。在 message.reply 中使用它:"You reported " + offender + " for reason: " + reason -
好的,字符串部分有问题,谢谢你现在改一下@MrMythical
标签: javascript discord discord.js