在这行代码中,我试图向初始消息作者发送私人消息。由于某种未知原因,出现错误:
<块引用>(node:17560) UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“发送”
这是这行代码:
const appStart = await message.author.send(questions[collectCounter++]);
这是整个文件:
const Discord = require("discord.js");
const {Client, Message, MessageEmbed} = require("discord.js");
module.exports = {
name: 'apply',
/**
*
* @param {Client} client
* @param {Messafe} message
* @param {String[]} args
*/
run : async(client, message, args) => {
const questions = [
"What is your in-game name?",
"Do you accept the rules?",
"How old are you?",
"Where are you from?",
"How long have you been playing Minecraft?",
"Where did you find the server?",
"Have you ever been banned from another server on Minecraft? If yes, what was the reason?",
"Why should we accept you?",
]
let collectCounter = 0;
let endCounter = 0;
const filter = (m) => m.author.id === message.author.id;
const appStart = await message.author.send(questions[collectCounter++]);
const channel = appStart.channel;
const collector = channel.createMessageCollector(filter);
collector.on("collect", () => {
if(collectCounter < questions.length) {
channel.send(questions[collectedCounter++]);
} else {
channel.send("Your application has been sent!");
collector.stop("fulfilled");
}
});
const appsChannel = client.channel.cache.get('863534867668009004');
collector.on('end', (collected, reason) => {
if(reason === 'fulfilled') {
let index = 1;
const mappedResponses = collected.map((msg) => {
return `&{index++}) &{questions[endCounter++]}\n-> ${msg.content}`
})
.join('\n\n');
}
appsChannel.send(message.channel.send('New Application!', mappedResponses));
});
},
};
这是我的 index.js:
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const BOT_ID = "863550035923697674";
const prefix = "!";
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity("ur cheat files...", {
type: "WATCHING",
url: "https://discord.gg/bzYXx8t3fE"
});
});
for(const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on("message", message => {
let userID = message.author.id;
if (userID == BOT_ID) {
return;
}
const version = "Wersja bota 0.2";
///const args = message.content;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(!client.commands.has(command)) return;
try{
client.commands.get(command).run(message, args);
}catch(error){
console.error(error);
message.reply('**There was an issue executing that command!**');
}
});
client.login("tokenishere");
感谢您的帮助!
答案 0 :(得分:3)
这只是意味着 message
不是您认为的那样。在您的命令处理程序中,您使用参数 message
和 args
(顺序很重要)执行了命令。但是,在命令文件中,您希望参数为 client
、message
和 args
。
这意味着,在您的命令文件中,client
实际上指的是消息,message
实际上指的是 args,而第三个参数不存在。
要解决此问题,您可以修改任一文件中参数的名称和顺序。
答案 1 :(得分:0)
这是一个简单的修复...因为@Lionness100 提到您的参数错误,我在这里展示代码示例
在 index.js
中找到这行代码:
client.commands.get(command).run(message, args);
// and change it to
client.commands.get(command).run(message, args, client);
然后转到您的“文件”并修改这行代码:
run : async(client, message, args) => {
// And change it to
run : async(message, args) => {
您的问题是您在“文件”中执行了错误的参数,因此您只需要将参数从 client, message, args
更改为 message, args, client
这是一个常见的错误