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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
use async_trait::async_trait;
use std::sync::Arc;
use tokio::sync::Mutex;
use twilight_model::{channel::{Channel, Message}, id::{marker::ChannelMarker, Id}, util::Timestamp};
use crate::system::types::{System, Member, Response};
use super::{CommandOutcome, SeancePlugin};
use tokio::time::sleep;
use std::time::Duration;
pub struct Autoproxy {
current_state: Arc<Mutex<InnerState>>,
}
#[derive(Clone)]
enum InnerState {
Off,
LatchActive { current_member: Member, last_message: Timestamp },
LatchInactive,
Member { current_member: Member },
}
impl Autoproxy {
pub fn new() -> Self {
Self {
current_state: Arc::new(Mutex::new(InnerState::Off))
}
}
}
#[async_trait]
impl SeancePlugin for Autoproxy {
async fn handle_command(&self, system: &System, message: &Message) -> CommandOutcome {
if message.content.starts_with(format!("{}auto ", system.command_prefix).as_str()) {
let args = message.content.replace(format!("{}auto ", system.command_prefix).as_str(), "");
let mut remainder = args.split_whitespace();
let first_word = remainder.next();
match first_word {
Some("off") => *self.current_state.lock().await = InnerState::Off,
Some("latch") => *self.current_state.lock().await = InnerState::LatchInactive,
Some("member") => {
return CommandOutcome::Errored { message: "Member mode not supported yet".to_string() }
},
Some(other) => return CommandOutcome::Errored { message: format!("Unknown autoproxy mode: {other}") },
None => return CommandOutcome::Errored { message: "Must specify autoproxy mode".to_string() },
}
CommandOutcome::Handled
} else {
CommandOutcome::Skipped
}
}
async fn handle_message(&self, system: &System, message: &Message, response: &mut Response) {
if message.content.starts_with("\\") {
if message.content.starts_with("\\\\") {
if let InnerState::LatchActive {current_member: _, last_message: _} = {self.current_state.lock().await.clone()} {
{*self.current_state.lock().await = InnerState::LatchInactive};
}
}
*response = Response::Noop { delete_source: message.content == "\\\\" };
return
}
if let Response::Noop { delete_source: _ } = response {
let current_state = {self.current_state.lock().await.clone()};
match current_state {
InnerState::Off => return,
InnerState::LatchInactive => return,
InnerState::Member { current_member } => {
*response = Response::Proxy {
member: current_member.clone(),
content: message.content.clone(),
}
},
InnerState::LatchActive { current_member, last_message: _ } => {
*response = Response::Proxy {
member: current_member.clone(),
content: message.content.clone(),
}
},
}
}
}
async fn post_response(&self, system: &System, message: &Message, channel: Id<ChannelMarker>, response: &Response) {
match response {
Response::Noop { delete_source } => return,
Response::Proxy { member, content } => {
let current_state = {self.current_state.lock().await.clone()};
match current_state {
InnerState::Off => return,
InnerState::Member { current_member } => return,
InnerState::LatchInactive => {
{*self.current_state.lock().await = InnerState::LatchActive {
current_member: member.clone(),
last_message: message.timestamp,
}};
let state_arc = self.current_state.clone();
let sent_member = member.clone();
let sent_timestamp = message.timestamp.clone();
tokio::spawn(async move {
sleep(Duration::from_secs(15 * 60)).await;
let current_state = {state_arc.lock().await.clone()};
if let InnerState::LatchActive { current_member, last_message } = current_state {
if sent_member.discord_token == current_member.discord_token && sent_timestamp.as_micros() == last_message.as_micros() {
{*state_arc.lock().await = InnerState::LatchInactive};
}
}
});
},
InnerState::LatchActive { current_member: _, last_message: _ } => {
{*self.current_state.lock().await = InnerState::LatchActive {
current_member: member.clone(),
last_message: message.timestamp,
}};
let state_arc = self.current_state.clone();
let sent_member = member.clone();
let sent_timestamp = message.timestamp.clone();
tokio::spawn(async move {
sleep(Duration::from_secs(15 * 60)).await;
let current_state = {state_arc.lock().await.clone()};
if let InnerState::LatchActive { current_member, last_message } = current_state {
if sent_member.discord_token == current_member.discord_token && sent_timestamp.as_micros() == last_message.as_micros() {
{*state_arc.lock().await = InnerState::LatchInactive};
}
}
});
},
}
},
}
}
}
|