diff --git a/README.md b/README.md index 60c1021..28a3647 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ The game proceeds as a normal Werewolf game. ## Commands ## Host -* `$start ` - starts the game +* `$start hm ` - starts the game * `$end` - Ends the current game * `$say ` - Allows the host to speak into the game chat * `$broadcast ` - Broadcasts a system message, this message is then pinned in each player channel -* `$next_phase ` - Send the next phase message. Also cycles the phase +* `$next_phase hm ` - Send the next phase message. Also cycles the phase * `$terminate ` - Kills a player and removes them from the game -* `$add_time ` - Adds more time to the current game +* `$add_time hm` - Adds more time to the current game ## Players * `$vote ` - Casts a vote for a player to be terminated. Only can be used during the day phase diff --git a/src/discord/event_handler.rs b/src/discord/event_handler.rs new file mode 100644 index 0000000..ed3ad4d --- /dev/null +++ b/src/discord/event_handler.rs @@ -0,0 +1,66 @@ +use serenity::async_trait; +use serenity::client::{Context, EventHandler}; +use serenity::http::AttachmentType; +use serenity::model::channel::Message; +use serenity::model::gateway::Ready; + +use crate::discord::helper::send_msg_to_player_channels; +use crate::game::global_data::GlobalData; +use crate::game::MessageSource; + +pub struct Handler {} + +#[async_trait] +impl EventHandler for Handler { + async fn message(&self, ctx: Context, msg: Message) { + if msg.author.bot { + return; + } + + if msg.content.starts_with('$') { + return; + } + + let data = ctx.data.read().await; + + let global_data = data.get::().unwrap(); + + let mut global_data = global_data.lock().await; + + if global_data.game_state.is_none() { + // no game in progress + return; + } + + if let Some(player_data) = global_data + .game_state() + .unwrap() + .get_player_from_channel(msg.channel_id.0) + { + let guild = msg.guild(&ctx.cache).await.unwrap(); + let user_msg = format!("**{}** > {}", player_data.codename, msg.content); + + let attachments: Vec = msg + .attachments + .iter() + .map(|a| AttachmentType::Image(&a.url)) + .collect(); + + send_msg_to_player_channels( + &ctx, + &guild, + &mut global_data, + MessageSource::Player(msg.channel_id.0), + &user_msg, + Some(attachments), + false, + ) + .await + .expect("Unable to send message to players"); + } + } + + async fn ready(&self, _ctx: Context, ready: Ready) { + println!("{} is connected!", ready.user.name); + } +} diff --git a/src/discord/mod.rs b/src/discord/mod.rs index c31adff..2b8ae18 100644 --- a/src/discord/mod.rs +++ b/src/discord/mod.rs @@ -1,2 +1,3 @@ pub mod commands; pub mod helper; +pub mod event_handler; diff --git a/src/main.rs b/src/main.rs index 4ed9c07..441a311 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,12 @@ use std::sync::Arc; -use serenity::async_trait; use serenity::client::bridge::gateway::GatewayIntents; -use serenity::http::AttachmentType; -use serenity::model::prelude::{Message, Ready}; use serenity::prelude::*; use structopt::StructOpt; use discord::commands::command_framework; -use discord::helper::send_msg_to_player_channels; +use discord::event_handler::Handler; use game::global_data::GlobalData; -use game::MessageSource; use crate::config::{Args, BotConfig}; @@ -19,63 +15,6 @@ mod discord; mod error; mod game; -struct Handler {} - -#[async_trait] -impl EventHandler for Handler { - async fn message(&self, ctx: Context, msg: Message) { - if msg.author.bot { - return; - } - - if msg.content.starts_with('$') { - return; - } - - let data = ctx.data.read().await; - - let global_data = data.get::().unwrap(); - - let mut global_data = global_data.lock().await; - - if global_data.game_state.is_none() { - // no game in progress - return; - } - - if let Some(player_data) = global_data - .game_state() - .unwrap() - .get_player_from_channel(msg.channel_id.0) - { - let guild = msg.guild(&ctx.cache).await.unwrap(); - let user_msg = format!("**{}** > {}", player_data.codename, msg.content); - - let attachments: Vec = msg - .attachments - .iter() - .map(|a| AttachmentType::Image(&a.url)) - .collect(); - - send_msg_to_player_channels( - &ctx, - &guild, - &mut global_data, - MessageSource::Player(msg.channel_id.0), - &user_msg, - Some(attachments), - false, - ) - .await - .expect("Unable to send message to players"); - } - } - - async fn ready(&self, _ctx: Context, ready: Ready) { - println!("{} is connected!", ready.user.name); - } -} - #[tokio::main] async fn main() { let args: Args = Args::from_args();