Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for v3 #10

Merged
merged 7 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
335 changes: 180 additions & 155 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rrplug = { version = "2.0.0", features = ["presence"] }
rrplug = { git = "https://github.com/R2NorthstarTools/rrplug" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the current state of publishing to crates.io?

I'm fine with merging while still pointing to git repo but I'd kinda wanna see rrplug for v3 being on crates.io soon after so that it's more accessible to anyone developing plugins ^^

Copy link
Member Author

@catornot catornot Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can publish it at any moment since it's basically done, but I would rather not.
The main is issue is that I don't quite know how much traits/generics I need over macros.

currently it's mix of both. I want to remove as much macros as possible. Due to how rrplug has to interact with the plugin system and the engine for some cases it's hard to decide what would be better.

discord-sdk = "0.3.2"
tokio = "1.26.0"
parking_lot = "0.12.1"

[build-dependencies]
windres = "0.2.2"
Expand Down
7 changes: 4 additions & 3 deletions manifest/manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "Northstar.DiscordRPC",
"displayname": "DiscordRPC",
"displayname": "DPRESENCE",
"dependencyName": "DISCORDRPC",
"description": "Discord Rich Presence for Northstar",
"api_version": "2",
"version": "2.0",
"api_version": "3",
"version": "3.0",
"run_on_server": false,
"run_on_client": true
}
121 changes: 121 additions & 0 deletions src/discord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#![deny(non_snake_case)]

use std::num::NonZeroU32;

use discord_sdk::{
activity::{ActivityBuilder, Assets, PartyPrivacy},
user::User,
wheel::UserState,
wheel::Wheel,
Discord, DiscordApp, Subscriptions,
};
use rrplug::prelude::*;

use crate::exports::PLUGIN;

/// the discord app's id, taken from older v1 discord rpc
const APP_ID: i64 = 941428101429231617;

pub struct Client {
pub discord: Discord,
pub user: User,
pub wheel: Wheel,
}

pub async fn async_main() {
let activity = &PLUGIN.wait().activity;

let client = match make_client(Subscriptions::ACTIVITY).await {
Ok(c) => c,
Err(_) => {
log::error!("Is your discord running?");
return;
}
};

match client.discord.clear_activity().await {
Ok(_) => log::info!("cleared activity"),
Err(err) => log::error!("coudln't clear activity because of {:?}", err),
}

loop {
let data = activity.lock().clone();

if let Some(img) = &data.large_image {
if img.is_empty() {
log::warn!("img is empty at {:?}", data.last_state);
}
}

let mut activity_builder = ActivityBuilder::default()
.details(data.details)
.state(data.state)
.assets(Assets {
large_image: data.large_image,
large_text: data.large_text,
small_image: data.small_image,
small_text: data.small_text,
});

if let Some(start) = data.start {
activity_builder = activity_builder.start_timestamp(if start == 0 { 1 } else { start });
}
if let Some(end) = data.end {
activity_builder = activity_builder.end_timestamp(end);
}
if let Some(party) = data.party {
activity_builder = activity_builder.party(
"whar",
GeckoEidechse marked this conversation as resolved.
Show resolved Hide resolved
Some(party.0.try_into().unwrap_or(NonZeroU32::new(1).unwrap())),
Some(party.1.try_into().unwrap_or(NonZeroU32::new(1).unwrap())),
PartyPrivacy::Private,
);
}

// updates presence here
if let Err(err) = client.discord.update_activity(activity_builder).await {
log::info!("failed to updated discord activity; {err}");
#[cfg(not(debug_assertions))]
return;
}

wait(1000);
GeckoEidechse marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// discord connection init sourced from https://github.com/EmbarkStudios/discord-sdk/blob/d311db749b7e11cc55cb1a9d7bfd9a95cfe61fd1/examples-shared/src/lib.rs#L16
pub async fn make_client(subs: Subscriptions) -> Result<Client, ()> {
let (wheel, handler) = Wheel::new(Box::new(|err| {
log::warn!("encountered an error {err:?}; shouldn't be fatal");
}));

let mut user = wheel.user();

let discord = match Discord::new(DiscordApp::PlainId(APP_ID), subs, Box::new(handler)) {
Ok(d) => d,
Err(_) => {
log::error!("unable to create discord client");
Err(())?
}
};

log::info!("waiting for handshake...");
user.0.changed().await.unwrap();

let user = match &*user.0.borrow() {
UserState::Connected(user) => user.clone(),
UserState::Disconnected(err) => {
log::error!("failed to connect to Discord: {}", err);
Err(())?
}
};

#[cfg(debug_assertions)]
log::info!("connected to Discord, local user is {:#?}", user);

Ok(Client {
discord,
user,
wheel,
})
}
Loading