Getting started
This will guide you through the basics of setting up DDBKit and your bot. By the end, we should have something that can connect to Discord, and you can work from there afterward.
Prerequisites
- A decent understanding of the Swift programming language.
- Something that can compile Swift code. macOS and Linux are preferred. Get Swift here.
Making our directory and package
We need to set up our bot’s directory. Open a terminal and copy and paste the following command.
mkdir
makes a folder, cd
changes the current directory to that folder.
Now that we’re in the folder, we can create our Package.swift
file.
Copy and paste the following command.
We now have a Package.swift
. This just kind of tells Swift how to
run our program, a bot in this case. The defaults aren’t exactly
what we need, so copy and paste the following code into Package.swift
.
Creating our entry point
The entry point file is where the bot will be configured (token, intents,
etc.) and what to do when the bot is ready. You can name this anything
but main.swift
. We will just go with Bot.swift
here.
The command you ran earlier has already made a Sources/main.swift
file.
Rename this to your choice and copy and paste the following code into it.
Change the token to your bot’s token, and if any, specify
the intents you need in intents
under BotGatewayManager
.
First Command
Let’s make a simple command to test the waters. In your Bot.swift
file, add the following code.
Don’t yet concern yourself with try?
, it’s not too important right now.
Try running your bot. After a minute or two, you’ll find a new /ping
command available for use.
Command Modifiers
You’ll find that Command
types feature many modifiers, such as the one you may have already discovered above. DDBKit implements these modifiers that configure your command’s options before they’re registered after body evaluation.
These modifiers help describe and configure your commands, such as default permission requirements, DM usability, integration contexts and more!
Expanding on this, DDBKit includes extra utilities as another module you can import as DDBKitUtilities
, which contains builders for messages and other composable objects, and modifiers that offer extra client side functionality.
Organisation
The best way to organise your code is to extend your bot’s structure with a computed variable for groups of commands/events.
Begin by making a new directory named Commands and Events
or whatever suits your needs.
Make a new swift file, naming it appropriately. Use the following code to get started.
Your directory structure should now look a little like this:
- Package.swift
DirectorySources
DirectoryCommands and Events
- TestCommands.swift
- …
- Bot.swift
As such, you can continue organising your code in a similar fashion or in any way you desire.
Your bot is now ready to build and run, good job 🎉