use crate::data::{GlobalData, MessageSource}; use serenity::model::id::UserId; use serenity::model::prelude::ChannelId; use serenity::model::prelude::Guild; use serenity::prelude::Context; use std::fs::File; use std::io::prelude::*; pub async fn send_msg_to_player_channels( ctx: &Context, guild: &Guild, global_data: &GlobalData, msg_source: MessageSource, msg: &str, pin: bool, ) { for player_data in &global_data.game_state.player_data { if let MessageSource::Player(channel_id) = msg_source { if channel_id == player_data.channel { continue; } } let channel = guild .channels .get(&ChannelId::from(player_data.channel)) .unwrap(); let msg = channel .send_message(&ctx.http, |m| m.content(&msg)) .await .unwrap(); if pin { // pin system messages msg.pin(&ctx.http).await.unwrap(); } } let host_channel = guild .channels .get(&ChannelId::from(global_data.cfg.host_channel)) .unwrap(); let source = match msg_source { MessageSource::Player(channel_id) => { let discord_id = global_data .game_state .get_player_from_channel(channel_id) .unwrap() .discord_id; let name = guild .members .get(&UserId::from(discord_id)) .unwrap() .display_name(); name.to_string() } MessageSource::Host => "Host".to_string(), MessageSource::Automated => "Automated".to_string(), }; host_channel .send_message(&ctx.http, |m| m.content(format!("({}): {}", source, msg))) .await .unwrap(); } pub fn save_game_state(global_data: &GlobalData) -> std::io::Result<()> { let s = toml::to_string_pretty(&global_data.game_state).unwrap(); let mut file = File::create(global_data.cfg.get_game_state_path())?; file.write_all(s.as_bytes()) } pub fn get_game_state(global_data: &mut GlobalData) -> std::io::Result<()> { let mut file = File::open(global_data.cfg.get_game_state_path())?; let mut data = String::new(); file.read_to_string(&mut data)?; global_data.game_state = toml::from_str(&data).unwrap(); Ok(()) } pub fn clear_game_state(global_data: &mut GlobalData) -> std::io::Result<()> { global_data.game_state.clear(); let state_path = global_data.cfg.get_game_state_path(); if state_path.exists() { std::fs::remove_file(global_data.cfg.get_game_state_path())?; } Ok(()) }