0

I shall start this question with, i'm a beginner for discord.js, and please help me!

My whole index.js is:

const Discord = require('discord.js');
const client =  new Discord.Client();
const botsettings = require('./botsettings.json')

client.once('ready', () => {
    console.log("Ready!");
 });
client.on("message", async message =>{
    const playminecraftwithus = message.guild.channels.cache.find(channel => channel.name === 'play-minecraft-with-us')
    if(playminecraftwithus.content.startsWith("IGN:")) {
        return; 
 } else {
   message.delete();
}
});

client.login(botsettings.token);

and the problem is in this block:

client.on("message", async message =>{
    const playminecraftwithus = message.guild.channels.cache.find(channel => channel.name === 'play-minecraft-with-us')
    if(playminecraftwithus.content.startsWith("IGN:")) {
        return; 
 } else {
   message.delete();
}
});

but error message is like:

(node:13811) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined | discord.js

If you need anything else, please tell me!

Joona Yoon
  • 314
  • 3
  • 16
  • it means `playminecraftwithus.content` is undefined - to debug, suggest you `console.log(playminecraftwithus)` to see what you are dealing with – Jaromanda X Oct 06 '20 at 23:01
  • `playminecraftwithus` is a `Channel`, and doesn't have the `content` property. What exactly are you trying to do? – Lioness100 Oct 06 '20 at 23:30

2 Answers2

0

I think you meant to type message.content.startsWith() instead of playminecraftwithus.content.startsWith().

RTFL
  • 145
  • 1
  • 7
0

playminecraftwithus.content is undefined.

undefined has no method startsWith()

if you want to avoid an error, you could convert to string and use it.

Try this: (playminecraftwithus.content || "").startsWith()

|| means that when previous one is undefined use next. more details on here

Joona Yoon
  • 314
  • 3
  • 16
  • There is no `content` property on `Channel` objects. – RTFL Oct 07 '20 at 03:38
  • @김현진 Yes you're right. As like linoness10 said on comment, i do not know what is expected result for question. thus i fixed an error to use string method for undefined. Even we don't know the context neither what he want. – Joona Yoon Oct 07 '20 at 04:00