Skip to content

jay3332/rs-cord

Repository files navigation

rs-cord

rs-cord

A high-level Rust crate around the Discord API, aimed to be easy and straight-forward to use.
DocumentationCrates.ioDiscord


Navigation

Motive

Currenty, the leading crates that wrap around Discord's API (e.g. serenity), although some relatively high level, do not provide as much abstraction as I would like and requires lots of concepts - ones that are common in other frameworks - to be done manually.

Take asset/avatar handling. discord.py does this pretty well:

The equivalent of member.avatar.replace(format="png", size=2048) in an existing Rust crate, such as serenity would be:

format!("https://cdn.discordapp.com/avatars/{}/{}.png?size={}", member.user.id, member.user.avatar, 2048)  // Verbose for what?
// or
member.face().replace(".webp", ".png") + "?size=2048"  // Not elegant

(For clarification, I have nothing against serenity; it's a pretty good crate.)

rs-cord has been designed with two things in mind: elegance and performance.

Elegance

Assets

rs-cord takes a different approach to assets and avatars than serenity. The Asset struct will fulfill all your needs:

user.avatar().with_format("png").with_size(2048).url()

No more member.user.x

Discord's API has a nested user in member objects. Many Rust crates do not merge these two, which can cause confusion to the user.

rs-cord merges all fields from User into Member:

println!("Hello {}", member.name);

Installation

Just like with every other Rust crate, insert it into your Cargo.toml file:

[dependencies]
rs-cord = "0"

Installing from GitHub

If you would like to use the most recent update of rs-cord, make sure you have git installed and insert the following into your Cargo.toml:

[dependencies]
rs-cord = { git = "https://github.com/jay3332/rs-cord" }

Note that for production usage, it is recommended to stay with a stable version upload on crates.io.

Documentation for the GitHub version is available here.

Getting Started

You may see the examples folder for code examples.

Example Ping-pong Bot:

use rs_cord::{Client, ClientState, EventListener};
use rs_cord::events::{ReadyEvent, MessageCreateEvent};
use rs_cord::intents;

struct Listener;

#[rs_cord::async_trait]
impl EventListener for Listener {
    async fn ready(state: &ClientState, _event: &ReadyEvent) {
        println!("Logged in as {} (ID: {})", state.user.tag(), state.user.id);
    }

    async fn message_create(state: &ClientState, event: &MessageCreateEvent) {
        if event.message.content == "ping" {
            event.message.reply("Pong!");
        }
    }
}

#[tokio::main]
async fn main() {
    Client::new_with_token("my secret token")
        .with_intents(intents!(GUILDS, MESSAGES))
        .with_event_listener(Listener)
        .start()
        .await
        .expect("failed to start client");
}

Need help?

You can join our Discord server in order to get help on all things rs-cord.

Contributing

We appreciate all of your contributions! Please read CONTRIBUTING.md for instructions on how to contribute.

Links

About

A high-level Rust crate around the Discord API, aimed to be easy and straight-forward to use.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Languages