# Basic Bot

Once you're done [Creating a Project](/discord.swift/overview/creating-a-project.md) and the process of [Installing the Package](/discord.swift/overview/installing-the-package.md) is complete, your project should look like this. If you're using an executable, it will look like [Creating a Project](/discord.swift/overview/creating-a-project.md#step-2a).

<figure><img src="/files/LrzygSOL07EIEnnQA1CS" alt=""><figcaption></figcaption></figure>

The next step is to import the library so we can get your bot online. You'll need your token for this. If you didn't save it somewhere, don't fret. Head to [Getting Started](/discord.swift/overview/getting-started.md#step-3)and reset your token. Once you have your token, run the following code and you'll see your bot come online.

```swift
import Discord

let bot = Bot(token: "<your token>", intents: Intents.default)
bot.run()
```

It's that simple. Even though it's online, it doesn't do much at the moment so lets change that. Lets have your bot respond to a simple message.

## Responding to a message

The library comes with the `EventListener` class. This is used to listen to a handful of events that can be dispatched by Discord. This is how you add an event listener.

```swift
import Discord

let bot = Bot(token: "<your token>", intents: Intents.default)

class MyListener : EventListener {
    override func onMessageCreate(message: Message) async {
        // Don't respond to our own message
        guard !message.author.isBot else {
            return
        }

        if message.content == "hi swifty" {
            try! await message.channel.send("Hello!")
        }
    }
}

try! bot.addListeners(MyListener(name: "example"))
bot.run()
```

When you send a message in a channel, you should get a response.

<figure><img src="/files/6UOtrlUadVClccEeYVEn" alt="" width="563"><figcaption></figcaption></figure>

## Slash Commands

Discord.swift doesn't support the old format of text commands (`!command`). It utilizes Discords feature rich, UI based slash commands (`/command`). Lets create one!

```swift
import Discord

let bot = Bot(token: "<your token>", intents: Intents.default)

bot.addSlashCommand(
    name: "example",
    description: "Example command",
    guildId: 123456789012345678,
    onInteraction: { interaction in
        try! await interaction.respondWithMessage("This is an example")
    }
)

try! await bot.syncApplicationCommands() // Only needs to be done once
bot.run()
```

Once you've executed the above code, it should show up in your server like so.

<figure><img src="/files/ETqY75ixDfnTTvfLFndA" alt=""><figcaption></figcaption></figure>

When you've used the command, it will produce the following.

<figure><img src="/files/ZDk0ma1yIJbsz3c1qJW5" alt="" width="435"><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://discord-swift.gitbook.io/discord.swift/overview/basic-bot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
