Crate rust_discord_activity

source
Expand description

§Rust Discord Activity

A lightweight Rust library to control Discord Rich Presence

§Installation

Rust Discord Activity is available directly on crates.io: cargo add rust-discord-activity

§How to use

  1. Instantiate a new DiscordClient
  2. Create your Activity and set desired data using provided structs
  3. Create a new Payload with your Activity
  4. Send your Payload through the DiscordClient

Et voilà !

§Example

let mut client = DiscordClient::new("<application_id>");

let limg = Some(String::from("https://placehold.co/600x400/png"));
let simg = Some(String::from("https://placehold.co/200x100/png"));
let asset = Asset::new(limg, None, simg, None);
let now_in_millis = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis();
let timestamp = Timestamp::new(Some(now_in_millis - 10000), None);

let party = Party::new(None, Some((2, 4)));
let mut button_vec = vec![];
button_vec.push(Button::new("First Button".into(), "https://google.com".into()));
button_vec.push(Button::new("Second Button".into(), "https://yahoo.com".into()));

let mut activity = Activity::new();

activity
    .set_state(Some("This is State".into()))
    .set_activity_type(Some(ActivityType::LISTENING))
    .set_details(Some("This is Details".parse().unwrap()))
    .set_timestamps(Some(timestamp))
    .set_assets(Some(asset))
    .set_party(Some(party))
    .set_instance(Some(true))
    .set_buttons(Some(button_vec));

let payload = Payload::new(EventName::Activity, EventData::Activity(activity));

let _ = client.send_payload(payload);

And voilà! This sets-up a new Activity for the current Discord user:

Discord Rich Presence

§Limitations

For the moment, the library only works with MacOS and local Discord application.

§Next Steps

  • Write proper documentation for this library
  • Write unit tests

Structs§

  • Represents a Discord Activity object to be send to Discord application. See https://discord.com/developers/docs/game-sdk/activities#data-models for more information.
  • Contains Large and Small images of an Activity.
  • Simple structure containing a label and an URL to form a Discord Activity button.
  • Client used to communicate with Discord through IPC.
  • Contains data of an Emoji. ID must be a Discord Emoji ID.
  • Contains data of a Party.
  • Payload object used to encapsulate data to send to Discord Client.
  • Contains Secrets URIs to join, spectate or instantiate a match through Discord Chat.
  • Contains start and end time for an Activity. Must be in Milliseconds since UNIX_EPOCH time. If only Start is set and is in the past, it will display “xx:xx elapsed” Otherwise if End is set, it will display “xx:xx remaining”

Enums§

  • List of Activity Flags to send to Discord Client.
  • List of Activity Type - Only Game is supported for the moment.
  • List of EventData to send to Discord - Currently only supports Activity.
  • List of EventName to send to Discord - Currently only supports Activity.