Revision as of 07:24, 14 February 2026 by Lucash (talk | contribs) (Created page with "== Preface == Rust is a compiled, strongly and statically typed general purpose programming language. Compared to Python, Rust applications must be compiled for each operating system and architecture combination, and all variables defined should have a single type. To learn more about Rust, read [https://doc.rust-lang.org/book/ "The Book"], or follow a more [https://doc.rust-lang.org/rust-by-example/ interactive learning guide] with example code and output. == Getting...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Discord Bot - Rust

Preface

Rust is a compiled, strongly and statically typed general purpose programming language. Compared to Python, Rust applications must be compiled for each operating system and architecture combination, and all variables defined should have a single type.

To learn more about Rust, read "The Book", or follow a more interactive learning guide with example code and output.

Getting Started

What to expect in this section:

  1. Installing Rust and Cargo
  2. Setting up a Rust project
  3. Installing needed dependencies (also known as "crates")

To get started, install Rust and Cargo (the package manager for Rust) using rustup.

Once installed, create a new project by using cargo init ping-pong-rs.

Running cargo init ping-pong-rs will create a folder named ping-pong-rs with the basic structure of a Rust project.

Next, we need to install some dependencies (crates). For this tutorial, we'll be using serenity to interact with Discord, tokio with the "full" feature to manage asynchronous behavior, and dotenvy to read your Discord bot token from a file.

To install these crates, use cargo add dotenvy tokio serenity -F tokio/full. Once you have the crates installed, you are now ready to code!

Writing the Discord Bot

What to expect in this section:

  1. Creating an example ping-pong bot
  2. Building and testing your Discord bot on your machine

We'll be using a modified version of serenity's sample ping-pong bot. Open the main.rs file located in the src folder and paste the following code inside of it:

use std::env;

use dotenvy::dotenv;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::prelude::*;

struct Handler;

#[async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        if msg.content == "!ping" {
            if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
                println!("Error sending message: {why:?}");
            }
        }
    }
}

#[tokio::main]
async fn main() {
    // Load all variables from a local environment variable file
    dotenv().expect("Expected a .env file to be created");

    // Login with a bot token from the environment
    let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

    // Set gateway intents, which decides what events the bot will be notified about
    let intents = GatewayIntents::GUILD_MESSAGES
        | GatewayIntents::DIRECT_MESSAGES
        | GatewayIntents::MESSAGE_CONTENT;

    // Create a new instance of the Client, logging in as a bot.
    let mut client = Client::builder(&token, intents)
        .event_handler(Handler)
        .await
        .expect("Err creating client");

    println!("Starting bot");

    // Start listening for events by starting a single shard
    if let Err(why) = client.start().await {
        println!("Client error: {why:?}");
    }
}

Next, you'll need to copy your Discord bot token into a file named .env. This file should be located in the same place as the src folder, NOT inside of it.

The environment file should contain your token:

DISCORD_TOKEN=your_discord_bot_token

Finally, you are now ready to run your bot locally to test it out! To run your bot, run cargo run. The run command will build and run your bot locally. Remember, Rust is a compiled language. This means that your bot must be compiled before it can be run, this can take around a minute.

lucas@fozziebear:~/heliohost/ping-pong-rs$ cargo run
   Compiling proc-macro2 v1.0.106
   Compiling quote v1.0.44
   .
   .
   .
   Compiling hyper-rustls v0.27.7
   Compiling reqwest v0.12.28
   Compiling ping-pong-rs v0.1.0 (/home/lucas/heliohost/ping-pong-rs)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 41.00s
     Running `target/debug/ping-pong-rs`
Starting bot

You should see the message Starting bot at the end if everything compiled successfully.

Once your project is up and running, return to Discord and type !ping in the channel your bot is located in and see it return your message!

Building your Discord Bot for HelioHost

What to expect in this section:

  1. How to build the correct file to work in HelioHost

Rust applications must be built to run on the machine that you want to run your application on. In simple terms, this means that to run a Rust Discord bot on HelioHost, you should build a Linux x86_64 application.


This page was last edited on 14 February 2026, at 07:24.