ability to send messages
This commit is contained in:
parent
d2a0603bbf
commit
6f5bfdfd82
127
src/bot.rs
127
src/bot.rs
|
@ -47,7 +47,6 @@ impl EventHandler for Handler {
|
||||||
} else {
|
} else {
|
||||||
"dm".to_string()
|
"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());
|
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");
|
sender.send(postman::Packet::Message(discord_message)).expect("failed to send packet");
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,32 +59,32 @@ impl EventHandler for Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn cache_ready(&self, context: Context, _guilds: Vec<serenity::model::prelude::GuildId>) {
|
async fn cache_ready(&self, context: Context, _guilds: Vec<serenity::model::prelude::GuildId>) {
|
||||||
println!("bot : cache built successfully!");
|
println!("bot : cache built successfully!");
|
||||||
|
|
||||||
let context = Arc::new(context);
|
let context = Arc::new(context);
|
||||||
|
|
||||||
if !self.is_loop_running.load(Ordering::Relaxed) {
|
if !self.is_loop_running.load(Ordering::Relaxed) {
|
||||||
// We have to clone the Arc, as it gets moved into the new thread.
|
// We have to clone the Arc, as it gets moved into the new thread.
|
||||||
let context1 = Arc::clone(&context);
|
let context1 = Arc::clone(&context);
|
||||||
// tokio::spawn creates a new green thread that can run in parallel with the rest of
|
// tokio::spawn creates a new green thread that can run in parallel with the rest of
|
||||||
// the application.
|
// the application.
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
get_guilds(&context1).await;
|
get_guilds(&context1).await;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Use tokio interval instead of spawning a loop
|
// Use tokio interval instead of spawning a loop
|
||||||
let context2 = Arc::clone(&context);
|
let context2 = Arc::clone(&context);
|
||||||
let mut interval = interval(Duration::from_millis(PACKET_REFRESH));
|
let mut interval = interval(Duration::from_millis(PACKET_REFRESH));
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
interval.tick().await;
|
interval.tick().await;
|
||||||
check_packets(&context2).await;
|
check_packets(&context2).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Now that the loop is running, we set the bool to true
|
// Now that the loop is running, we set the bool to true
|
||||||
self.is_loop_running.swap(true, Ordering::Relaxed);
|
self.is_loop_running.swap(true, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,41 +102,69 @@ async fn check_packets(context: &Context) {
|
||||||
} else {
|
} else {
|
||||||
println!("bot : failed to retrieve receiver");
|
println!("bot : failed to retrieve receiver");
|
||||||
}
|
}
|
||||||
|
|
||||||
for packet in packets_received {
|
|
||||||
match packet {
|
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
||||||
postman::Packet::FetchChannels(guild_id_str) => {
|
for packet in packets_received {
|
||||||
println!("bot : received FetchChannels packet, for guild '{}'", guild_id_str);
|
match packet {
|
||||||
|
postman::Packet::FetchChannels(guild_id_str) => {
|
||||||
match get_channels(context, guild_id_str).await {
|
println!("bot : received FetchChannels packet, for guild '{}'", guild_id_str);
|
||||||
Ok(_) => {
|
|
||||||
println!("bot : successfuly got channels");
|
match get_channels(context, guild_id_str).await {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("bot : successfuly got channels");
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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.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);
|
||||||
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 send_message(context, channel_id_str, content).await {
|
||||||
|
Ok(_) => {
|
||||||
match get_messages(context, guild_id_str, channel_id_str, first_message_id_str).await {
|
println!("bot : successfuly sent message");
|
||||||
Ok(_) => {
|
}
|
||||||
println!("bot : successfuly got messages");
|
Err(why) => {
|
||||||
}
|
println!("bot : error sending message : {}", why);
|
||||||
Err(why) => {
|
sender.send(postman::Packet::Error(why)).expect("Failed to send packet");
|
||||||
println!("bot : error getting messages : {}", why);
|
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("bot : unhandled 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> {
|
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())?;
|
let guild_id_u64 = guild_id_str.parse::<u64>().map_err(|e| e.to_string())?;
|
||||||
|
@ -149,9 +176,9 @@ async fn get_channels(context: &Context, guild_id_str: String) -> Result<(), Str
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
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)).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 {
|
} else {
|
||||||
return Err("guild not found".to_string())
|
return Err("guild not found".to_string())
|
||||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -146,11 +146,14 @@ impl eframe::App for Jiji {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
postman::Packet::Error(reason) => {
|
||||||
|
println!("gui : error received {}", reason);
|
||||||
|
}
|
||||||
postman::Packet::FinishedRequest => {
|
postman::Packet::FinishedRequest => {
|
||||||
self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0);
|
self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("unhandled packet");
|
println!("gui : unhandled packet");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -252,19 +255,22 @@ impl Jiji {
|
||||||
egui::TopBottomPanel::bottom("infobar")
|
egui::TopBottomPanel::bottom("infobar")
|
||||||
.resizable(false)
|
.resizable(false)
|
||||||
.show(ctx, |ui| {
|
.show(ctx, |ui| {
|
||||||
if self.selected_channel != None {
|
if let Some(guild_index) = self.selected_guild {
|
||||||
ui.label("");
|
if let Some(channel_index) = self.selected_channel {
|
||||||
ui.horizontal(|ui| {
|
ui.label("");
|
||||||
if ui.button(">").clicked() {
|
ui.horizontal(|ui| {
|
||||||
println!("gui : sent message");
|
if ui.button(">").clicked() {
|
||||||
}
|
let _ = self.sender.send(postman::Packet::SendMessage(self.guilds[guild_index].channels[channel_index].id.clone(), self.current_message.clone()));
|
||||||
egui::ScrollArea::vertical()
|
self.current_message = "".to_string();
|
||||||
.show(ui, |ui| {
|
}
|
||||||
let _response = ui.add(egui::TextEdit::multiline(&mut self.current_message)
|
egui::ScrollArea::vertical()
|
||||||
.desired_width(f32::INFINITY)
|
.show(ui, |ui| {
|
||||||
.lock_focus(true));
|
let _response = ui.add(egui::TextEdit::multiline(&mut self.current_message)
|
||||||
});
|
.desired_width(f32::INFINITY)
|
||||||
});
|
.lock_focus(true));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
|
||||||
|
|
|
@ -7,11 +7,12 @@ pub enum Packet {
|
||||||
Guild(discord_structure::Guild),
|
Guild(discord_structure::Guild),
|
||||||
Channel(discord_structure::Channel),
|
Channel(discord_structure::Channel),
|
||||||
Message(discord_structure::Message),
|
Message(discord_structure::Message),
|
||||||
|
SendMessage(String, String),
|
||||||
ChannelEnd(String, String),
|
ChannelEnd(String, String),
|
||||||
FetchChannels(String),
|
FetchChannels(String),
|
||||||
FetchMessages(String, String, String),
|
FetchMessages(String, String, String),
|
||||||
FinishedRequest,
|
FinishedRequest,
|
||||||
|
Error(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Sender;
|
pub struct Sender;
|
||||||
|
|
Loading…
Reference in a new issue