#discord #api #async

nightly automate

An asynchronous library to interact with Discord API

4 releases

✓ Uses Rust 2018 edition

0.1.3 Nov 29, 2019
0.1.2 Nov 20, 2019
0.1.1 Nov 15, 2019
0.1.0 Nov 15, 2019

#187 in Asynchronous

23 downloads per month

MIT license

130KB
3K SLoC

Automate   Actions Status GitHub issues Crates.io Crates.io

Automate is a low level and asynchronous rust library for interacting with the Discord API

DISCLAIMER: This crate is at its very early stage and does not have most functionalities a discord bot would need (no voice, and only channel manipulation operations). Please don't use this crate except for experimenting. The crate also only works in rust nightly.

Getting started

Automate is currently unstable and only works with Rust nightly. The minimum tested version and the one used in CI is nightly-2019-11-24. Refer to rust edition guide to learn how to switch to rust nightly.

In order to use Automate in your project, add the following line to your Cargo.toml under the [dependencies] section :

automate = "0.1.3"

You can then write the following in your main.rs. This simple example will respond Hello ! to any user posting a message in any channel while ignoring messages from bots. In order for this example to work, you need to define the DISCORD_API_TOKEN environment variable. You can create a bot and generate a token on Discord's developers portal.

#![allow(where_clauses_object_safety)]

use automate::async_trait;
use automate::{Error, Discord, Listener, Session};
use automate::gateway::MessageCreateDispatch;
use automate::http::CreateMessage;
use std::env;

struct MessageListener;

#[async_trait]
impl Listener for MessageListener {
    async fn on_message_create(&mut self, session: &Session, message: &MessageCreateDispatch) -> Result<(), Error> {
        let message = &message.0;

        if message.author.id != session.bot().id {
            let content = Some(format!("Hello {}!", message.author.username));

            session.create_message(message.channel_id, CreateMessage {
                content,
                ..Default::default()
            }).await?;
        }

        Ok(())
    }
}

fn main() -> Result<(), Error> {
    automate::setup_logging();

    Discord::new(&env::var("DISCORD_API_TOKEN").expect("API token not found"))
        .with_listener(Box::new(MessageListener))
        .connect_blocking()?;
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Automate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~12MB
~245K SLoC