added channel fetching
This commit is contained in:
parent
454047661c
commit
029d7a1d06
38
shell.nix
Normal file
38
shell.nix
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
{ pkgs ? import <nixpkgs> { overlays = [ (import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz)) ]; },
|
||||||
|
unstable ? import <nixos-unstable> { config = { allowUnfree = true; }; }
|
||||||
|
}:
|
||||||
|
with pkgs;
|
||||||
|
|
||||||
|
let
|
||||||
|
# Specify Pillow as a build input
|
||||||
|
pillow = python3Packages.pillow;
|
||||||
|
in
|
||||||
|
|
||||||
|
mkShell {
|
||||||
|
nativeBuildInputs = with xorg; [
|
||||||
|
libxcb
|
||||||
|
libXcursor
|
||||||
|
libXrandr
|
||||||
|
libXi
|
||||||
|
pkg-config
|
||||||
|
] ++ [
|
||||||
|
cargo
|
||||||
|
unstable.rustc
|
||||||
|
python3
|
||||||
|
libGL
|
||||||
|
libGLU
|
||||||
|
libxkbcommon
|
||||||
|
gtk3-x11
|
||||||
|
gnome.zenity
|
||||||
|
];
|
||||||
|
buildInputs = [
|
||||||
|
latest.rustChannels.stable.rust
|
||||||
|
xorg.libX11
|
||||||
|
wayland
|
||||||
|
libxkbcommon
|
||||||
|
];
|
||||||
|
|
||||||
|
shellHook = ''
|
||||||
|
export LD_LIBRARY_PATH=/run/opengl-driver/lib/:${lib.makeLibraryPath ([libGL libGLU libxkbcommon])}
|
||||||
|
'';
|
||||||
|
}
|
95
src/bot.rs
95
src/bot.rs
|
@ -3,7 +3,7 @@ use serenity::{
|
||||||
model::{channel::Message, gateway::Ready},
|
model::{channel::Message, gateway::Ready},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use serenity::model::prelude::GuildId;
|
use serenity::model::prelude::ChannelType;
|
||||||
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};
|
||||||
|
@ -17,7 +17,7 @@ use crate::discord_structure;
|
||||||
mod token;
|
mod token;
|
||||||
|
|
||||||
const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing.";
|
const HELP_MESSAGE: &str = "Hello there, Human! I am a messenger for the wandering penwing.";
|
||||||
const HELP_COMMAND: &str = "!jiji";
|
const HELP_COMMAND: &str = "!penwing";
|
||||||
const PACKET_REFRESH : u64 = 500;
|
const PACKET_REFRESH : u64 = 500;
|
||||||
|
|
||||||
struct Handler {
|
struct Handler {
|
||||||
|
@ -38,50 +38,45 @@ impl EventHandler for Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ready(&self, context: Context, ready: Ready) {
|
async fn ready(&self, _context: Context, ready: Ready) {
|
||||||
println!("bot : {} is connected!", ready.user.name);
|
println!("bot : {} is connected!", ready.user.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn cache_ready(&self, context: Context, _guilds: Vec<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;
|
||||||
});
|
});
|
||||||
|
|
||||||
let context2 = Arc::clone(&context);
|
let context2 = Arc::clone(&context);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
check_packets(&context2).await;
|
check_packets(&context2).await;
|
||||||
thread::sleep(Duration::from_millis(PACKET_REFRESH));
|
thread::sleep(Duration::from_millis(PACKET_REFRESH));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_packets(context: &Context) {
|
async fn check_packets(context: &Context) {
|
||||||
|
let mut packets_received : Vec<postman::Packet> = vec![];
|
||||||
|
|
||||||
if let Some(receiver_mutex) = context.data.read().await.get::<postman::Receiver>() {
|
if let Some(receiver_mutex) = context.data.read().await.get::<postman::Receiver>() {
|
||||||
if let Ok(receiver) = receiver_mutex.lock() {
|
if let Ok(receiver) = receiver_mutex.lock() {
|
||||||
while let Ok(packet) = receiver.try_recv() {
|
while let Ok(packet) = receiver.try_recv() {
|
||||||
match packet {
|
packets_received.push(packet);
|
||||||
postman::Packet::FetchChannels(guild_id) => {
|
|
||||||
println!("bot : fetch channels request received : '{}'", guild_id);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
println!("bot : unhandled packet");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("bot : failed to lock receiver");
|
println!("bot : failed to lock receiver");
|
||||||
|
@ -89,8 +84,52 @@ 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 {
|
||||||
|
postman::Packet::FetchChannels(guild_id_str) => {
|
||||||
|
println!("bot : received FetchChannels packet, id '{}'", 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");
|
||||||
|
} else {
|
||||||
|
println!("bot : failed to retrieve sender");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("bot : Failed to get channels : {}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("bot : guild not found");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("bot : Failed to parse guild ID string to u64: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("bot : unhandled packet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn get_guilds(context: &Context) {
|
async fn get_guilds(context: &Context) {
|
||||||
let guilds = context.cache.guilds().await;
|
let guilds = context.cache.guilds().await;
|
||||||
|
|
||||||
|
|
55
src/main.rs
55
src/main.rs
|
@ -43,7 +43,8 @@ struct Jiji {
|
||||||
sender: mpsc::Sender<postman::Packet>,
|
sender: mpsc::Sender<postman::Packet>,
|
||||||
receiver: mpsc::Receiver<postman::Packet>,
|
receiver: mpsc::Receiver<postman::Packet>,
|
||||||
guilds: Vec<discord_structure::Guild>,
|
guilds: Vec<discord_structure::Guild>,
|
||||||
selected_guild: Option<discord_structure::Guild>,
|
selected_guild: Option<usize>,
|
||||||
|
selected_channel: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Jiji {
|
impl Jiji {
|
||||||
|
@ -54,6 +55,7 @@ impl Jiji {
|
||||||
receiver,
|
receiver,
|
||||||
guilds: vec![],
|
guilds: vec![],
|
||||||
selected_guild: None,
|
selected_guild: None,
|
||||||
|
selected_channel: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,6 +75,13 @@ impl eframe::App for Jiji {
|
||||||
}
|
}
|
||||||
postman::Packet::Channel(channel) => {
|
postman::Packet::Channel(channel) => {
|
||||||
println!("gui : channel received : '{}'", channel.name);
|
println!("gui : channel received : '{}'", channel.name);
|
||||||
|
for i in 0..self.guilds.len() {
|
||||||
|
if self.guilds[i].id != channel.guild_id {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
self.guilds[i].channels.push(channel.clone());
|
||||||
|
println!("gui : channel added to '{}'", self.guilds[i].name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
postman::Packet::Message(message) => {
|
postman::Packet::Message(message) => {
|
||||||
println!("gui : message received : '{}'", message.content);
|
println!("gui : message received : '{}'", message.content);
|
||||||
|
@ -98,32 +107,56 @@ impl Jiji {
|
||||||
.show(ctx, |ui| {
|
.show(ctx, |ui| {
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.label("Where do you want to look ? ");
|
ui.label("Where do you want to look ? ");
|
||||||
let selected_text = if let Some(guild) = &self.selected_guild {
|
let selected_guild_text = if let Some(selected_guild_index) = &self.selected_guild {
|
||||||
guild.name.clone()
|
self.guilds[*selected_guild_index].name.clone()
|
||||||
} else {
|
} else {
|
||||||
"None".to_string()
|
"None".to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
egui::ComboBox::from_label("")
|
egui::ComboBox::from_label("")
|
||||||
.selected_text(format!("{}", selected_text))
|
.selected_text(format!("{}", selected_guild_text))
|
||||||
.show_ui(ui, |ui| {
|
.show_ui(ui, |ui| {
|
||||||
ui.style_mut().wrap = Some(false);
|
ui.style_mut().wrap = Some(false);
|
||||||
ui.set_min_width(60.0);
|
ui.set_min_width(60.0);
|
||||||
if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() {
|
if ui.add(egui::SelectableLabel::new(self.selected_guild == None, "None")).clicked() {
|
||||||
self.selected_guild = None;
|
self.selected_guild = None;
|
||||||
}
|
}
|
||||||
for guild in self.guilds.clone() {
|
for i in 0..self.guilds.len() {
|
||||||
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(guild.clone()), guild.name.clone())).clicked() {
|
if ui.add(egui::SelectableLabel::new(self.selected_guild == Some(i), self.guilds[i].name.clone())).clicked() {
|
||||||
self.selected_guild = Some(guild);
|
self.selected_guild = Some(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(guild) = &self.selected_guild {
|
|
||||||
if guild.channels.len() == 0 {
|
if let Some(selected_guild_index) = &self.selected_guild {
|
||||||
|
if self.guilds[*selected_guild_index].channels.len() == 0 {
|
||||||
if ui.add(egui::Button::new("get channels")).clicked() {
|
if ui.add(egui::Button::new("get channels")).clicked() {
|
||||||
let _ = self.sender.send(postman::Packet::FetchChannels(guild.id.clone()));
|
let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[*selected_guild_index].id.clone()));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
let selected_channel_text = if let Some(selected_channel_index) = &self.selected_channel {
|
||||||
|
self.guilds[*selected_guild_index].channels[*selected_channel_index].name.clone()
|
||||||
|
} else {
|
||||||
|
"None".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
egui::ComboBox::from_label(":")
|
||||||
|
.selected_text(format!("{}", selected_channel_text))
|
||||||
|
.show_ui(ui, |ui| {
|
||||||
|
ui.style_mut().wrap = Some(false);
|
||||||
|
ui.set_min_width(60.0);
|
||||||
|
if ui.add(egui::SelectableLabel::new(self.selected_channel == None, "None")).clicked() {
|
||||||
|
self.selected_channel = None;
|
||||||
|
}
|
||||||
|
for i in 0..self.guilds[*selected_guild_index].channels.len() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue