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 { match packet {
postman::Packet::FetchChannels(guild_id_str) => { postman::Packet::FetchChannels(guild_id_str) => {
println!("bot : received FetchChannels packet, for guild '{}'", guild_id_str); println!("bot : received FetchChannels packet, for guild '{}'", guild_id_str);
match guild_id_str.parse::<u64>() {
Ok(guild_id_u64) => { match get_channels(context, guild_id_str).await {
if let Some(guild) = context.cache.guild(guild_id_u64).await { Ok(_) => {
match guild.channels(&context.http).await { println!("bot : successfuly got channels");
Ok(guild_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>() { 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 { for (channel_id, channel) in guild_channels {
if channel.kind != ChannelType::Text { if channel.kind != ChannelType::Text {
continue 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()); 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"); 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"); sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} else { } else {
println!("bot : failed to retrieve sender"); return Err("guild not found".to_string())
}
}
Err(why) => {
eprintln!("bot : Failed to get channels : {}", why);
}
} }
} else { } else {
println!("bot : guild not found"); return Err("failed to retriev sender".to_string())
};
}
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");
}
}
} }
Ok(())
} }
async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: String, first_message_id_str: String) -> Result<(), String> { 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())? }).await.map_err(|e| e.to_string())?
}; };
println!("bot : got messages");
for message in &messages { for message in &messages {
let author_name = message.author.name.clone(); 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()); 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())?; sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?;
} else {
return Err("failed to retriev sender".to_string())
} }
Ok(()) Ok(())
} }