【问题标题】:Application System应用系统
【发布时间】:2021-03-14 03:04:52
【问题描述】:

所以我为不和谐服务器制作了一个应用程序系统,在测试它时,机器人工作正常,直到应用程序的问题完成,它说应用程序已经发送,但它没有发送应用程序,我收到一个错误提示

(node:11408) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined

这是我的代码错误在appsChannel.send(applicationembed);

const { Client, Message, MessageEmbed } = require('discord.js')

module.exports = {
    name: "apply",
    /**
     * @param {Client} client
     * @param {Message} message
     * @param {string[]} args
     */
    run: async (client, message, args) => {
        const questions = [
            "What is your IGN currently?",
            "What is your discord Name and Tag?",
            "Why do you want to join the guild?",
            "What games stats are you applying with?",
            "Is there anything else you want to tell us?"
        ];
        let collectCounter = 0;
        let endCounter = 0;

        const filter = (m) => m.author.id === message.author.id;

        const appStart = await message.author.send(questions[collectCounter++])
        const channel = appStart.channel;

        const collector = channel.createMessageCollector(filter);
        
        collector.on("collect", () => {
            if(collectCounter < questions.length) {
                channel.send(questions[collectCounter++])
            } else {
                channel.send("You application has been sent!")
                collector.stop("fulfilled")
            }
        })

        const appsChannel = client.channels.cache.get('820355472635985991')
        collector.on('end', (collected, reason) => {
            if(reason === 'fulfilled') {
                let index = 1;
                const mappedResponses = collected.map((msg) => {
                    return `${index++}) ${questions[endCounter++]}\n-> ${msg.content}`;
                })
                .join('\n\n');
              
           const applicationembed = new MessageEmbed()
                    .setAuthor(
                        message.author.tag,
                        message.author.displayAvatarURL({ dynamic: ture })
                    )
                    .setTitle('New Application!')
                    .setDescription(mappedResponses)
                    .setColor('RANDOM')
                    .setTimestamp()
            appsChannel.send(applicationembed);

        }
                
        })
    },
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    您尝试获取的频道可能不在缓存中。最好是获取而不是依赖缓存。

    将您的 appsChannel 定义更改为此。

    const appsChannel = await client.channels.cache.fetch('820355472635985991');
    

    【讨论】:

      【解决方案2】:

      你可以尝试在这里使用 await 吗?

      const appsChannel = await client.channels.cache.get('820355472635985991')
      

      编辑:

      const applicationembed = await new MessageEmbed()
                          .setAuthor(
                              message.author.tag,
                              message.author.displayAvatarURL({ dynamic: ture })
                          )
                          .setTitle('New Application!')
                          .setDescription(mappedResponses)
                          .setColor('RANDOM')
                          .setTimestamp()
                  await appsChannel.send(applicationembed);
      

      为了让你的异步工作:

      collector.on('end', async (collected, reason) => {
      

      【讨论】:

      • 还要添加这个吗?在我的回答中查看我的编辑。
      • 它说 SyntaxError: await is only valid in async function 即使在第 10 行它说 run: async (client, message, args) => {
      • 嗯,我现在才看到,但您忘记了 MessageEmbed 上的括号。我会再次编辑我的帖子:) 编辑:nvm 我错了。关于括号。这是因为您嵌套在另一个没有异步的函数中。让我编辑我的帖子等等。
      • Collection#get 不返回承诺,也不创建新的嵌入。不需要await
      • 你为什么需要到处等待?这些不是承诺。
      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 2013-02-02
      • 2014-05-10
      • 2013-07-25
      • 1970-01-01
      相关资源
      最近更新 更多