better bot functions

This commit is contained in:
WanderingPenwing 2024-07-11 16:19:44 +02:00
parent a66e462937
commit 84cd96b2f6

View file

@ -91,12 +91,42 @@ async fn check_packets(context: &Context) {
match packet {
postman::Packet::FetchChannels(guild_id_str) => {
println!("bot : received FetchChannels packet, for guild '{}'", guild_id_str);
match guild_id_str.parse::<u64>() {
Ok(guild_id_u64) => {
if let Some(guild) = context.cache.guild(guild_id_u64).await {
match guild.channels(&context.http).await {
Ok(guild_channels) => {
match get_channels(context, guild_id_str).await {
Ok(_) => {
println!("bot : successfuly got channels");
}
Err(why) => {
println!("bot : error getting channels : {}", why);
}
}
}
postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => {
println!("bot : received FetchMessages packet, channel : '{}', first message : {}", channel_id_str, first_message_id_str);
match get_messages(context, guild_id_str, channel_id_str, first_message_id_str).await {
Ok(_) => {
println!("bot : successfuly got messages");
}
Err(why) => {
println!("bot : error getting messages : {}", why);
}
}
}
_ => {
println!("bot : unhandled packet");
}
}
}
}
async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), String> {
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
let guild_id_u64 = guild_id_str.parse::<u64>().map_err(|e| e.to_string())?;
if let Some(guild) = context.cache.guild(guild_id_u64).await {
let guild_channels = guild.channels(&context.http).await.map_err(|e| e.to_string())?;
for (channel_id, channel) in guild_channels {
if channel.kind != ChannelType::Text {
continue
@ -104,37 +134,15 @@ async fn check_packets(context: &Context) {
let discord_channel = discord_structure::Channel::new(channel.name, format!("{}",channel_id), guild_id_str.to_string());
sender.send(postman::Packet::Channel(discord_channel)).expect("Failed to send packet");
}
println!("bot : sent channels");
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} else {
println!("bot : failed to retrieve sender");
}
}
Err(why) => {
eprintln!("bot : Failed to get channels : {}", why);
}
return Err("guild not found".to_string())
}
} else {
println!("bot : guild not found");
};
}
Err(why) => {
eprintln!("bot : Failed to parse guild ID string to u64: {}", why);
}
}
}
postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => {
println!("bot : received FetchMessages packet, channel : '{}', first message : {}", channel_id_str, first_message_id_str);
let _result = get_messages(context, guild_id_str, channel_id_str, first_message_id_str).await;
}
_ => {
println!("bot : unhandled packet");
}
}
return Err("failed to retriev sender".to_string())
}
Ok(())
}
async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: String, first_message_id_str: String) -> Result<(), String> {
@ -153,8 +161,6 @@ async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: S
}).await.map_err(|e| e.to_string())?
};
println!("bot : got messages");
for message in &messages {
let author_name = message.author.name.clone();
let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), author_name, message.content.clone(), message.timestamp.to_string());
@ -167,6 +173,8 @@ async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: S
}
sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?;
} else {
return Err("failed to retriev sender".to_string())
}
Ok(())
}