#activity #discord #activities #macro

macro activity-macros

Rust 中 Discord 活动的宏

3 个版本

0.1.3 2024年3月28日
0.1.2 2024年3月27日
0.1.1 2024年3月26日

过程宏 中排名 1174


用于 activity

MIT 许可协议

6KB

activities-rs

Ergonomic Rust 绑定到 Discord Embedded App SDK

开发中

  • 支持所有命令
  • 支持所有事件

示例用法

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(())
}

编译活动

有一个构建工具可以轻松编译和打包 Rust 活动。打包后的文件将位于 build/activity

或者您也可以使用带有 wasm-pack 插件的 webpack。请参阅 webpack 示例

# 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

依赖项

~1–2.8MB
~52K SLoC