当用户使用一个新的不和谐斜杠命令时,我正试图从我们的不和谐机器人发送一个DM给他们。
代码在下面。Discord文档说`Interaction.Member应该是Discord GuildMember,但是,下面的代码给了我以下错误:
TypeError:Interaction.Member.Send不是函数
我可以从data字段调用其他本地函数,但一直无法弄清楚如何将用户DM回来。我假设我做错了什么(根据错误),但我无法从slash命令回调中找出DM用户的方法。
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,
}
});
}
交互数据直接来自Discord的API,因此interaction.member
将是一个对象。
member: {
user: {
username: 'Username',
public_flags: 0,
id: '0',
discriminator: '0000',
avatar: ''
},
roles: [],
premium_since: null,
permissions: '0',
pending: false,
nick: null,
mute: false,
joined_at: '2020-12-26T19:10:54.943000+00:00',
is_pending: false,
deaf: false
}
您必须手动获取成员,要么从缓存中获取成员,要么从API中获取成员。
const user = client.users.cache.get(interaction.member.user.id);
user.send("Hello").catch(console.error);
client.ws.on("INTERACTION_CREATE", async interaction => {
const guild = client.guilds.cache.get(interaction.guild_id);
const user = client.users.cache.get(interaction.member.user.id);
user.send(`hello, you used the ${interaction.data.name.toLowerCase()} command in ${guild.name}`).catch(console.error);
});