Giter VIP home page Giter VIP logo

activities-rs's Introduction

activities-rs

Ergonomic Rust bindings to the Discord Embedded App SDK.

Features

  • Support for all commands
  • Support for all events
  • No JS required

Example Usage

use activity::*;
use std::{mem::forget, option_env};

#[activity]
pub async fn start() -> Result<(), JsValue> {
    console_log!("Starting activity...");

    let client_id = option_env!("CLIENT_ID").expect("CLIENT_ID environment variable must be set");
    let sdk = DiscordSDK::new(client_id)?;
    sdk.ready().await?;

    console_log!("Activity ready!");

    authenticate_user(&sdk).await?;

    let s = sdk
        .subscribe(
            |e: VoiceStateUpdateEvent| {
                console_log!("Voice state update: {:?}", e);
                Ok(())
            },
            SubscribeArgs::channel_id(sdk.channel_id().unwrap()),
        )
        .await?;

    // When the subscription is dropped, the event will be unsubscribed
    forget(s);

    Ok(())
}

async fn authenticate_user(sdk: &DiscordSDK) -> Result<(), JsValue> {
    let res = sdk
        .authorize(AuthorizeCommandArgs {
            client_id: sdk.client_id(),
            response_type: "code".to_string(),
            state: "".to_string(),
            prompt: "none".to_string(),
            scope: vec![
                "identify".to_string(),
                "guilds".to_string(),
                "rpc.voice.read".to_string(),
            ],
        })
        .await?;

    // The token exchange must happen through your own server. Implement this yourself!
    let access_token = exchange_token(&res.code).await?;

    let res = sdk
        .authenticate(AuthenticateCommandArgs { access_token })
        .await?;

    console_log!("Authenticated user: {:?}", res.user);

    Ok(())
}

Compiling an Activity

There is a build tool that makes it really easy to compile and bundle Rust activities. The finished bundle will be located in build/activity.

Alternatively you can also use webpack with the wasm-pack plugin. See the webpack example.

# Install the build tool
cargo install activity-build

# Set env vars
export CLIENT_ID=1234567890

# Run it in the directory where your `Cargo.toml` is located
activity-build --release

activities-rs's People

Contributors

merlinfuchs avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.