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

时间:2020-10-06 22:47:49

标签: javascript discord.js

我将从以下问题开始,我是discord.js的初学者,请帮助我!

我的整个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);

问题出在这个区块:

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

但错误消息如下:

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

如果您还有其他需要,请告诉我!

2 个答案:

答案 0 :(得分:0)

我认为您打算输入// this buttons executes when the user presses the 'submit' button: function process_payment() { $.ajax({ url : 'process_payment', data : { csrfmiddlewaretoken: 'the_csrf_token', field1 : 'field1_data', // pass other fields as needed ... }, success: payment_success_function, }); } $('#submit-button-id').click(payment_success); // this function executes when a response is received from the backend: payment_method_success(response) { // unpack response (context) from views.py: var payment_success = response.payment_success; if (payment_success) window.location('the_new_url'); else alert('Something went wrong with your payment!'); } 而不是message.content.startsWith()

答案 1 :(得分:0)

playminecraftwithus.content未定义。

未定义没有方法startsWith()

如果要避免错误,可以转换为字符串并使用它。

尝试以下操作:(playminecraftwithus.content || "").startsWith()

||表示当上一个未定义时使用next。 more details on here