【发布时间】:2021-12-23 15:03:20
【问题描述】:
我正在尝试创建一个警告命令,向指定用户发送消息以通知他们。这是我正在尝试使用的特定代码:
const user = await client.users
.fetch(interaction.options.getString("user"), false)
.catch(() => null);
console.log(interaction.options.getString("user"));
const embed = new MessageEmbed()
.setColor("#FCE100")
.setTitle(`⚠️ Warning!`)
.setDescription(`${interaction.options.getString("warning")}`);
await user.send({ embeds: embed }).catch(() => {
interaction.channel.send(
"Error: user not found"
);
});
user 的 getString 正确地吸引了用户,正如我从 console.log() 测试的那样。我只能假设语法在某种程度上是错误的,尽管我似乎找不到正确的方法来做到这一点。我得到的唯一错误是:
TypeError: Cannot read properties of null (reading 'send')
【问题讨论】:
-
你能展示一下
user的输出吗?因为您可以使用InteractionOptions#getUser 而不是我想这会更简单,还有哪一行是错误?只是仔细检查 -
哦,没关系,我刚刚注意到一些事情,您不能将 .catch() 与 await 一起使用,因为它不返回承诺,而是承诺的响应,这意味着错误应该在 user.send .使用 try-catch,但是,仍然请显示用户输出的内容
-
似乎您的
.catch运行并返回null。尝试删除.catch -
@MrMythical 删除
.catch返回一个新错误:DiscordAPIError: Invalid Form Body user_id: Value "<@!155474254249459713>" is not snowflake.
标签: javascript discord.js