aboutsummaryrefslogtreecommitdiffstats
path: root/src/discord.rs
blob: e4221710450b6cd87be12fe0b30c7195d2106e92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::agenda::AgendaPoint;

use discord::{
    model::{
        ChannelId,
        Event,
    },
    Discord,
    Error,
};
use futures::join;
use tokio::{
    sync::mpsc,
    task::{
        spawn,
        spawn_blocking,
    },
};

pub async fn handle(
    token: Option<String>,
    sender: mpsc::UnboundedSender<AgendaPoint>,
    receiver: mpsc::UnboundedReceiver<AgendaPoint>,
) {
    println!("Setting up Discord");

    let token = std::env::var("DISCORD_API_TOKEN").unwrap_or(token.unwrap());
    let client = Discord::from_bot_token(&token);

    if let Ok(client) = client {
        let (connection, _) = client.connect().expect("Discord connect failed"); //TODO
        let our_id = client.get_current_user().unwrap().id;
        println!("Discord ready");

        let (_, _) = join!( //TODO?
            spawn_blocking(move || receive_events(our_id, connection, sender)),
            spawn(receive_from_slack(receiver, client))
        );
    }
}

fn receive_events(
    our_id: discord::model::UserId,
    mut connection: discord::Connection,
    sender: mpsc::UnboundedSender<AgendaPoint>
) {
    loop {
        match connection.recv_event() {
            Ok(Event::MessageCreate(message)) => {
                if message.author.id != our_id {
                    sender.send(AgendaPoint{
                        title: message.content,
                        adder: message.author.name,
                    }).unwrap();
                }
            }
            Ok(_) => {}
            Err(Error::Closed(code, body)) => {
                println!("Discord closed with code {:?}: {}", code, body);
                break;
            }
            Err(e) => {
                println!("Discord error: {:?}", e);
            }
        }
    }
}

async fn receive_from_slack(
    mut receiver: mpsc::UnboundedReceiver<AgendaPoint>,
    client: discord::Discord,
) {
    while let Some(point) = receiver.recv().await {
        println!("Discord received '{}'", point);
        client.send_message(ChannelId(697057150106599488), //TODO
                            &point.to_add_message(),
                            "",
                            false
        );
    }

}