Add discord online-status

This commit is contained in:
Sofia 2024-03-16 03:21:21 +02:00
parent 0c9e763e8c
commit 21c4b82658

View File

@ -30,41 +30,56 @@ const CONFIG_PATH: &'static str = "./config.json";
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let config: Config = serde_json::from_slice(&fs::read(CONFIG_PATH).unwrap()).unwrap();
let client = Client::builder(&config.discord_token, GatewayIntents::empty())
let mut client = Client::builder(&config.discord_token, GatewayIntents::empty())
.await
.expect("Err creating client");
let addr = SocketAddr::from((config.host, config.port));
let listener = TcpListener::bind(addr).await?;
loop {
let (stream, _) = listener.accept().await?;
let cache = client.cache.clone();
let http = client.http.clone();
let io = TokioIo::new(stream);
let client_handle = tokio::task::spawn(async move {
client.start().await.unwrap();
});
let cache = client.cache.clone();
let http = client.http.clone();
let config = config.clone();
let loop_handle = tokio::task::spawn(async move {
loop {
let (stream, _) = listener.accept().await.unwrap();
// Spawn a tokio task to serve multiple connections concurrently
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
// `service_fn` converts our function in a `Service`
.serve_connection(
io,
service_fn(|req: Request<body::Incoming>| {
let cache = cache.clone();
let http = http.clone();
let config = config.clone();
async move { hello(req, (&cache, http.as_ref()), &config).await }
}),
)
.await
{
println!("Error serving connection: {:?}", err);
}
});
}
let io = TokioIo::new(stream);
let cache = cache.clone();
let http = http.clone();
let config = config.clone();
// Spawn a tokio task to serve multiple connections concurrently
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
// `service_fn` converts our function in a `Service`
.serve_connection(
io,
service_fn(|req: Request<body::Incoming>| {
let cache = cache.clone();
let http = http.clone();
let config = config.clone();
async move { hello(req, (&cache, http.as_ref()), &config).await }
}),
)
.await
{
println!("Error serving connection: {:?}", err);
}
});
}
});
let (res1, res2) = tokio::join!(client_handle, loop_handle);
res1.unwrap();
res2.unwrap();
Ok(())
}
async fn hello(
@ -136,12 +151,6 @@ async fn send(user: User, ctx: impl CacheHttp + Copy, event: MinifluxEvent, conf
}
fn message_from_entry(entry: &Entry, feed: &Feed, config: &Config) -> CreateMessage {
let content = MessageBuilder::new()
.push("New article from feed ")
.push_named_link(&feed.title, &feed.site_url)
.push(" published!")
.build();
let author = CreateEmbedAuthor::new(&feed.title).url(&feed.site_url);
let footer = CreateEmbedFooter::new(format!("{} minutes", entry.reading_time.to_string()));
@ -171,5 +180,5 @@ fn message_from_entry(entry: &Entry, feed: &Feed, config: &Config) -> CreateMess
let external_button = CreateButton::new_link(&entry.url).label("external").emoji('📤');
let minreq_button = CreateButton::new_link(minreq_url).label("minreq").emoji('📩');
CreateMessage::new().content(content).embed(embed).button(external_button).button(minreq_button)
CreateMessage::new().embed(embed).button(external_button).button(minreq_button)
}