【问题标题】:Discord bot in node: Cannot read property 'send' of undefined节点中的不和谐机器人:无法读取未定义的属性“发送”
【发布时间】:2021-03-27 15:51:05
【问题描述】:

这是我不断出错的代码:

const {
    Message
} = require('discord.js');

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

module.exports = {
    name: 'apply',
    /**
     * @param {Client} client
     * @param {Message} message
     * @param {String[]} args
     */
    run: async (client, message, args) => {
        message.channel.send('DM\'ing you, here is a reminder, you have to have DMs open for me to start and application process so remember to open DMs if they are closed.')
        const questions = [`Would You Like To Apply?`, `This is a test correct?`, `am I friends with you`, `is this bot cool?`];
        const f = new Discord.MessageEmbed()
            .setTitle(`Hello would you answer some questions?`)
            .setDescription(`I have seen you have sent the command a!apply in ${message.guild.name}.`)
        const dmChannel = await message.author.send(f);
        const collector = dmChannel.channel.createMessageCollector((msg) => msg.author.id == message.author.id);
        let i = 0;
        const res = [];
        dmChannel.channel.send(questions[0])
        collector.on('collect', async (msg) => {
            if (questions.length == i) return collector.stop('MAX');
            const answer = msg.content;
            res.push({
                question: questions[i],
                answer
            });
            i++;
            if (questions.length == i) return collector.stop('MAX');
            else {
                dmChannel.channel.send(questions[i]);
            }
        });
        collector.on('end', async (collected, reason) => {
            if (reason == 'MAX') {
                const data = message.guild.channels.cache.find(ch => ch.name.toLowerCase() == 'app-logs' && ch.type == 'text');
                const embed = new Discord.MessageEmbed()
                    .setTitle(`New Application`)
                    .setDescription(`**from** <@${message.author.id}>`)
                    .setColor('RANDOM')
                const e = new Discord.MessageEmbed()
                    .setTitle(`Questions and Answers:`)
                    .setDescription(`${res.map(d => `**${d.question} - Answer:** ${d.answer}`).join("\n")}`, '\n ^^^^')
                    .setColor('RANDOM')
                dmChannel.channel.send('Your application has been recorded thanks!')
                // trying to code this
                if (!data) {
                    message.guild.channels.create('app-logs', {
                        type: 'text',
                        permissionOverwrites: [{
                                id: message.guild.id,
                                deny: ['VIEW_CHANNEL'],
                            },
                            {
                                id: message.author.id,
                                deny: ['VIEW_CHANNEL'],
                            },
                            {
                                id: client.user.id,
                                allow: ['VIEW_CHANNEL'],
                            },
                        ],

                    })

                    await data.send(embed)
                    await data.send(e)
                }
                await data.send(embed)
                await data.send(e)
                console.log(`${res.map(d => `${d.question} - ${d.answer}`).join("\n")}`)
                console.log(`this is an application by from @${message.author.tag} in the guild (${message.guild.name})`)


            }
        })
    }
}

错误是:

0|索引 | TypeError:无法读取未定义的属性“发送”

我正在尝试了解此错误的来源以及原因?

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    你有这个代码块:

    if (!data) {
                        message.guild.channels.create('app-logs', {
                            type: 'text',
                            permissionOverwrites: [{
                                    id: message.guild.id,
                                    deny: ['VIEW_CHANNEL'],
                                },
                                {
                                    id: message.author.id,
                                    deny: ['VIEW_CHANNEL'],
                                },
                                {
                                    id: client.user.id,
                                    allow: ['VIEW_CHANNEL'],
                                },
                            ],
    
                        })
    
                        await data.send(embed)
                        await data.send(e)
                    }
    

    它测试数据是虚假的,创建一个通道,然后对数据进行操作而不将其设置为任何内容。我怀疑您想等待 channels.create 的结果并将结果保存在数据中:

       data = await  message.guild.channels.create('app-logs', { // ...
          //...
       );
    
    
       if (data) {
          await data.send(embed)
          await data.send(e)
       }
    

    您可能希望有一个异常处理程序,以防发送因其他原因(如延迟等)而失败

    【讨论】:

      猜你喜欢
      • 2020-07-31
      • 1970-01-01
      • 2021-05-25
      • 2019-04-19
      • 2021-09-25
      • 2020-12-10
      • 2019-01-27
      • 2020-11-22
      • 2020-08-30
      相关资源
      最近更新 更多