In this example we will deploy a Serenity bot with Shuttle that can add, list and complete todos using Application Commands. To persist the todos we need a database. We will have Shuttle provison a PostgreSQL database for us by passing #[shared::Postgres] pool: PgPool as an argument to our main function.

To run this bot we need a valid Discord Token. To get started log in to the Discord developer portal.

  1. Click the New Application button, name your application and click Create.
  2. Navigate to the Bot tab in the lefthand menu, and add a new bot.
  3. On the bot page click the Reset Token button to reveal your token. Put this token in your Secrets.toml. It’s very important that you don’t reveal your token to anyone, as it can be abused. Create a .gitignore file to omit your Secrets.toml from version control.

To add the bot to a server we need to create an invite link.

  1. On your bot’s application page, open the OAuth2 page via the lefthand panel.
  2. Go to the URL Generator via the lefthand panel, and select the applications.commands scope.
  3. Copy the URL, open it in your browser and select a Discord server you wish to invite the bot to.

For this example we also need a GuildId.

  1. Open your Discord client, open the User Settings and navigate to Advanced. Enable Developer Mode.
  2. Right click the Discord server you’d like to use the bot in and click Copy Id. This is your Guild ID.
  3. Store it in Secrets.toml and retrieve it like we did for the Discord Token.

For more information please refer to the Discord docs as well as the Serenity repo for more examples.

use anyhow::Context as _;
use serenity::async_trait;
use serenity::model::application::command::CommandOptionType;
use serenity::model::application::interaction::application_command::CommandDataOptionValue;
use serenity::model::application::interaction::{Interaction, InteractionResponseType};
use serenity::model::gateway::Ready;
use serenity::model::id::GuildId;
use serenity::prelude::*;
use shuttle_secrets::SecretStore;
use sqlx::{Executor, PgPool};
use tracing::{error, info};

mod db;

struct Bot {
    database: PgPool,
    guild_id: String,

impl EventHandler for Bot {
    async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
        let user_id: i64 = interaction

        if let Interaction::ApplicationCommand(command) = interaction {
            info!("Received command interaction: {:#?}", command);

            let content = match {
                "todo" => {
                    let command ="Expected command");

                    // if the todo subcommand has a CommandOption the command is either `add` or `complete`
                    if let Some(subcommand) = command.options.get(0) {
                        match subcommand.resolved.as_ref().expect("Valid subcommand") {
                            CommandDataOptionValue::String(note) => {
                                db::add(&self.database, note, user_id).await.unwrap()
                            CommandDataOptionValue::Integer(index) => {
                                db::complete(&self.database, index, user_id)
                                    .unwrap_or_else(|_| {
                                        "Please submit a valid index from your todo list"
                            _ => "Please enter a valid todo".to_string(),
                    // if the todo subcommand doesn't have a CommandOption the command is `list`
                    } else {
                        db::list(&self.database, user_id).await.unwrap()
                _ => "Command not implemented".to_string(),

            if let Err(why) = command
                .create_interaction_response(&ctx.http, |response| {
                        .interaction_response_data(|message| message.content(content))
                error!("Cannot respond to slash command: {}", why);

    async fn ready(&self, ctx: Context, ready: Ready) {
        info!("{} is connected!",;

        let guild_id = GuildId(self.guild_id.parse().unwrap());

        let _ = GuildId::set_application_commands(&guild_id, &ctx.http, |commands| {
            commands.create_application_command(|command| {
                    .description("Add, list and complete todos")
                    .create_option(|option| {
                            .description("Add a new todo")
                            .create_sub_option(|option| {
                                    .description("The todo note to add")
                    .create_option(|option| {
                            .description("The todo to complete")
                            .create_sub_option(|option| {
                                    .description("The index of the todo to complete")
                    .create_option(|option| {
                            .description("List your todos")

async fn serenity(
    #[shuttle_shared_db::Postgres] pool: PgPool,
    #[shuttle_secrets::Secrets] secret_store: SecretStore,
) -> shuttle_serenity::ShuttleSerenity {
    // Get the discord token set in `Secrets.toml`
    let token = secret_store
        .context("'DISCORD_TOKEN' was not found")?;
    // Get the guild_id set in `Secrets.toml`
    let guild_id = secret_store
        .context("'GUILD_ID' was not found")?;

    // Run the schema migration
        .context("failed to run migrations")?;

    let bot = Bot {
        database: pool,
    let client = Client::builder(&token, GatewayIntents::empty())
        .expect("Err creating client");