ability to send messages
This commit is contained in:
parent
d2a0603bbf
commit
6f5bfdfd82
35
src/bot.rs
35
src/bot.rs
|
@ -47,7 +47,6 @@ impl EventHandler for Handler {
|
|||
} else {
|
||||
"dm".to_string()
|
||||
};
|
||||
|
||||
let discord_message = discord_structure::Message::new(msg.id.to_string(), msg.channel_id.to_string(), guild_id, author_name, msg.content.clone(), msg.timestamp.to_rfc2822());
|
||||
sender.send(postman::Packet::Message(discord_message)).expect("failed to send packet");
|
||||
} else {
|
||||
|
@ -104,6 +103,8 @@ async fn check_packets(context: &Context) {
|
|||
println!("bot : failed to retrieve receiver");
|
||||
}
|
||||
|
||||
|
||||
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
||||
for packet in packets_received {
|
||||
match packet {
|
||||
postman::Packet::FetchChannels(guild_id_str) => {
|
||||
|
@ -115,6 +116,8 @@ async fn check_packets(context: &Context) {
|
|||
}
|
||||
Err(why) => {
|
||||
println!("bot : error getting channels : {}", why);
|
||||
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
|
||||
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,20 +125,44 @@ async fn check_packets(context: &Context) {
|
|||
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 {
|
||||
match get_messages(context, guild_id_str.clone(), channel_id_str.clone(), first_message_id_str).await {
|
||||
Ok(_) => {
|
||||
println!("bot : successfuly got messages");
|
||||
}
|
||||
Err(why) => {
|
||||
println!("bot : error getting messages : {}", why);
|
||||
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
|
||||
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
postman::Packet::SendMessage(channel_id_str, content) => {
|
||||
println!("bot : received SendMessage packet, channel : '{}', content : {}", channel_id_str, content);
|
||||
|
||||
match send_message(context, channel_id_str, content).await {
|
||||
Ok(_) => {
|
||||
println!("bot : successfuly sent message");
|
||||
}
|
||||
Err(why) => {
|
||||
println!("bot : error sending message : {}", why);
|
||||
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
|
||||
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
_ => {
|
||||
println!("bot : unhandled packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_message(context: &Context, channel_id_str: String, content: String) -> Result<(), String> {
|
||||
let channel_id_u64 = channel_id_str.parse::<u64>().map_err(|e| e.to_string())?;
|
||||
ChannelId(channel_id_u64).say(&context.http, content).await.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), String> {
|
||||
|
@ -149,9 +176,9 @@ async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), Str
|
|||
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::Channel(discord_channel)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
|
||||
sender.send(postman::Packet::FinishedRequest).map_err(|e| e.to_string())?;
|
||||
|
||||
} else {
|
||||
return Err("guild not found".to_string())
|
||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -146,11 +146,14 @@ impl eframe::App for Jiji {
|
|||
}
|
||||
|
||||
}
|
||||
postman::Packet::Error(reason) => {
|
||||
println!("gui : error received {}", reason);
|
||||
}
|
||||
postman::Packet::FinishedRequest => {
|
||||
self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0);
|
||||
}
|
||||
_ => {
|
||||
println!("unhandled packet");
|
||||
println!("gui : unhandled packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -252,11 +255,13 @@ impl Jiji {
|
|||
egui::TopBottomPanel::bottom("infobar")
|
||||
.resizable(false)
|
||||
.show(ctx, |ui| {
|
||||
if self.selected_channel != None {
|
||||
if let Some(guild_index) = self.selected_guild {
|
||||
if let Some(channel_index) = self.selected_channel {
|
||||
ui.label("");
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button(">").clicked() {
|
||||
println!("gui : sent message");
|
||||
let _ = self.sender.send(postman::Packet::SendMessage(self.guilds[guild_index].channels[channel_index].id.clone(), self.current_message.clone()));
|
||||
self.current_message = "".to_string();
|
||||
}
|
||||
egui::ScrollArea::vertical()
|
||||
.show(ui, |ui| {
|
||||
|
@ -266,6 +271,7 @@ impl Jiji {
|
|||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
ui.horizontal(|ui| {
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
|
||||
ui.label(&format!("time per frame : {:.1} ms", self.time_watch));
|
||||
|
|
|
@ -7,11 +7,12 @@ pub enum Packet {
|
|||
Guild(discord_structure::Guild),
|
||||
Channel(discord_structure::Channel),
|
||||
Message(discord_structure::Message),
|
||||
SendMessage(String, String),
|
||||
ChannelEnd(String, String),
|
||||
FetchChannels(String),
|
||||
FetchMessages(String, String, String),
|
||||
FinishedRequest,
|
||||
|
||||
Error(String),
|
||||
}
|
||||
|
||||
pub struct Sender;
|
||||
|
|
Loading…
Reference in a new issue