【问题标题】:Can't find the message author找不到消息作者
【发布时间】:2021-09-21 04:56:53
【问题描述】:

在这行代码中,我尝试向初始消息作者发送私人消息。由于某些未知原因,出现错误:

(node:17560) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined

这是这行代码:

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");

感谢您的帮助!

【问题讨论】:

  • 您是否尝试在它引发错误的行之前记录消息对象?

标签: javascript node.js discord discord.js


【解决方案1】:

这是一个简单的解决方法......正如@Lioness100 提到的你的参数是错误的,我在这里展示一个代码示例

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

这是一个常见的错误

【讨论】:

  • 我个人建议不要添加client 作为参数,而只使用messageargs。这是因为您始终可以通过message.client 访问客户端。
  • 我同意@Lioness100
  • 好吧,我按照你的建议做了,但是我在第 41 行遇到了错误const appsChannel = client.channel.cache.get('863534867668009004');,上面写着:(node:32096) UnhandledPromiseRejectionWarning: ReferenceError: client is not defined 所以基本上现在它无法识别客户端(可能)
  • 好的,那就把参数替换成message, args, client
  • 好的,然后只需将参数替换为消息,参数,客户端@SzymonRudnikowski 这应该可以解决错误,祝你有美好的一天
【解决方案2】:

这只是意味着message 不是你想象的那样。在您的命令处理程序中,您使用参数messageargs 执行了命令(顺序很重要)。但是,在命令文件中,您希望参数是 clientmessage,然后是 args

这意味着,在你的命令文件中,client实际上是指消息,message实际上是指参数,第三个参数不存在。

要解决此问题,您可以修改任一文件中参数的名称和顺序。

【讨论】:

  • 是的,您也应该将Messafe 更改为Message...(在您的jsdoc 中)
猜你喜欢
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多