javascript - 机器人的用户名未定义

标签 javascript bots discord.js

我收到此错误 UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined这是由 client.user.username 引起的在embed.setFooter() .

module.exports = {
    name: 'suggest',
    aliases: ['sug', 'suggestion'],
    description: 'Suggest something for the Bot',
    execute(message, client, args) {
        const Discord = require('discord.js');
        const filter = m => m.author.id === message.author.id;

        message.channel.send(`Please provide a suggestion for the Bot or cancel this command with "cancel"!`)

        message.channel.awaitMessages(filter, { max: 1, })
            .then(async (collected) => {
                if (collected.first().content.toLowerCase() === 'cancel') {
                    message.reply("Your suggestion has been cancelled.")
                }
                else {
                    let embed = new Discord.MessageEmbed()
                        .setFooter(client.user.username, client.user.displayAvatarURL)
                        .setTimestamp()
                        .addField(`New Suggestion from:`, `**${message.author.tag}**`)
                        .addField(`New Suggestion:`, `${collected.first().content}`)
                        .setColor('0x0099ff');
                    client.channels.fetch("702825446248808519").send(embed)

                    message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
                }
            })
    },
    catch(err) {
        console.log(err)
    }
};

最佳答案

根据您的评论here

try { command.execute(message, args); } catch (error) { console.error(error); message.reply('There was an error trying to execute that command!'); } });

您没有将 client 传递给 execute(),您需要这样做。

您还需要在 channels.fetch() 上使用 await,因为它返回一个 Promise,因此请替换 client.channels.fetch("702825446248808519")。发送(嵌入):

const channel = await client.channels.fetch("702825446248808519")
channel.send(embed)

关于javascript - 机器人的用户名未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61467073/

相关文章:

javascript - 钛的Paypal集成问题

javascript - 格式化 JavaScript 函数以将匹配项映射到时间组

c# - 从同一解决方案中的不同测试项目引用项目中的 C# 命名空间

javascript - 无法将 Observable 返回的对象作为输入组件 Angular 5 传递

bots - Microsoft Bot Framework - 如何更改演示消息

ruby - 如何让 Selenium/Ruby 机器人在执行操作之前等待?

javascript - SQL排行榜问题nodejs discord.js

node.js - 是否可以检查消息是否不包含 "ew"并删除该消息?

node.js - 以编程方式获取剩余的免费动力时间

Javascript getFullYear() 返回错误的年份