[英]Cannot read property 'startsWith' of undefined on Discord.js
I shall start this question with, i'm a beginner for discord.js, and please help me!我将开始这个问题,我是 discord.js 的初学者,请帮助我!
My whole index.js is:我的整个 index.js 是:
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!如果您还需要什么,请告诉我!
我想你的意思是输入message.content.startsWith()
而不是playminecraftwithus.content.startsWith()
。
playminecraftwithus.content
is undefined. playminecraftwithus.content
未定义。
undefined has no method startsWith()
undefined 没有方法startsWith()
if you want to avoid an error, you could convert to string and use it.如果你想避免错误,你可以转换为字符串并使用它。
Try this: (playminecraftwithus.content || "").startsWith()
试试这个: (playminecraftwithus.content || "").startsWith()
||
means that when previous one is undefined use next.意味着当前一个未定义时使用下一个。 more details on here 更多细节在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.