summary refs log tree commit diff
path: root/src/system/bot/mod.rs
blob: 3c4585f91e49ea1d83d2586cee8bbde5374ef460 (plain)
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
83
84
85
mod client;
mod gateway;

use std::sync::Arc;
use tokio::sync::mpsc::Sender;
use tokio::sync::RwLock;
use twilight_http::error::Error as TwiError;
use twilight_http::request::channel::reaction::RequestReactionType;

pub use super::types::*;
pub use client::MessageDuplicateError;
use gateway::Gateway;
use client::Client;

#[derive(Clone)]
pub struct BotConfig {
    pub member_id: MemberId,
    pub reference_user_id: UserId,
    pub discord_token: String,
    pub last_status: Status,
    pub message_handler: Option<Sender<MessageEvent>>,
    pub system_handler: Option<Sender<SystemEvent>>,
}

pub struct Bot {
    bot_conf: Arc<RwLock<BotConfig>>,
    gateway: Gateway,
    client: Client,
}

impl Bot {
    pub fn new(
        member_id: MemberId,
        config: &crate::config::Member,
        reference_user_id: UserId,
    ) -> Self {
        let bot_conf = Arc::new(RwLock::new(BotConfig {
            member_id,
            reference_user_id,
            discord_token: config.discord_token.clone(),
            last_status: Status::Online,
            message_handler: None,
            system_handler: None,
        }));

        Self {
            gateway: Gateway::new(&config.discord_token, &bot_conf),
            client: Client::new(&config.discord_token, &bot_conf),
            bot_conf,
        }
    }

    pub async fn set_message_handler(&mut self, handler: Sender<MessageEvent>) {
        self.bot_conf.write().await.message_handler = Some(handler);
    }

    pub async fn set_system_handler(&mut self, handler: Sender<SystemEvent>) {
        self.bot_conf.write().await.system_handler = Some(handler);
    }

    pub async fn set_status(&self, status: Status) {
        self.gateway.set_status(status).await;
    }

    pub fn start(&self) {
        self.gateway.start_listening()
    }

    pub async fn refetch_message(&self, message_id: MessageId, channel_id: ChannelId) {
        self.client.refetch_message(message_id, channel_id).await;
    }

    pub async fn delete_message(&self, channel_id: ChannelId, message_id: MessageId) -> Result<(), TwiError> {
        self.client.delete_message(channel_id, message_id).await
    }

    pub async fn react_message(&self, channel_id: ChannelId, message_id: MessageId, react: &'_ RequestReactionType<'_>) -> Result<(), TwiError> {
        self.client.react_message(channel_id, message_id, react).await
    }

    pub async fn duplicate_message(&self, message_id: &TwiMessage, content: &str) ->  Result<TwiMessage, MessageDuplicateError> {
        self.client.duplicate_message(message_id, content).await
    }
}