fetch messages
This commit is contained in:
parent
2f2a9e40ae
commit
391c284b59
|
@ -9,5 +9,5 @@ edition = "2021"
|
||||||
eframe = "0.26.2"
|
eframe = "0.26.2"
|
||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
image = "0.24.9"
|
image = "0.24.9"
|
||||||
serenity = { default-features = false, features = ["client", "gateway", "model", "rustls_backend", "cache", "http"], version = "0.9.0-rc.2"}
|
serenity = { version = "0.9.0-rc.2", default-features = false, features = ["client", "gateway", "model", "builder", "rustls_backend", "cache", "http"] }
|
||||||
tokio = {features = ["macros"], version = "0.2"}
|
tokio = {features = ["macros"], version = "0.2"}
|
||||||
|
|
45
src/bot.rs
45
src/bot.rs
|
@ -4,6 +4,8 @@ use serenity::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use serenity::model::prelude::ChannelType;
|
use serenity::model::prelude::ChannelType;
|
||||||
|
use serenity::model::id::ChannelId;
|
||||||
|
use serenity::model::id::MessageId;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
@ -31,7 +33,7 @@ impl EventHandler for Handler {
|
||||||
println!("bot : message received : '{}' from {}", msg.content, msg.author);
|
println!("bot : message received : '{}' from {}", msg.content, msg.author);
|
||||||
if msg.content == HELP_COMMAND {
|
if msg.content == HELP_COMMAND {
|
||||||
if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await {
|
if let Err(why) = msg.channel_id.say(&ctx.http, HELP_MESSAGE).await {
|
||||||
println!("bot : Error sending message: {:?}", why);
|
eprintln!("bot : Error sending message: {:?}", why);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
println!("bot : successfuly sent reply");
|
println!("bot : successfuly sent reply");
|
||||||
|
@ -109,8 +111,8 @@ async fn check_packets(context: &Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(why) => {
|
||||||
eprintln!("bot : Failed to get channels : {}", e);
|
eprintln!("bot : Failed to get channels : {}", why);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -118,14 +120,43 @@ async fn check_packets(context: &Context) {
|
||||||
println!("bot : guild not found");
|
println!("bot : guild not found");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(why) => {
|
||||||
eprintln!("bot : Failed to parse guild ID string to u64: {}", e);
|
eprintln!("bot : Failed to parse guild ID string to u64: {}", why);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
postman::Packet::FetchMessages(guild_id_str, channel_id_str) => {
|
postman::Packet::FetchMessages(guild_id_str, channel_id_str, first_message_id_str) => {
|
||||||
println!("bot : received FetchMessages packet, for guild '{}', channel : '{}'", guild_id_str, channel_id_str);
|
println!("bot : received FetchMessages packet, channel : '{}', first message : {}", channel_id_str, first_message_id_str);
|
||||||
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
if let Some(sender) = context.data.read().await.get::<postman::Sender>() {
|
||||||
|
match channel_id_str.parse::<u64>() {
|
||||||
|
Ok(channel_id_u64) => {
|
||||||
|
let channel_id = ChannelId::from(channel_id_u64);
|
||||||
|
|
||||||
|
let maybe_messages = channel_id.messages(&context.http, |retriever| {
|
||||||
|
retriever.after(MessageId::from(158339864557912064)).limit(25)
|
||||||
|
}).await;
|
||||||
|
|
||||||
|
match maybe_messages {
|
||||||
|
Ok(messages) => {
|
||||||
|
println!("bot : got messages");
|
||||||
|
|
||||||
|
for message in messages {
|
||||||
|
let discord_message = discord_structure::Message::new(message.id.to_string(), channel_id_str.clone(), guild_id_str.clone(), message.author.to_string(), message.content, message.timestamp.to_string());
|
||||||
|
sender.send(postman::Packet::Message(discord_message)).expect("Failed to send packet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(why) => {
|
||||||
|
eprintln!("bot : Failed to fetch messages : {}", why);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(why) => {
|
||||||
|
eprintln!("bot : Failed to parse channel ID string to u64: {}", why);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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");
|
println!("bot : failed to retrieve sender");
|
||||||
|
|
|
@ -34,27 +34,29 @@ impl Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn greetings(&mut self) {
|
pub fn greetings(&mut self) {
|
||||||
self.messages.push(Message::new("-".into(), "0".into(), self.id.clone(), self.guild_id.clone(), "start of the conversation".into()));
|
self.messages.push(Message::new("0".into(), self.id.clone(), self.guild_id.clone(), "-".into(), "start of the conversation".into(), "".into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Clone)]
|
#[derive(PartialEq, Clone)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub author_name: String,
|
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub channel_id: String,
|
pub channel_id: String,
|
||||||
pub guild_id: String,
|
pub guild_id: String,
|
||||||
|
pub author_name: String,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
|
pub timestamp: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub fn new(author_name: String, id: String, channel_id: String, guild_id: String, content: String) -> Self {
|
pub fn new(id: String, channel_id: String, guild_id: String, author_name: String, content: String, timestamp: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
author_name,
|
|
||||||
id,
|
id,
|
||||||
channel_id,
|
channel_id,
|
||||||
guild_id,
|
guild_id,
|
||||||
|
author_name,
|
||||||
content,
|
content,
|
||||||
|
timestamp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
37
src/main.rs
37
src/main.rs
|
@ -90,6 +90,24 @@ impl eframe::App for Jiji {
|
||||||
}
|
}
|
||||||
postman::Packet::Message(message) => {
|
postman::Packet::Message(message) => {
|
||||||
println!("gui : message received : '{}'", message.content);
|
println!("gui : message received : '{}'", message.content);
|
||||||
|
|
||||||
|
let mut guild: Option<usize> = None;
|
||||||
|
|
||||||
|
for i in 0..self.guilds.len() {
|
||||||
|
if self.guilds[i].id != message.guild_id {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
guild = Some(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(guild_index) = guild {
|
||||||
|
for i in 0..self.guilds[guild_index].channels.len() {
|
||||||
|
if self.guilds[guild_index].channels[i].id != message.channel_id {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
self.guilds[guild_index].channels[i].messages.insert(0, message.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
@ -174,7 +192,7 @@ impl Jiji {
|
||||||
if ui.add(egui::SelectableLabel::new(self.selected_channel == Some(i), self.guilds[*selected_guild_index].channels[i].name.clone())).clicked() {
|
if ui.add(egui::SelectableLabel::new(self.selected_channel == Some(i), self.guilds[*selected_guild_index].channels[i].name.clone())).clicked() {
|
||||||
self.selected_channel = Some(i);
|
self.selected_channel = Some(i);
|
||||||
if self.guilds[*selected_guild_index].channels[i].messages.len() == 0 {
|
if self.guilds[*selected_guild_index].channels[i].messages.len() == 0 {
|
||||||
let _ = self.sender.send(postman::Packet::FetchMessages(self.guilds[*selected_guild_index].id.clone(), self.guilds[*selected_guild_index].channels[i].id.clone()));
|
let _ = self.sender.send(postman::Packet::FetchMessages(self.guilds[*selected_guild_index].id.clone(), self.guilds[*selected_guild_index].channels[i].id.clone(), "".into()));
|
||||||
|
|
||||||
self.pending_bot_requests += 1;
|
self.pending_bot_requests += 1;
|
||||||
|
|
||||||
|
@ -211,7 +229,22 @@ impl Jiji {
|
||||||
|
|
||||||
pub fn draw_feed(&mut self, ctx: &egui::Context) {
|
pub fn draw_feed(&mut self, ctx: &egui::Context) {
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
ui.label("");
|
egui::ScrollArea::vertical()
|
||||||
|
.stick_to_bottom(true)
|
||||||
|
.show(ui, |ui| {
|
||||||
|
if let Some(selected_guild_index) = &self.selected_guild {
|
||||||
|
if let Some(selected_channel_index) = &self.selected_channel {
|
||||||
|
for message in &self.guilds[*selected_guild_index].channels[*selected_channel_index].messages {
|
||||||
|
if message.author_name == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ui.separator();
|
||||||
|
ui.label(&message.content);
|
||||||
|
ui.label(&message.author_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub enum Packet {
|
||||||
Channel(discord_structure::Channel),
|
Channel(discord_structure::Channel),
|
||||||
Message(discord_structure::Message),
|
Message(discord_structure::Message),
|
||||||
FetchChannels(String),
|
FetchChannels(String),
|
||||||
FetchMessages(String, String),
|
FetchMessages(String, String, String),
|
||||||
FinishedRequest,
|
FinishedRequest,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue