ability to receive msg
This commit is contained in:
parent
19fc2626f7
commit
d2a0603bbf
66
src/bot.rs
66
src/bot.rs
|
@ -10,8 +10,9 @@ use std::sync::mpsc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::thread;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use tokio::time::interval;
|
||||||
|
|
||||||
|
|
||||||
use crate::postman;
|
use crate::postman;
|
||||||
use crate::discord_structure;
|
use crate::discord_structure;
|
||||||
|
@ -41,13 +42,11 @@ impl EventHandler for Handler {
|
||||||
|
|
||||||
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
||||||
let author_name = msg.author.name.clone();
|
let author_name = msg.author.name.clone();
|
||||||
let mut guild_id = "dm".to_string();
|
let guild_id = if let Some(id) = msg.guild_id {
|
||||||
|
id.to_string()
|
||||||
if let Some(channel) = msg.channel(&context.cache).await {
|
} else {
|
||||||
if let Some(guild) = channel.guild() {
|
"dm".to_string()
|
||||||
guild_id = guild.id.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");
|
||||||
|
@ -61,31 +60,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;
|
||||||
});
|
});
|
||||||
|
|
||||||
//comment this to get the listening to messages working
|
// Use tokio interval instead of spawning a loop
|
||||||
let context2 = Arc::clone(&context);
|
let context2 = Arc::clone(&context);
|
||||||
tokio::spawn(async move {
|
let mut interval = interval(Duration::from_millis(PACKET_REFRESH));
|
||||||
loop {
|
tokio::spawn(async move {
|
||||||
check_packets(&context2).await;
|
loop {
|
||||||
thread::sleep(Duration::from_millis(PACKET_REFRESH));
|
interval.tick().await;
|
||||||
}
|
check_packets(&context2).await;
|
||||||
});
|
}
|
||||||
|
});
|
||||||
// Now that the loop is running, we set the bool to true
|
|
||||||
self.is_loop_running.swap(true, Ordering::Relaxed);
|
// Now that the loop is running, we set the bool to true
|
||||||
}
|
self.is_loop_running.swap(true, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -102,6 +102,7 @@ impl eframe::App for Jiji {
|
||||||
guild = Some(i);
|
guild = Some(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if let Some(guild_index) = guild {
|
if let Some(guild_index) = guild {
|
||||||
let mut unkown_channel = true;
|
let mut unkown_channel = true;
|
||||||
for i in 0..self.guilds[guild_index].channels.len() {
|
for i in 0..self.guilds[guild_index].channels.len() {
|
||||||
|
@ -110,12 +111,17 @@ impl eframe::App for Jiji {
|
||||||
}
|
}
|
||||||
self.guilds[guild_index].channels[i].insert(message.clone());
|
self.guilds[guild_index].channels[i].insert(message.clone());
|
||||||
unkown_channel = false;
|
unkown_channel = false;
|
||||||
|
println!("gui : message put in : '{}'", self.guilds[guild_index].channels[i].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if unkown_channel {
|
if unkown_channel {
|
||||||
println!("gui : unkown channel");
|
println!("gui : unkown channel");
|
||||||
self.guilds[guild_index].channels.push(discord_structure::Channel::new(message.channel_id.clone(), message.channel_id, message.guild_id));
|
self.guilds[guild_index].channels.push(discord_structure::Channel::new(message.channel_id.clone(), message.channel_id, message.guild_id));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
println!("gui : message guild issue : '{}'", message.guild_id);
|
||||||
|
|
||||||
|
println!("gui : guilds {:?}", self.guilds.clone().into_iter().map(|guild| { guild.id.clone()}).collect::<Vec<String>>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
postman::Packet::ChannelEnd(guild_id, channel_id) => {
|
postman::Packet::ChannelEnd(guild_id, channel_id) => {
|
||||||
|
@ -193,12 +199,14 @@ impl Jiji {
|
||||||
for i in 0..self.guilds.len() {
|
for i in 0..self.guilds.len() {
|
||||||
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() {
|
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() {
|
||||||
self.selected_guild = Some(i);
|
self.selected_guild = Some(i);
|
||||||
|
self.selected_channel = None;
|
||||||
|
|
||||||
if self.guilds[i].channels.len() == 0 && self.guilds[i].id != "dm" {
|
if self.guilds[i].channels.len() == 0 && self.guilds[i].id != "dm" {
|
||||||
let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone()));
|
let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone()));
|
||||||
|
|
||||||
self.selected_channel = None;
|
|
||||||
self.pending_bot_requests += 1;
|
self.pending_bot_requests += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue