wip refactoring
This commit is contained in:
@@ -27,9 +27,15 @@ impl Retrive {
|
||||
return error_msg(Box::new(e));
|
||||
}
|
||||
|
||||
let server_db_id = server::Model::get_by_discord_id(guild_id, db).await;
|
||||
|
||||
if let Err(e) = server_db_id {
|
||||
return error_msg(Box::new(e));
|
||||
}
|
||||
|
||||
let invoker_messages = messages::Entity::find()
|
||||
.filter(messages::Column::IdSender.eq(invoker_db.unwrap().unwrap().id))
|
||||
.filter(messages::Column::IdServer.eq(server_db_id))
|
||||
.filter(messages::Column::IdServer.eq(server_db_id.unwrap().unwrap().id))
|
||||
.all(db)
|
||||
.await;
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
use std::error::Error;
|
||||
|
||||
use entities::{channel, content, members, messages, server};
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
|
||||
use serenity::all::{Context, Message};
|
||||
|
||||
pub async fn log_message(
|
||||
db: &DatabaseConnection,
|
||||
ctx: &Context,
|
||||
msg: Message,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
// TODO This is a mess, it should be split into its on function(s)
|
||||
msg.channel(&ctx.http).await.unwrap();
|
||||
|
||||
let content = content::Model::get_or_create(msg.content.clone(), db).await?;
|
||||
|
||||
let member =
|
||||
members::Model::get_or_create(msg.author.id.get().try_into()?, msg.author.name, db).await?;
|
||||
|
||||
let server_id = msg.guild_id.unwrap().get();
|
||||
let server_id_i32 = server_id.clone() as i64;
|
||||
let guild_name = msg
|
||||
.channel(&ctx.http)
|
||||
.await
|
||||
.unwrap()
|
||||
.guild()
|
||||
.unwrap()
|
||||
.name
|
||||
.clone();
|
||||
|
||||
let server_db = entities::server::Entity::find()
|
||||
.filter(entities::server::Column::IdDiscord.eq(server_id_i32))
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let server_db = match server_db {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
let activeModel = server::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
name: sea_orm::ActiveValue::Set(guild_name.to_string()),
|
||||
id_discord: sea_orm::ActiveValue::Set(server_id.clone() as i64),
|
||||
};
|
||||
activeModel.insert(db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
let channel_id = msg.channel(&ctx.http).await.unwrap().id().get() as i64;
|
||||
let channel = entities::channel::Entity::find()
|
||||
.filter(entities::channel::Column::IdDiscord.eq(channel_id))
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let channel = match channel {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
let activeModel = channel::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(channel_id as i64),
|
||||
};
|
||||
activeModel.insert(db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
println!(
|
||||
"{}, {}, {}, {}, {}",
|
||||
msg.id.get(),
|
||||
member,
|
||||
server.id,
|
||||
content,
|
||||
channel.id
|
||||
);
|
||||
|
||||
let activeModel = messages::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(msg.id.get() as i64),
|
||||
id_sender: sea_orm::ActiveValue::Set(member),
|
||||
id_server: sea_orm::ActiveValue::Set(server.id),
|
||||
id_content: sea_orm::ActiveValue::Set(content),
|
||||
id_channel: sea_orm::ActiveValue::Set(channel.id),
|
||||
};
|
||||
activeModel.insert(db).await;
|
||||
Ok(())
|
||||
}
|
||||
+4
-104
@@ -12,8 +12,11 @@ use serenity::model::channel::Message;
|
||||
use serenity::model::guild;
|
||||
use serenity::prelude::*;
|
||||
|
||||
use crate::logger::log_message;
|
||||
|
||||
mod command;
|
||||
mod common;
|
||||
mod logger;
|
||||
|
||||
struct Bot {
|
||||
db: DatabaseConnection,
|
||||
@@ -27,110 +30,7 @@ impl EventHandler for Bot {
|
||||
println!("Error sending message: {why:?}");
|
||||
}
|
||||
}
|
||||
// TODO This is a mess, it should be split into its on function(s)
|
||||
msg.channel(&ctx.http).await.unwrap();
|
||||
|
||||
let content = content::Entity::find()
|
||||
.filter(content::Column::Content.eq(msg.content.clone()))
|
||||
.one(&self.db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let content = match content {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
let activeModel = content::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
content: sea_orm::ActiveValue::Set(msg.content.clone()),
|
||||
r#type: sea_orm::ActiveValue::Set("".to_string()),
|
||||
};
|
||||
activeModel.insert(&self.db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
let member = members::Entity::find()
|
||||
.filter(members::Column::IdDiscord.eq(msg.author.id.get() as i64))
|
||||
.one(&self.db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let member = match member {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
let activeModel = members::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(msg.author.id.get() as i64),
|
||||
name: sea_orm::ActiveValue::Set(msg.author.name.clone()),
|
||||
};
|
||||
activeModel.insert(&self.db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
let server_id = msg.guild_id.unwrap().get();
|
||||
let server_id_i32 = server_id.clone() as i64;
|
||||
let guild_name = msg
|
||||
.channel(&ctx.http)
|
||||
.await
|
||||
.unwrap()
|
||||
.guild()
|
||||
.unwrap()
|
||||
.name
|
||||
.clone();
|
||||
let server = entities::server::Entity::find()
|
||||
.filter(entities::server::Column::IdDiscord.eq(server_id_i32))
|
||||
.one(&self.db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let server = match server {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
let activeModel = server::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
name: sea_orm::ActiveValue::Set(guild_name.to_string()),
|
||||
id_discord: sea_orm::ActiveValue::Set(server_id.clone() as i64),
|
||||
};
|
||||
activeModel.insert(&self.db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
let channel_id = msg.channel(&ctx.http).await.unwrap().id().get() as i64;
|
||||
let channel = entities::channel::Entity::find()
|
||||
.filter(entities::channel::Column::IdDiscord.eq(channel_id))
|
||||
.one(&self.db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let channel = match channel {
|
||||
Some(x) => x,
|
||||
None => {
|
||||
let activeModel = channel::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(channel_id as i64),
|
||||
};
|
||||
activeModel.insert(&self.db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
println!(
|
||||
"{}, {}, {}, {}, {}",
|
||||
msg.id.get(),
|
||||
member.id,
|
||||
server.id,
|
||||
content.id,
|
||||
channel.id
|
||||
);
|
||||
|
||||
let activeModel = messages::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(msg.id.get() as i64),
|
||||
id_sender: sea_orm::ActiveValue::Set(member.id),
|
||||
id_server: sea_orm::ActiveValue::Set(server.id),
|
||||
id_content: sea_orm::ActiveValue::Set(content.id),
|
||||
id_channel: sea_orm::ActiveValue::Set(channel.id),
|
||||
};
|
||||
activeModel.insert(&self.db).await.unwrap();
|
||||
// medo
|
||||
log_message(&self.db, &ctx, msg).await;
|
||||
}
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if let Interaction::Command(command) = interaction {
|
||||
|
||||
Reference in New Issue
Block a user