can store and retrive data now lol ok xd
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use entities::{channel, members, messages, server};
|
||||
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, Related};
|
||||
use serenity::all::{
|
||||
Color, Command, CommandInteraction, Context, CreateCommand, CreateEmbed, CreateEmbedFooter,
|
||||
CreateInteractionResponse, CreateInteractionResponseMessage, Embed, EmbedMessageBuilding,
|
||||
InteractionContext, Message, MessageBuilder, MessageId, ResolvedOption,
|
||||
};
|
||||
|
||||
pub struct Retrive {}
|
||||
|
||||
impl Retrive {
|
||||
pub async fn run(
|
||||
db: &DatabaseConnection,
|
||||
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 user_db_id = members::Entity::find()
|
||||
.filter(members::Column::IdDiscord.eq(invoker_id))
|
||||
.one(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;
|
||||
}
|
||||
|
||||
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::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
|
||||
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() {
|
||||
let channel_db = channel::Entity::find_by_id(msg.id_channel)
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
map_channel.insert(msg.id_channel, channel_db.id_discord);
|
||||
}
|
||||
let channel_discord_id = map_channel.get(&msg.id_channel).unwrap();
|
||||
if let Some(count) = map.get(channel_discord_id) {
|
||||
let count = count + 1;
|
||||
map.insert(*channel_discord_id, count)
|
||||
} else {
|
||||
map.insert(*channel_discord_id, 1)
|
||||
};
|
||||
}
|
||||
|
||||
let mut summary_string: String = String::new();
|
||||
for x in map {
|
||||
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));
|
||||
|
||||
let reply = CreateInteractionResponseMessage::new().embed(embed);
|
||||
|
||||
CreateInteractionResponse::Message(reply)
|
||||
}
|
||||
pub fn register() -> CreateCommand {
|
||||
CreateCommand::new("rective")
|
||||
.name("userdata")
|
||||
.description("Gets all the data related to the user calling the command")
|
||||
.contexts(vec![InteractionContext::Guild])
|
||||
}
|
||||
}
|
||||
+49
-2
@@ -1,16 +1,19 @@
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
||||
use entities::{content, members, messages, server};
|
||||
use entities::{channel, content, members, messages, server};
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, Database, DatabaseConnection, EntityTrait, QueryFilter,
|
||||
};
|
||||
use serenity::all::{Command, Interaction, Ready};
|
||||
use serenity::async_trait;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::model::guild;
|
||||
use serenity::prelude::*;
|
||||
|
||||
mod command;
|
||||
|
||||
struct Bot {
|
||||
db: DatabaseConnection,
|
||||
}
|
||||
@@ -23,7 +26,7 @@ impl EventHandler for Bot {
|
||||
println!("Error sending message: {why:?}");
|
||||
}
|
||||
}
|
||||
// This is a mess, it should be split into its on function(s)
|
||||
// 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,
|
||||
@@ -75,14 +78,58 @@ impl EventHandler for Bot {
|
||||
}
|
||||
};
|
||||
|
||||
let channel_id = msg.channel(&ctx.http).await.unwrap().id().get() as i32;
|
||||
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),
|
||||
};
|
||||
activeModel.insert(&self.db).await.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
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_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
|
||||
}
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
if let Interaction::Command(command) = interaction {
|
||||
println!("Received command interaction: {command:#?}");
|
||||
|
||||
let content = match command.data.name.as_str() {
|
||||
"userdata" => Some(command::Retrive::run(&self.db, &ctx, &command).await),
|
||||
_ => Some(serenity::all::CreateInteractionResponse::Pong),
|
||||
};
|
||||
|
||||
if let Some(content) = content {
|
||||
if let Err(why) = command.create_response(&ctx.http, content).await {
|
||||
println!("Cannot respond to slash command: {why}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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:#?}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user