summary refs log tree commit diff
path: root/src/system/plugin/reproxy.rs
blob: 145bed85c45d0228aa9087f51d83223d66f8ac54 (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
use async_trait::async_trait;
use twilight_model::id::{marker::ChannelMarker, Id};
use twilight_model::channel::Message;
use crate::system::util;
use crate::system::{log::Logger, types::{Response, System}};

use super::{PluginCommand, SeancePlugin};

pub struct Reproxy;

impl Reproxy {
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl<'system> SeancePlugin<'system> for Reproxy {
    fn get_commands(&self) -> Vec<PluginCommand> {
        vec![]
    }

    async fn handle_command<'message>(&self, _logger: &'system Logger, _system: &'system System, _message: &'message Message, _command: PluginCommand, _args: Vec<&'message str>) {
        unreachable!("Reproxy has no commands");
    }

    async fn handle_message<'message>(&self, logger: &'system Logger, system: &'system System, message: &'message Message, response: &'message mut Response) {
        if let Response::Proxy { member, content } = response {
            if content.as_str().trim() == "*" {
                let most_recent_message = system.get_most_recent_message(message.channel_id).await;
                if let Some((reproxy_target, update_most_recent)) = match message.kind {
                    twilight_model::channel::message::MessageType::Regular => most_recent_message.map(|(message, _)| (message, true)),
                    twilight_model::channel::message::MessageType::Reply => async {Some((
                        message.referenced_message.as_ref()?.as_ref().clone(),
                        message.referenced_message.as_ref()?.id == most_recent_message?.0.id
                    ))}.await,
                    _ => todo!(),
                } {
                    if let Ok(new_message) = util::duplicate_message(&member.client, &reproxy_target, reproxy_target.content.as_str()).await {
                        let _ = {member.client.lock().await.delete_message(reproxy_target.channel_id, reproxy_target.id).await};

                        if update_most_recent {
                            system.cache_most_recent_message(new_message.channel_id, new_message, member.clone()).await;
                        }

                        *response = Response::ExplicitNoop { delete_source: true, member: Some(member.clone()) };
                    } else {
                        logger.log_err(None, format!("Error reproxying message")).await;
                        *response = Response::ExplicitNoop { delete_source: false, member: None };
                    }
                } else {
                    logger.log_err(None, format!("Could not find reproxy target")).await;
                    *response = Response::ExplicitNoop { delete_source: false, member: None };
                }
            }
        }
    }

    async fn post_response<'message>(&self, _logger: &'system Logger, _system: &'system System, _message: &'message Message, _channel: Id<ChannelMarker>, _response: &'message Response) {
        // noop
    }
}