Startup → Docker Image → Rust. There are two ways to run it.
Way 1: we build it
Upload Cargo.toml, Cargo.lock, the src folder and .env (not target). On start we run cargo build --release and start your program.
The first build takes a few minutes; later builds only redo what changed. Building a Discord library needs about 1 GB of memory or more. If the console says Killed, use way 2.
Way 2: build on your computer (best for small plans)
rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-musl
Upload target/x86_64-unknown-linux-musl/release/<your bot> to the main folder, type its name in Main file, and turn off Install packages on start. Starts in a second, uses little memory.
The code
Cargo.toml:
[package]
name = "mybot"
version = "0.1.0"
edition = "2021"
[dependencies]
serenity = "0.12"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
dotenvy = "0.15"
src/main.rs:
use serenity::async_trait;
use serenity::model::gateway::Ready;
use serenity::prelude::*;
struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, _: Context, ready: Ready) {
println!("Logged in as {}", ready.user.name);
}
}
#[tokio::main]
async fn main() {
dotenvy::dotenv().ok();
let token = std::env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN is missing");
let mut client = Client::builder(&token, GatewayIntents::GUILDS)
.event_handler(Handler)
.await
.expect("could not create the client");
client.start().await.expect("the client stopped");
}
.env holds the token: DISCORD_TOKEN=your-token-here.
More help: Run your Discord bot 24/7.