This command also provides boilerplate for listeners and automatic restarts when you save a file
splitscriptdevapp.ts
Run code on start
You can run a function after the bot starts, by using the ready event
functions/discord/ready/hello.ts
import { Events } from'@splitscript.js/discord';exportdefaultasyncfunction (event:Events.Ready) {console.log(`Logged in as ${event.user.username}#${event.user.discriminator}`)}
After saving this, look at your terminal. It should show the above message
Add a message (prefix) command
Enable Message Content Intent
We need to set the intent in our code to be able to read messages
Next, lets handle the command. We'll use the discord/message/create event
functions/discord/message/create/ping.ts
import { Events, messages } from'@splitscript.js/discord'exportdefaultasyncfunction (event:Events.MessageCreate) {// Check if the message starts with !ping, and exit if it doesn'tif (!event.content.startsWith("!ping")) return// Send a message backawaitmessages.create(event.channelId, { content:"Pong!" })}
Now, when you send a message starting with !ping, our bot responds with Pong!
Slash Commands
This section requires the applications.commands scope
Register the Command
Before we can handle commands, we need to register it first. Lets do this automatically in our functions/discord/ready folder.