我目前正在开发一个 Discord.js 机器人,我需要检测用户何时触发用于其他机器人的应用程序命令。我希望能够跟踪用户何时发送用于音乐机器人的“/play”等命令,然后捕获音乐机器人响应的交互。
示例:用户输入“/play”,认为这会触发音乐机器人,但我的机器人拦截了它并记录了它是用于音乐机器人的。然后,当音乐机器人响应用户的命令时,我的机器人应该检测到这种交互并记录音乐机器人的回复,可能还包含其他详细信息,例如包含链接的嵌入。
我的问题是:是否可以使用 Discord.js 实现此功能,如果可以,我该如何有效地实现它?
我尝试过的:
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return; // Ignore if not a slash command
// Check if the command is "/play"
if (interaction.commandName === 'play') {
// Log that the /play command was detected for the music bot
console.log("Detected /play command meant for music bot.");
// Check if the music bot responds with an embed containing a link
const filter = (response) => {
return response.author.id === '123456789012345678' && response.embeds.length && response.embeds[0].title === "Music Bot Response" && response.embeds[0].description === "Here is your requested song: [Song Title](songlink)";
};
interaction.channel.awaitMessages({ filter, max: 1, time: 10000, errors: ['time'] })
.then(async (collected) => {
console.log("Detected music bot replied with an embed containing a link.");
// logic for handling the music bot's response here
})
.catch((error) => {
console.error('Error while waiting for music bot response:', error);
});
}
});
您无法检测发送到其他机器人的交互(来源)
但是您可以监听机器人的交互响应(只要它不是短暂的)
client.on('messageCreate', async message => {
if (!message.guild) return; // Ignore messages outside a guild
if (message.interaction) {
// Message originates from an interaction
// Check if the command is "/play"
if(message.interaction.commandName === 'play') {
// logic for handling the music bot's response here, e.g.
console.log(`Detected /play command initiated by ${message.interaction.user.username}`)
}
}
})
message.interaction 对象不包含交互选项 (docs)
获取音乐链接的唯一方法是机器人通过其回复发送