mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2025-03-20 07:00:00 +03:00
Discord RPC: fixed connection / disconnection on game launch
This commit is contained in:
parent
ab27e072f4
commit
64921ea3cb
3 changed files with 20 additions and 12 deletions
|
@ -17,8 +17,8 @@ impl Default for DiscordRpc {
|
|||
app_id: 901534333360304168,
|
||||
enabled: false,
|
||||
|
||||
title: String::from("of Teyvat"),
|
||||
subtitle: String::from("Researching the world"),
|
||||
title: String::from("Researching the world"),
|
||||
subtitle: String::from("of Teyvat"),
|
||||
image: String::from("gi-icon")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,11 +42,13 @@ impl DiscordRpc {
|
|||
let mut client = DiscordIpcClient::new(&config.app_id.to_string())
|
||||
.expect("Failed to register discord ipc client");
|
||||
|
||||
let mut connected = false;
|
||||
|
||||
while let Ok(update) = receiver.recv() {
|
||||
match update {
|
||||
RpcUpdates::Connect => {
|
||||
if !config.enabled {
|
||||
config.enabled = true;
|
||||
if !connected {
|
||||
connected = true;
|
||||
|
||||
client.connect().expect("Failed to connect to discord");
|
||||
|
||||
|
@ -56,8 +58,8 @@ impl DiscordRpc {
|
|||
}
|
||||
|
||||
RpcUpdates::Disconnect => {
|
||||
if config.enabled {
|
||||
config.enabled = false;
|
||||
if connected {
|
||||
connected = false;
|
||||
|
||||
client.close().expect("Failed to disconnect from discord");
|
||||
}
|
||||
|
@ -68,14 +70,16 @@ impl DiscordRpc {
|
|||
config.subtitle = subtitle;
|
||||
config.image = image;
|
||||
|
||||
if config.enabled {
|
||||
if connected {
|
||||
client.set_activity(Self::get_activity(&config))
|
||||
.expect("Failed to update discord rpc activity");
|
||||
}
|
||||
}
|
||||
|
||||
RpcUpdates::ClearActivity => {
|
||||
client.clear_activity().expect("Failed to clear discord rpc activity");
|
||||
if connected {
|
||||
client.clear_activity().expect("Failed to clear discord rpc activity");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +90,8 @@ impl DiscordRpc {
|
|||
|
||||
pub fn get_activity(config: &DiscordRpcConfig) -> Activity {
|
||||
Activity::new()
|
||||
.state(&config.title)
|
||||
.details(&config.subtitle)
|
||||
.details(&config.title)
|
||||
.state(&config.subtitle)
|
||||
.assets(Assets::new().large_image(&config.image))
|
||||
}
|
||||
|
||||
|
|
|
@ -358,7 +358,9 @@ impl App {
|
|||
else {
|
||||
std::thread::sleep(std::time::Duration::from_secs(2));
|
||||
|
||||
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Connect);
|
||||
if config.launcher.discord_rpc.enabled {
|
||||
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Connect);
|
||||
}
|
||||
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(3));
|
||||
|
@ -375,7 +377,9 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Disconnect);
|
||||
if config.launcher.discord_rpc.enabled {
|
||||
this.widgets.preferences_stack.enhancements_page.discord_rpc.update(RpcUpdates::Disconnect);
|
||||
}
|
||||
|
||||
this.widgets.window.show();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue