0.0.1-alpha.1 |
|
---|
#19 in #discord-rich-presence
28KB
627 代码行
discord-ipc-rust
从 sardonicism-04/discord-rich-presence 复制
为什么/目标?
- 使用
access_token
登录 - 发送RPC命令
- 接收事件/命令
示例
简单演示如何使用此工具
use discord_ipc::{
models::commands::*, Command, DiscordIpc, DiscordIpcClient, Event, EventReceive,
};
// get all messages from the client
fn handle_message(event: EventReceive) {
if let EventReceive::CommandReturn(event_type) = event {
match event_type {
BasedCommandReturn::GetSelectedVoiceChannel { data } => {
println!("{:#?}", data.guild_id);
for user in data.voice_states.iter() {
println!("{}", user.nick);
}
}
BasedCommandReturn::SelectVoiceChannel { .. } => todo!(),
_ => {
println!("{:#?}", event_type);
}
}
} else if let EventReceive::Event(event_type) = event {
println!("Evt {:#?}", event_type);
}
}
#[tokio::main]
async fn main() {
// load env vars
dotenv::dotenv().ok();
// access token from env
let access_token = dotenv::var("ACCESS_TOKEN").unwrap();
// client id from env
let client_id = dotenv::var("CLIENT_ID").unwrap();
// connect to discord client with overlayed id
let mut client = DiscordIpcClient::new(&client_id)
.await
.expect("Client failed to connect");
// login to the client
client.login(access_token).await.unwrap();
// test join a voice channel
client
.emit(Command::get_selected_voice_channel())
.await
.ok();
client
.emit(Event::speaking_start_event("1022132922565804062"))
.await
.ok();
client
.emit(Event::speaking_stop_event("1022132922565804062"))
.await
.ok();
// sub to all events to via this listener
client.handler(handle_message).await.ok();
}
设置
确保添加一个包含有效访问令牌和客户端ID的 .env
文件。
ACCESS_TOKEN="dank_meme"
CLIENT_ID="42069420"
本地运行
运行 make
或 carco run
以运行主文件
另请参阅
https://github.com/sardonicism-04/discord-rich-presence https://gitlab.com/valeth/discord-rpc-client.rs https://github.com/ldesgoui/discord_game_sdk https://github.com/jewlexx/discord-presence
依赖关系
约4-11MB
约110K SLoC