Add discord online-status
This commit is contained in:
parent
0c9e763e8c
commit
21c4b82658
75
src/main.rs
75
src/main.rs
@ -30,41 +30,56 @@ const CONFIG_PATH: &'static str = "./config.json";
|
|||||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
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 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
|
.await
|
||||||
.expect("Err creating client");
|
.expect("Err creating client");
|
||||||
|
|
||||||
let addr = SocketAddr::from((config.host, config.port));
|
let addr = SocketAddr::from((config.host, config.port));
|
||||||
let listener = TcpListener::bind(addr).await?;
|
let listener = TcpListener::bind(addr).await?;
|
||||||
|
|
||||||
loop {
|
let cache = client.cache.clone();
|
||||||
let (stream, _) = listener.accept().await?;
|
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 loop_handle = tokio::task::spawn(async move {
|
||||||
let http = client.http.clone();
|
loop {
|
||||||
let config = config.clone();
|
let (stream, _) = listener.accept().await.unwrap();
|
||||||
|
|
||||||
// Spawn a tokio task to serve multiple connections concurrently
|
let io = TokioIo::new(stream);
|
||||||
tokio::task::spawn(async move {
|
|
||||||
if let Err(err) = http1::Builder::new()
|
let cache = cache.clone();
|
||||||
// `service_fn` converts our function in a `Service`
|
let http = http.clone();
|
||||||
.serve_connection(
|
let config = config.clone();
|
||||||
io,
|
|
||||||
service_fn(|req: Request<body::Incoming>| {
|
// Spawn a tokio task to serve multiple connections concurrently
|
||||||
let cache = cache.clone();
|
tokio::task::spawn(async move {
|
||||||
let http = http.clone();
|
if let Err(err) = http1::Builder::new()
|
||||||
let config = config.clone();
|
// `service_fn` converts our function in a `Service`
|
||||||
async move { hello(req, (&cache, http.as_ref()), &config).await }
|
.serve_connection(
|
||||||
}),
|
io,
|
||||||
)
|
service_fn(|req: Request<body::Incoming>| {
|
||||||
.await
|
let cache = cache.clone();
|
||||||
{
|
let http = http.clone();
|
||||||
println!("Error serving connection: {:?}", err);
|
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(
|
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 {
|
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 author = CreateEmbedAuthor::new(&feed.title).url(&feed.site_url);
|
||||||
let footer = CreateEmbedFooter::new(format!("{} minutes", entry.reading_time.to_string()));
|
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 external_button = CreateButton::new_link(&entry.url).label("external").emoji('📤');
|
||||||
let minreq_button = CreateButton::new_link(minreq_url).label("minreq").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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user