如何实现Discord机器人触发指令后监听并回复后续消息——Google Assistant Discord机器人开发求助
Got it, let's work through this together. The core idea here is to track which users have triggered the initial "hey google" command, so your bot knows to prioritize their next message. Here's a practical implementation using Discord.js (the most popular library for building Discord bots):
1. Track Listening Users with a Set
First, create a scoped Set to keep track of user IDs that are in the "waiting for follow-up" state. This lets your bot remember who it needs to listen to after the trigger:
// At the top of your bot's main file const listeningUsers = new Set();
2. Handle the Initial Trigger Command
In your messageCreate event, detect when a user sends "hey google", reply to confirm you're listening, and add their ID to the listening set. You can also add a timeout to stop listening if the user goes inactive:
client.on('messageCreate', async (message) => { // Ignore messages from bots to avoid loops if (message.author.bot) return; // Check for the initial "hey google" trigger if (message.content.toLowerCase() === 'hey google') { await message.reply("I'm listening"); listeningUsers.add(message.author.id); // Optional: Stop listening after 5 minutes of inactivity setTimeout(() => { if (listeningUsers.has(message.author.id)) { listeningUsers.delete(message.author.id); message.channel.send(`${message.author}, I stopped listening since you didn't send a request.`).catch(console.error); } }, 5 * 60 * 1000); // 5 minutes in milliseconds return; } });
3. Process Follow-Up Messages
Right after handling the trigger, add logic to check if the user is in the listening set. If they are, process their request and then remove them from the set (or keep them if you want continuous listening):
// Inside the same messageCreate event, after the trigger check if (listeningUsers.has(message.author.id)) { // Handle specific user requests switch (message.content.toLowerCase()) { case 'what time is it': const now = new Date(); // Format time to a friendly 12-hour string (e.g., "2:40 pm") const formattedTime = now.toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true }); await message.reply(formattedTime); break; case 'what's the weather': // Add your weather API integration here await message.reply("It's sunny with a high of 75°F today!"); break; default: await message.reply("Sorry, I don't get that. Try asking something like 'what time is it' or 'what's the weather'."); } // Remove the user from listening state after processing their request listeningUsers.delete(message.author.id); }
Quick Tips
- Per-Server Tracking: If your bot runs on multiple servers, you might want to track users per guild (e.g., using a
Mapthat links guild IDs to Sets of user IDs) instead of a global Set. - Continuous Listening: If you want the bot to keep accepting commands after the first follow-up, skip deleting the user from the Set until they send a "stop" command or hit the timeout.
- Error Handling: Wrap
awaitcalls intry/catchblocks if you want to gracefully handle cases where the bot can't send a message (e.g., missing permissions).
内容的提问来源于stack exchange,提问作者Solid Coder




