+ Instead of deleting channels, players are marked dead so they can't interact with the game + Players are listed as dead in the $players command + All player channels are cleaned up at the end of the game + clippy + fmt
91 lines
2.6 KiB
Rust
91 lines
2.6 KiB
Rust
use serenity::async_trait;
|
|
use serenity::client::{Context, EventHandler};
|
|
use serenity::http::AttachmentType;
|
|
use serenity::model::channel::Message;
|
|
use serenity::model::gateway::Ready;
|
|
use serenity::utils::parse_emoji;
|
|
|
|
use crate::discord::helper::send_webhook_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)
|
|
{
|
|
// Don't process message if the player is dead
|
|
if !player_data.alive {
|
|
return;
|
|
}
|
|
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
let user_msg = msg.content.clone();
|
|
|
|
let re = regex::Regex::new(r"<a?:.+:\d+>").unwrap();
|
|
|
|
for emoji_cap in re.captures_iter(&user_msg) {
|
|
if let Some(emoji) = parse_emoji(&emoji_cap[0]) {
|
|
if !msg
|
|
.guild(&ctx.cache)
|
|
.await
|
|
.unwrap()
|
|
.emojis
|
|
.contains_key(&emoji.id)
|
|
{
|
|
msg.reply(&ctx.http, "Your messages contains custom emojis from outside this guild and has been blocked.").await.unwrap();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
let attachments: Vec<AttachmentType> = msg
|
|
.attachments
|
|
.iter()
|
|
.map(|a| AttachmentType::Image(&a.url))
|
|
.collect();
|
|
|
|
let msg_source = MessageSource::Player(Box::new(player_data.clone()));
|
|
|
|
send_webhook_msg_to_player_channels(
|
|
&ctx,
|
|
&guild,
|
|
&mut global_data,
|
|
msg_source,
|
|
&user_msg,
|
|
Some(attachments),
|
|
)
|
|
.await
|
|
.expect("Unable to send message to players");
|
|
}
|
|
}
|
|
|
|
async fn ready(&self, _ctx: Context, ready: Ready) {
|
|
println!("{} is connected!", ready.user.name);
|
|
}
|
|
}
|