Moved event_handler to its own module and cleaned up readme
This commit is contained in:
@@ -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,2 +1,3 @@
|
||||
pub mod commands;
|
||||
pub mod helper;
|
||||
pub mod event_handler;
|
||||
|
||||
+1
-62
@@ -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::<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);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args: Args = Args::from_args();
|
||||
|
||||
Reference in New Issue
Block a user