Moved event_handler to its own module and cleaned up readme

This commit is contained in:
2022-03-20 09:46:40 -06:00
parent 0baf273237
commit 673d5b56f0
4 changed files with 71 additions and 65 deletions
+66
View File
@@ -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::<GlobalData>().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<AttachmentType> = 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);
}
}
+1
View File
@@ -1,2 +1,3 @@
pub mod commands;
pub mod helper;
pub mod event_handler;