better bot functions

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

View file

@ -91,44 +91,28 @@ 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) => {
if let Some(guild) = context.cache.guild(guild_id_u64).await {
match guild.channels(&context.http).await {
Ok(guild_channels) => {
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
for (channel_id, channel) in guild_channels {
if channel.kind != ChannelType::Text {
continue
}
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");
}
} match get_channels(context, guild_id_str).await {
Err(why) => { Ok(_) => {
eprintln!("bot : Failed to get channels : {}", why); println!("bot : successfuly got channels");
}
}
} else {
println!("bot : guild not found");
};
} }
Err(why) => { Err(why) => {
eprintln!("bot : Failed to parse guild ID string to u64: {}", why); println!("bot : error getting channels : {}", why);
} }
} }
} }
postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => { 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); 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; 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"); println!("bot : unhandled packet");
@ -137,6 +121,30 @@ async fn check_packets(context: &Context) {
} }
} }
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
}
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::FinishedRequest).expect("Failed to send packet");
} else {
return Err("guild not found".to_string())
}
} else {
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> { async fn get_messages(context: &Context, guild_id_str: String, channel_id_str: String, first_message_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 channel_id_u64 = channel_id_str.parse::<u64>().map_err(|e| e.to_string())?; let channel_id_u64 = channel_id_str.parse::<u64>().map_err(|e| e.to_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(())
} }