looooopy
This commit is contained in:
parent
1013b7153f
commit
ecd1fdf001
1 changed files with 28 additions and 29 deletions
17
src/main.rs
17
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,6 +92,9 @@ 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 token = env::var("DISCORD_TOKEN").expect("token");
|
||||||
|
|
||||||
|
loop {
|
||||||
let (stream, _) = listener.accept().await?;
|
let (stream, _) = listener.accept().await?;
|
||||||
let (send, recv) =
|
let (send, recv) =
|
||||||
tokio_tungstenite::accept_hdr_async(stream, |req: &Request, resp: Response| {
|
tokio_tungstenite::accept_hdr_async(stream, |req: &Request, resp: Response| {
|
||||||
|
@ -111,17 +114,13 @@ async fn main() -> anyhow::Result<()> {
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
.split();
|
.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 intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
|
||||||
let mut client = Client::builder(token, intents)
|
let mut client = Client::builder(token.clone(), intents)
|
||||||
.event_handler(Handler(Mutex::new(send), channel_id))
|
.event_handler(Handler(Mutex::new(send), channel_id))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// start listening for events by starting a single shard
|
|
||||||
tokio::try_join!(
|
tokio::try_join!(
|
||||||
client.start().map_err(|e| e.into()),
|
client.start().map_err(|e| e.into()),
|
||||||
handle_recv(recv, http, webhook)
|
handle_recv(recv, &http, webhook.clone())
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue