【发布时间】:2021-04-04 03:08:31
【问题描述】:
当用户使用新的 Discord 斜杠命令之一时,我正尝试从我们的 Discord 机器人向用户发送 DM。
代码如下。 Discord 文档说 `interaction.member 应该是 Discord GuildMember,但是,下面的代码给了我以下错误:
类型错误:interaction.member.send 不是函数
我可以从数据字段调用其他本地函数,但无法弄清楚如何回复用户。我假设我做错了什么(根据错误),但我无法从斜杠命令回调中找出向用户发送消息的食谱。
client.ws.on("INTERACTION_CREATE", async (interaction) => {
const command = interaction.data.name.toLowerCase();
const args = interaction.data.options;
if (command == "testing") {
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 2,
data: interaction.member.send("hello").catch(console.error),
},
});
}
});
编辑:在 Jakye 的帮助下的最终解决方案。注意,我必须使用“fetch”而不是“get”,因为 get 一直返回一个未定义的用户。
if (command == 'testing') {
client.users.fetch(interaction.member.user.id)
.then(user => user.send("hello").catch(console.error))
.catch(console.error);
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 2,
}
});
}
【问题讨论】:
标签: javascript node.js discord http-post discord.js