Responding to Commands
Hello World! Coming through loud and clear!
Now that you've registered a command, lets respond to it! For the /hello command, we want our bot to respond with a random fun message:
Hello, World!
Helloooo! I can hear you!
Who's there?
Beep, boop, I'm a bot.
I'm alive!
Let's start by putting all those messages in an array in our Bot
struct:
We can use the built-in .randomElement()
method on our array to get a random hello message. As we're sure our array isn't empty, we can safely force-unwrap the result with !
. Putting all that together in our interaction's handler function gives us:
Run your bot and invoke the /hello command. Your bot should respond with a random message each time, as shown below! Congrats on your first functional slash command!
You'll have to respond to an interaction within 3 seconds, if not the Discord client will display an error stating that "The interaction did not respond".
If you need more than 3s to respond to an interaction (e.g. a đ database), you'll need to send a deferred response first (guide coming soon) to extend the deadline to 15 minutes. This will show the user a loading state which will be replaced by a response once you send one.
Last updated