diff --git a/crates/bot/src/channelcommand.rs b/crates/bot/src/channelcommand.rs new file mode 100644 index 0000000..1aae31d --- /dev/null +++ b/crates/bot/src/channelcommand.rs @@ -0,0 +1,83 @@ +use std::collections::HashMap; + +use entities::{channel, members, messages, server}; +use sea_orm::{ColIdx, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter}; +use serenity::all::{ + CommandInteraction, Context, CreateCommand, CreateEmbed, CreateInteractionResponse, + CreateInteractionResponseMessage, InteractionContext, +}; + +use crate::common::error_msg; + +pub struct Channelretrive {} + +impl Channelretrive { + pub async fn run( + db: &DatabaseConnection, + ctx: &Context, + command: &CommandInteraction, + ) -> CreateInteractionResponse { + let invoker_id = command.user.id.get() as i64; + let channel_id = command.channel_id.get() as i64; + // let guild_id = command.guild_id.unwrap().get() as i64; + // let guild_name = command.guild_id.unwrap().name(&ctx.cache).unwrap(); + + let channel_db = channel::Model::get_or_create(channel_id, db).await; + + if let Err(e) = channel_db { + return error_msg(Box::new(e)); + } + + // let guild_db = server::Model::get_or_create(guild_id, guild_name, db).await; + // + // if let Err(e) = guild_db { + // return error_msg(Box::new(e)); + // } + + let messages_by_channels = messages::Entity::find() + .filter(messages::Column::IdChannel.eq(channel_db.unwrap())) + .all(db) + .await + .unwrap(); + + let mut user_lookup: HashMap = HashMap::new(); + let mut user_msg_amount: HashMap = HashMap::new(); + + for x in messages_by_channels { + if let Some(user) = user_lookup.get(&x.id_sender) { + if let Some(user_amount) = user_msg_amount.get(user) { + user_msg_amount.insert(user.to_string(), (user_amount + 1)); + } else { + user_msg_amount.insert(user.clone(), 1); + } + } else { + let memebr = members::Entity::find_by_id(x.id_sender) + .one(db) + .await + .unwrap() + .unwrap(); + user_lookup.insert(memebr.id, memebr.name.clone()); + user_msg_amount.insert(memebr.name, 1); + } + } + + let mut summary_string: String = String::new(); + for x in &user_msg_amount { + summary_string.push_str(format!("{} - {}\n", x.0, x.1).as_str()); + } + + let embed = CreateEmbed::new() + .title("Summary") + .description(format!("{}", summary_string)); + + let reply = CreateInteractionResponseMessage::new().embed(embed); + + CreateInteractionResponse::Message(reply) + } + pub fn register() -> CreateCommand { + CreateCommand::new("retriveChannel") + .name("channeldata") + .description("description") + .contexts(vec![InteractionContext::Guild]) + } +} diff --git a/crates/bot/src/fetchcommand.rs b/crates/bot/src/fetchcommand.rs new file mode 100644 index 0000000..86b2f78 --- /dev/null +++ b/crates/bot/src/fetchcommand.rs @@ -0,0 +1,18 @@ +use sea_orm::DatabaseConnection; +use serenity::all::{ + CommandInteraction, Context, CreateCommand, CreateInteractionResponse, InteractionContext, +}; + +pub struct Fetch; + +// This is "possible" but with ratelimiting, it'll take a long long long time to rip a medium sized +// server + +impl Fetch { + pub fn register() -> CreateCommand { + CreateCommand::new("retriveChannel") + .name("channeldata") + .description("description") + .contexts(vec![InteractionContext::Guild]) + } +} diff --git a/crates/bot/src/logger.rs b/crates/bot/src/logger.rs index 66283eb..a37fee8 100644 --- a/crates/bot/src/logger.rs +++ b/crates/bot/src/logger.rs @@ -3,7 +3,15 @@ use std::error::Error; use entities::{channel, content, members, messages, server}; use migration::prelude::{DateTime, Utc}; use sea_orm::{ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter}; -use serenity::all::{Context, Message}; +use serenity::all::{Context, Message, VoiceState}; + +pub async fn log_vc_event( + db: &DatabaseConnection, + ctx: &Context, + vc: VoiceState, +) -> Result<(), Box> { + Ok(()) +} pub async fn log_message( db: &DatabaseConnection, diff --git a/crates/bot/src/main.rs b/crates/bot/src/main.rs index 7ec3023..0569189 100644 --- a/crates/bot/src/main.rs +++ b/crates/bot/src/main.rs @@ -6,16 +6,18 @@ use migration::{Migrator, MigratorTrait}; use sea_orm::{ ActiveModelTrait, ColumnTrait, Database, DatabaseConnection, EntityTrait, Iden, QueryFilter, }; -use serenity::all::{Command, Interaction, Ready}; +use serenity::all::{Command, Interaction, Ready, VoiceState}; use serenity::async_trait; use serenity::model::channel::Message; use serenity::model::guild; use serenity::prelude::*; -use crate::logger::log_message; +use crate::logger::{log_message, log_vc_event}; +mod channelcommand; mod command; mod common; +mod fetchcommand; mod logger; struct Bot { @@ -32,15 +34,25 @@ impl EventHandler for Bot { } log_message(&self.db, &ctx, msg).await.unwrap(); } + async fn voice_state_update(&self, ctx: Context, _old: Option, new: VoiceState) { + log_vc_event(&self.db, &ctx, new).await.unwrap(); + } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { if let Interaction::Command(command) = interaction { println!("Received command interaction: {command:#?}"); + println!("{:#?}", command.data.name.as_str()); + let content = match command.data.name.as_str() { "userdata" => Some(command::Retrive::run(&self.db, &ctx, &command).await), + "channeldata" => { + Some(channelcommand::Channelretrive::run(&self.db, &ctx, &command).await) + } _ => Some(serenity::all::CreateInteractionResponse::Pong), }; + println!("{:#?}", content); + if let Some(content) = content { if let Err(why) = command.create_response(&ctx.http, content).await { println!("Cannot respond to slash command: {why}"); @@ -51,10 +63,12 @@ impl EventHandler for Bot { async fn ready(&self, ctx: Context, ready: Ready) { println!("{} is connected!", ready.user.name); - let global_command = - Command::create_global_command(&ctx.http, command::Retrive::register()).await; - - println!("I created the following global slash command: {global_command:#?}"); + Command::create_global_command(&ctx.http, command::Retrive::register()) + .await + .unwrap(); + Command::create_global_command(&ctx.http, channelcommand::Channelretrive::register()) + .await + .unwrap(); } } @@ -69,7 +83,8 @@ async fn main() -> Result<(), Box> { // Set gateway intents, which decides what events the bot will be notified about let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::DIRECT_MESSAGES - | GatewayIntents::MESSAGE_CONTENT; + | GatewayIntents::MESSAGE_CONTENT + | GatewayIntents::GUILD_VOICE_STATES; let bot = Bot { db: dbc }; diff --git a/notes.md b/notes.md new file mode 100644 index 0000000..fba652a --- /dev/null +++ b/notes.md @@ -0,0 +1,14 @@ +Id +Id_User +Event +Timestamp + +enum event : + - Joined Vc + - Left Vc + - Started Screenshare + - Stopped Screenshare + - Muted + - Unmuted + - Deafened + - Undeafened diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..7107950 --- /dev/null +++ b/todo.md @@ -0,0 +1 @@ +#TODO /channelstats shows the amount of users which sent msgs in the server with their name, e.i Doloro - 25 msgs