Moved event_handler to its own module and cleaned up readme

msg_refactor
Joey Hines 2022-03-20 09:46:40 -06:00
parent 0baf273237
commit 673d5b56f0
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
4 changed files with 71 additions and 65 deletions

View File

@ -19,13 +19,13 @@ The game proceeds as a normal Werewolf game.
## Commands
## Host
* `$start <list of player ids>` - starts the game
* `$start <hours>h<minutes>m <list of player ids>` - starts the game
* `$end` - Ends the current game
* `$say <msg>` - Allows the host to speak into the game chat
* `$broadcast <msg>` - Broadcasts a system message, this message is then pinned in each player channel
* `$next_phase <duration> <msg>` - Send the next phase message. Also cycles the phase
* `$next_phase <hours>h<minutes>m <msg>` - Send the next phase message. Also cycles the phase
* `$terminate <player>` - Kills a player and removes them from the game
* `$add_time <duration>` - Adds more time to the current game
* `$add_time <hours>h<minutes>m` - Adds more time to the current game
## Players
* `$vote <player>` - Casts a vote for a player to be terminated. Only can be used during the day phase

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);
}
}

View File

@ -1,2 +1,3 @@
pub mod commands;
pub mod helper;
pub mod event_handler;

View File

@ -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();