Module game_logic::discord[][src]

Expand description

Interfacing with Discord

Overview

When the game is run at the same time as a Discord client on a computer, it will attach to the user’s account and display Rich Presence information.

This is handled through the discord-sdk crate, but still requires some additional code to get everything set up.

Our main focuses in this module are:

  • Ensuring that the game does not crash when Discord is not running
  • Ensuring that Discord can not pause the game by taking too long to respond to an update

To solve these, we run this task in its own thread, and talk to it through Tokio’s mpsc implementation (as we are already working in an async context).

Usage

let app_id = 123456789;
 
// Connect to discord
let discord = DiscordRpcThreadHandle::new(app_id).await.unwrap();
let event_loop_discord_tx = discord.get_channel();
 
// When this variable is dropped, the connection is closed, so keep this around
let discord_task_handle = discord.begin_thread_non_blocking();
 
// We can then send signals any time we want
event_loop_discord_tx.send(DiscordRpcSignal::BeginGameTimer).await.unwrap();

Re-exports

pub use signal::DiscordRpcSignal;
pub use ipc::DiscordError;

Modules

Discord Rich Presence utilities

This file contains a system for signaling Discord RPC context changes between threads.

Structs

Constants

How long to wait before we give up on connecting to Discord.

Type Definitions

A cross-thread communication channel for sending Discord RPC events.