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(
|
||||
mut recv: SplitStream<WebSocketStream<TcpStream>>,
|
||||
http: Http,
|
||||
http: &Http,
|
||||
webhook: Webhook,
|
||||
) -> anyhow::Result<()> {
|
||||
while let Some(msg) = recv.next().await {
|
||||
if let WsMessage::Text(content) = msg? {
|
||||
let message: ChatMessage = serde_json::from_str(&content)?;
|
||||
webhook
|
||||
.execute(&http, false, |w| {
|
||||
.execute(http, false, |w| {
|
||||
w.content(message.message)
|
||||
.username(message.user)
|
||||
.avatar_url(format!(
|
||||
|
@ -92,36 +92,35 @@ async fn main() -> anyhow::Result<()> {
|
|||
let channel_id = env::var("CHANNEL_ID").expect("channel id").parse()?;
|
||||
|
||||
let listener = TcpListener::bind(env::var("BIND_ADDR").expect("bind address")).await?;
|
||||
let (stream, _) = listener.accept().await?;
|
||||
let (send, recv) =
|
||||
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)
|
||||
let token = env::var("DISCORD_TOKEN").expect("token");
|
||||
|
||||
loop {
|
||||
let (stream, _) = listener.accept().await?;
|
||||
let (send, recv) =
|
||||
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 {
|
||||
let mut resp = ErrorResponse::new(None);
|
||||
*resp.status_mut() = StatusCode::UNAUTHORIZED;
|
||||
Err(resp)
|
||||
}
|
||||
} else {
|
||||
let mut resp = ErrorResponse::new(None);
|
||||
*resp.status_mut() = StatusCode::UNAUTHORIZED;
|
||||
Err(resp)
|
||||
}
|
||||
})
|
||||
.await?
|
||||
.split();
|
||||
// Login with a bot token from the environment
|
||||
let token = env::var("DISCORD_TOKEN").expect("token");
|
||||
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(())
|
||||
})
|
||||
.await?
|
||||
.split();
|
||||
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?;
|
||||
tokio::try_join!(
|
||||
client.start().map_err(|e| e.into()),
|
||||
handle_recv(recv, &http, webhook.clone())
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue