79 lines
2.3 KiB
Rust
79 lines
2.3 KiB
Rust
mod data;
|
|
mod commands;
|
|
|
|
use serenity::prelude::*;
|
|
use serenity::model::prelude::{Message, Ready};
|
|
use std::sync::Arc;
|
|
use serenity::client::bridge::gateway::GatewayIntents;
|
|
use crate::data::{GlobalData, Args, BotConfig};
|
|
use serenity::async_trait;
|
|
use crate::commands::command_framework;
|
|
use structopt::StructOpt;
|
|
use serenity::model::id::ChannelId;
|
|
|
|
struct Handler {}
|
|
|
|
#[async_trait]
|
|
impl EventHandler for Handler {
|
|
async fn message(&self, ctx: Context, msg: Message) {
|
|
if msg.author.bot {
|
|
return;
|
|
}
|
|
|
|
let data = ctx.data.read().await;
|
|
|
|
let game_data = data.get::<GlobalData>().unwrap();
|
|
|
|
let game_data = game_data.lock().await;
|
|
|
|
|
|
if let Some(player_data) = game_data.game_state.player_channels.get(&msg.channel_id.0) {
|
|
let guild = msg.guild(&ctx.cache).await.unwrap();
|
|
let user_msg = format!("{} > {}", player_data.codename, msg.content);
|
|
|
|
for (channel, _) in &game_data.game_state.player_channels {
|
|
if *channel == msg.channel_id.0 {
|
|
continue;
|
|
}
|
|
|
|
let channel = guild.channels.get(&ChannelId::from(*channel)).unwrap();
|
|
|
|
channel.send_message(&ctx.http, |m| {
|
|
m
|
|
.content(&user_msg)
|
|
}).await.unwrap();
|
|
}
|
|
|
|
let host_channel = guild.channels.get(&ChannelId::from(game_data.cfg.host_channel)).unwrap();
|
|
|
|
host_channel.send_message(&ctx.http, |m| {
|
|
m
|
|
.content(&user_msg)
|
|
}).await.unwrap();
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
let bot_cfg: BotConfig = BotConfig::new(&args.cfg_path).expect("Unable to parse cfg");
|
|
|
|
let mut client = Client::builder(&bot_cfg.token)
|
|
.event_handler(Handler {})
|
|
.framework(command_framework())
|
|
.intents(GatewayIntents::all())
|
|
.type_map_insert::<GlobalData>(Arc::new(Mutex::new(GlobalData::new(bot_cfg))))
|
|
.await
|
|
.expect("Err creating client");
|
|
|
|
if let Err(why) = client.start().await {
|
|
println!("Client error: {:?}", why);
|
|
}
|
|
}
|