Szymon Rudnikowski
Szymon Rudnikowski

Reputation: 31

Can't find the message author

In this line of code I am trying to send private message, to initial message author. For some unknown reason there is an error:

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

Here is this line of code:

const appStart = await message.author.send(questions[collectCounter++]);

Here is whole file:

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));
        });
    },
};

This is my 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");

Thanks for help!

Upvotes: 0

Views: 378

Answers (2)

Kārlis Kazāks
Kārlis Kazāks

Reputation: 193

This is an easy fix... as @Lioness100 mentioned your parameters are wrong and im here to show a code example

locate this line of code in index.js :

client.commands.get(command).run(message, args);
// and change it to
client.commands.get(command).run(message, args, client);

after wards go to your "File" and fine this line of code :

run : async(client, message, args) => {
// And change it to
run : async(message, args) => {

Your problem was that you were executing wrong parameters in Your "File" so you just needed to change your parameters from client, message, args to message, args, client

This is a common mistake

Upvotes: 0

Lioness100
Lioness100

Reputation: 8402

This just means that message is not what you think it is. In your command handler, you executed the command with the arguments message and args (order matters). However, in the command file, you expected the parameters to be client, message, and then args.

This means, that in your command file, client actually refers to the message, message actually refers to the args, and the third parameter doesn't exist.

To fix this problem, you can modify the names and order of the parameters in either file.

Upvotes: 3

Related Questions