简体   繁体   English

无法读取 Discord.js 上未定义的属性“startsWith”

[英]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.

相关问题 JavaScript(discord.js)TypeError:无法读取未定义的属性“startsWith” - JavaScript(discord.js) TypeError: Cannot read property 'startsWith' of undefined Discord.JS UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“startsWith” - Discord.JS UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined Discord.JS:TypeError:无法读取未定义的属性“角色” - Discord.JS:TypeError: Cannot read property 'roles' of undefined Discord.js:无法读取未定义的属性“ set” - Discord.js: Cannot read property 'set' of undefined 无法读取undefined的属性'nsfw' - discord.js - Cannot read property 'nsfw' of undefined - discord.js discord.js TypeError:无法读取未定义的属性“名称” - discord.js TypeError: Cannot read property 'name' of undefined “ TypeError:无法读取未定义的属性'push'” discord.js - “TypeError: Cannot read property 'push' of undefined” discord.js Discord.js 无法读取未定义的属性“发送” - Discord.js Cannot read property 'send' of undefined 类型错误:无法使用 Discord.js 读取未定义的属性“集” - TypeError: Cannot read property 'set' of undefined with Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM