code clean up and error fixing
This commit is contained in:
+13
-43
@@ -8,6 +8,8 @@ use serenity::all::{
|
||||
InteractionContext, Message, MessageBuilder, MessageId, ResolvedOption,
|
||||
};
|
||||
|
||||
use crate::common::{self, error_msg};
|
||||
|
||||
pub struct Retrive {}
|
||||
|
||||
impl Retrive {
|
||||
@@ -16,57 +18,25 @@ impl Retrive {
|
||||
ctx: &Context,
|
||||
command: &CommandInteraction,
|
||||
) -> CreateInteractionResponse {
|
||||
let invoker_id = command.user.id.get() as i32;
|
||||
let guild_id = command.guild_id.unwrap().get() as i32;
|
||||
let invoker_id = command.user.id.get() as i64;
|
||||
let guild_id = command.guild_id.unwrap().get() as i64;
|
||||
|
||||
let user_db_id = members::Entity::find()
|
||||
.filter(members::Column::IdDiscord.eq(invoker_id))
|
||||
.one(db)
|
||||
.await;
|
||||
let invoker_db = members::Model::get_by_discord_id(invoker_id, db).await;
|
||||
|
||||
let user_db_id = match user_db_id {
|
||||
Ok(x) => match x {
|
||||
Some(x) => x.id,
|
||||
None => -1,
|
||||
},
|
||||
Err(e) => -1,
|
||||
};
|
||||
|
||||
if user_db_id == -1 {
|
||||
let embed = CreateEmbed::new()
|
||||
.title("Error!")
|
||||
.description("You're not in our database :c")
|
||||
.footer(CreateEmbedFooter::new("..or we ran into an error"))
|
||||
.color(Color::RED);
|
||||
let reply = CreateInteractionResponse::Message(
|
||||
CreateInteractionResponseMessage::new().embed(embed),
|
||||
);
|
||||
return reply;
|
||||
if let Err(e) = invoker_db {
|
||||
return error_msg(Box::new(e));
|
||||
}
|
||||
|
||||
let server_db_id = server::Entity::find()
|
||||
.filter(server::Column::IdDiscord.eq(guild_id))
|
||||
.one(db)
|
||||
.await;
|
||||
|
||||
let server_db_id = match server_db_id {
|
||||
Ok(x) => match x {
|
||||
Some(x) => x.id,
|
||||
None => -1,
|
||||
},
|
||||
Err(e) => -1,
|
||||
};
|
||||
|
||||
let invoker_messages = messages::Entity::find()
|
||||
.filter(messages::Column::IdSender.eq(user_db_id))
|
||||
.filter(messages::Column::IdSender.eq(invoker_db.unwrap().unwrap().id))
|
||||
.filter(messages::Column::IdServer.eq(server_db_id))
|
||||
.all(db)
|
||||
.await;
|
||||
|
||||
// TODO error checking here
|
||||
let invoker_messages = invoker_messages.unwrap();
|
||||
let mut map: HashMap<i32, i32> = HashMap::new(); // channel - amount
|
||||
let mut map_channel: HashMap<i32, i32> = HashMap::new(); // db_id - discord_id
|
||||
let mut map: HashMap<i64, i32> = HashMap::new(); // channel - amount
|
||||
let mut map_channel: HashMap<i32, i64> = HashMap::new(); // db_id - discord_id
|
||||
for msg in invoker_messages {
|
||||
// Get discord id of channel from db then store in map for cache
|
||||
if map_channel.get(&msg.id_channel).is_none() {
|
||||
@@ -75,7 +45,7 @@ impl Retrive {
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
map_channel.insert(msg.id_channel, channel_db.id_discord);
|
||||
map_channel.insert(msg.id_channel, channel_db.id_discord as i64);
|
||||
}
|
||||
let channel_discord_id = map_channel.get(&msg.id_channel).unwrap();
|
||||
if let Some(count) = map.get(channel_discord_id) {
|
||||
@@ -88,12 +58,12 @@ impl Retrive {
|
||||
|
||||
let mut summary_string: String = String::new();
|
||||
for x in map {
|
||||
summary_string.push_str(format!("#{} -- {} \n", x.0, x.1).as_str());
|
||||
summary_string.push_str(format!("#{} | {} \n", x.0, x.1).as_str());
|
||||
}
|
||||
|
||||
let embed = CreateEmbed::new()
|
||||
.title("Summary")
|
||||
.description(format!("{} msgs in this server", summary_string));
|
||||
.description(format!("{}", summary_string));
|
||||
|
||||
let reply = CreateInteractionResponseMessage::new().embed(embed);
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
use std::error::Error;
|
||||
|
||||
use serenity::all::{
|
||||
Color, CreateEmbed, CreateEmbedFooter, CreateInteractionResponse,
|
||||
CreateInteractionResponseMessage,
|
||||
};
|
||||
|
||||
pub fn error_msg(e: Box<dyn Error>) -> CreateInteractionResponse {
|
||||
let embed = CreateEmbed::new()
|
||||
.title("Error!")
|
||||
.description("we ran into an error :c")
|
||||
.footer(CreateEmbedFooter::new(format!("{}", e)))
|
||||
.color(Color::RED);
|
||||
let reply =
|
||||
CreateInteractionResponse::Message(CreateInteractionResponseMessage::new().embed(embed));
|
||||
return reply;
|
||||
}
|
||||
+48
-23
@@ -4,7 +4,7 @@ use std::error::Error;
|
||||
use entities::{channel, content, members, messages, server};
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, Database, DatabaseConnection, EntityTrait, QueryFilter,
|
||||
ActiveModelTrait, ColumnTrait, Database, DatabaseConnection, EntityTrait, Iden, QueryFilter,
|
||||
};
|
||||
use serenity::all::{Command, Interaction, Ready};
|
||||
use serenity::async_trait;
|
||||
@@ -13,6 +13,7 @@ use serenity::model::guild;
|
||||
use serenity::prelude::*;
|
||||
|
||||
mod command;
|
||||
mod common;
|
||||
|
||||
struct Bot {
|
||||
db: DatabaseConnection,
|
||||
@@ -28,30 +29,45 @@ impl EventHandler for Bot {
|
||||
}
|
||||
// TODO This is a mess, it should be split into its on function(s)
|
||||
msg.channel(&ctx.http).await.unwrap();
|
||||
let activeModel = content::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
content: sea_orm::ActiveValue::Set(msg.content.clone()),
|
||||
r#type: sea_orm::ActiveValue::Set("Content".into()),
|
||||
};
|
||||
let content = activeModel.insert(&self.db).await.unwrap();
|
||||
|
||||
let member = members::Entity::find()
|
||||
.filter(members::Column::IdDiscord.eq(msg.author.id.get() as i32))
|
||||
let content = content::Entity::find()
|
||||
.filter(content::Column::Content.eq(msg.content.clone()))
|
||||
.one(&self.db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if member.is_none() {
|
||||
let activeModel = members::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(msg.author.id.get() as i32),
|
||||
name: sea_orm::ActiveValue::Set(msg.author.name.clone()),
|
||||
};
|
||||
let member = activeModel.insert(&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 i32;
|
||||
let server_id_i32 = server_id.clone() as i64;
|
||||
let guild_name = msg
|
||||
.channel(&ctx.http)
|
||||
.await
|
||||
@@ -72,13 +88,13 @@ impl EventHandler for Bot {
|
||||
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 i32),
|
||||
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 i32;
|
||||
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)
|
||||
@@ -90,16 +106,25 @@ impl EventHandler for Bot {
|
||||
None => {
|
||||
let activeModel = channel::ActiveModel {
|
||||
id: sea_orm::ActiveValue::NotSet,
|
||||
id_discord: sea_orm::ActiveValue::Set(channel_id),
|
||||
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 i32),
|
||||
id_sender: sea_orm::ActiveValue::Set(member.unwrap().id),
|
||||
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),
|
||||
|
||||
@@ -7,7 +7,7 @@ use sea_orm::entity::prelude::*;
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub id_discord: i32,
|
||||
pub id_discord: i64,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
|
||||
@@ -8,10 +8,30 @@ pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub id_discord: i32,
|
||||
pub id_discord: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub async fn get_by_discord_id(
|
||||
id: i64,
|
||||
db: &DatabaseConnection,
|
||||
) -> Result<Option<Model>, DbErr> {
|
||||
let user_db_id = Entity::find()
|
||||
.filter(Column::IdDiscord.eq(id))
|
||||
.one(db)
|
||||
.await;
|
||||
|
||||
match user_db_id {
|
||||
Ok(x) => match x {
|
||||
Some(x) => Ok(Some(x)),
|
||||
None => Ok(None),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ use sea_orm::entity::prelude::*;
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub id_discord: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub id_discord: i64,
|
||||
pub id_sender: i32,
|
||||
pub id_content: i32,
|
||||
pub id_channel: i32,
|
||||
|
||||
@@ -8,10 +8,30 @@ pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
#[sea_orm(unique)]
|
||||
pub id_discord: i32,
|
||||
pub id_discord: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub async fn get_by_discord_id(
|
||||
id: i64,
|
||||
db: &DatabaseConnection,
|
||||
) -> Result<Option<Model>, DbErr> {
|
||||
let server_db_id = Entity::find()
|
||||
.filter(Column::IdDiscord.eq(id))
|
||||
.one(db)
|
||||
.await;
|
||||
|
||||
match server_db_id {
|
||||
Ok(x) => match x {
|
||||
Some(x) => Ok(Some(x)),
|
||||
None => Ok(None),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ impl MigrationTrait for Migration {
|
||||
.table(Server::Table)
|
||||
.if_not_exists()
|
||||
.col(pk_auto(Server::Id))
|
||||
.col(integer(Server::IdDiscord).unique_key())
|
||||
.col(big_integer(Server::IdDiscord).unique_key())
|
||||
.col(string(Server::Name))
|
||||
.to_owned(),
|
||||
)
|
||||
|
||||
@@ -12,7 +12,7 @@ impl MigrationTrait for Migration {
|
||||
.table(Members::Table)
|
||||
.if_not_exists()
|
||||
.col(pk_auto(Members::Id))
|
||||
.col(integer(Members::IdDiscord).unique_key())
|
||||
.col(big_integer(Members::IdDiscord).unique_key())
|
||||
.col(string(Members::Name))
|
||||
.to_owned(),
|
||||
)
|
||||
|
||||
@@ -15,7 +15,7 @@ impl MigrationTrait for Migration {
|
||||
.table(Messages::Table)
|
||||
.if_not_exists()
|
||||
.col(pk_auto(Messages::Id))
|
||||
.col(integer(Messages::IdDiscord))
|
||||
.col(big_integer(Messages::IdDiscord))
|
||||
.col(integer(Messages::IdSender))
|
||||
.col(integer(Messages::IdContent))
|
||||
.foreign_key(
|
||||
|
||||
@@ -12,7 +12,7 @@ impl MigrationTrait for Migration {
|
||||
.table(Channel::Table)
|
||||
.if_not_exists()
|
||||
.col(pk_auto(Channel::Id))
|
||||
.col(integer(Channel::IdDiscord))
|
||||
.col(big_integer(Channel::IdDiscord))
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user