-
Notifications
You must be signed in to change notification settings - Fork 7
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
Refactor for v3 #10
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa39b81
refactor for v3
catornot eeaefb0
fix bugs
catornot 53e7064
fmt and deny macros
catornot 0ada97f
a slightly better display name
catornot 9e87a38
match new sq function names
catornot c0a4316
nuke debug utils + rename presense to presence
catornot beef229
docs
catornot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ^^
There was a problem hiding this comment.
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.