looooopy
This commit is contained in:
parent
1013b7153f
commit
ecd1fdf001
1 changed files with 28 additions and 29 deletions
57
src/main.rs
57
src/main.rs
|
@ -62,14 +62,14 @@ impl EventHandler for Handler {
|
||||||
|
|
||||||
async fn handle_recv(
|
async fn handle_recv(
|
||||||
mut recv: SplitStream<WebSocketStream<TcpStream>>,
|
mut recv: SplitStream<WebSocketStream<TcpStream>>,
|
||||||
http: Http,
|
http: &Http,
|
||||||
webhook: Webhook,
|
webhook: Webhook,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
while let Some(msg) = recv.next().await {
|
while let Some(msg) = recv.next().await {
|
||||||
if let WsMessage::Text(content) = msg? {
|
if let WsMessage::Text(content) = msg? {
|
||||||
let message: ChatMessage = serde_json::from_str(&content)?;
|
let message: ChatMessage = serde_json::from_str(&content)?;
|
||||||
webhook
|
webhook
|
||||||
.execute(&http, false, |w| {
|
.execute(http, false, |w| {
|
||||||
w.content(message.message)
|
w.content(message.message)
|
||||||
.username(message.user)
|
.username(message.user)
|
||||||
.avatar_url(format!(
|
.avatar_url(format!(
|
||||||
|
@ -92,36 +92,35 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let channel_id = env::var("CHANNEL_ID").expect("channel id").parse()?;
|
let channel_id = env::var("CHANNEL_ID").expect("channel id").parse()?;
|
||||||
|
|
||||||
let listener = TcpListener::bind(env::var("BIND_ADDR").expect("bind address")).await?;
|
let listener = TcpListener::bind(env::var("BIND_ADDR").expect("bind address")).await?;
|
||||||
let (stream, _) = listener.accept().await?;
|
let token = env::var("DISCORD_TOKEN").expect("token");
|
||||||
let (send, recv) =
|
|
||||||
tokio_tungstenite::accept_hdr_async(stream, |req: &Request, resp: Response| {
|
loop {
|
||||||
if let Some(header) = req.headers().get("X-Token") {
|
let (stream, _) = listener.accept().await?;
|
||||||
if header.as_bytes() == auth_token.as_bytes() {
|
let (send, recv) =
|
||||||
Ok(resp)
|
tokio_tungstenite::accept_hdr_async(stream, |req: &Request, resp: Response| {
|
||||||
|
if let Some(header) = req.headers().get("X-Token") {
|
||||||
|
if header.as_bytes() == auth_token.as_bytes() {
|
||||||
|
Ok(resp)
|
||||||
|
} else {
|
||||||
|
let mut resp = ErrorResponse::new(None);
|
||||||
|
*resp.status_mut() = StatusCode::UNAUTHORIZED;
|
||||||
|
Err(resp)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut resp = ErrorResponse::new(None);
|
let mut resp = ErrorResponse::new(None);
|
||||||
*resp.status_mut() = StatusCode::UNAUTHORIZED;
|
*resp.status_mut() = StatusCode::UNAUTHORIZED;
|
||||||
Err(resp)
|
Err(resp)
|
||||||
}
|
}
|
||||||
} else {
|
})
|
||||||
let mut resp = ErrorResponse::new(None);
|
.await?
|
||||||
*resp.status_mut() = StatusCode::UNAUTHORIZED;
|
.split();
|
||||||
Err(resp)
|
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
|
||||||
}
|
let mut client = Client::builder(token.clone(), intents)
|
||||||
})
|
.event_handler(Handler(Mutex::new(send), channel_id))
|
||||||
.await?
|
.await?;
|
||||||
.split();
|
tokio::try_join!(
|
||||||
// Login with a bot token from the environment
|
client.start().map_err(|e| e.into()),
|
||||||
let token = env::var("DISCORD_TOKEN").expect("token");
|
handle_recv(recv, &http, webhook.clone())
|
||||||
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
|
)?;
|
||||||
let mut client = Client::builder(token, intents)
|
}
|
||||||
.event_handler(Handler(Mutex::new(send), channel_id))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// start listening for events by starting a single shard
|
|
||||||
tokio::try_join!(
|
|
||||||
client.start().map_err(|e| e.into()),
|
|
||||||
handle_recv(recv, http, webhook)
|
|
||||||
)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue