-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheditguar_command.rs
More file actions
202 lines (185 loc) · 7.76 KB
/
editguar_command.rs
File metadata and controls
202 lines (185 loc) · 7.76 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use {
crate::{
actors::bot_actor::ActorUpdateMessage,
commands::{admin_check, guardian_lookup, match_command, validate_username},
},
entity::guardians,
kameo::message::Context,
sea_orm::{ActiveModelTrait, Set},
};
command_actor!(
EditGuardianCommand,
"editguar",
"Edit information about registered guardians"
);
impl Message<ActorUpdateMessage> for EditGuardianCommand {
type Reply = ();
async fn handle(
&mut self,
message: ActorUpdateMessage,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
if let (Some(_), args) =
match_command(message.update.text(), Self::prefix(), &self.bot_name)
{
if args.is_none() {
return self.usage(&message).await;
}
// Split args in two or three:
// guardian id,
// subcommand,
// and optionally, parameters
let args = args.unwrap();
let args: Vec<&str> = args.splitn(3, ' ').collect();
if args.is_empty() || args.len() == 2 {
return self.usage(&message).await;
}
let name = args[0];
let connection = self.connection();
let guardian = if name == "my" {
let guardian = validate_username(&self.bot_ref, &message, connection).await;
if guardian.is_none() {
return; // TODO: You are not registered
}
guardian.unwrap()
} else {
let admin = admin_check(&self.bot_ref, &message, connection).await;
if admin.is_none() {
return self.send_reply(&message, "❌ You are not admin").await;
}
let guardian = guardian_lookup(name, connection).await;
let guardian = match guardian {
Ok(Some(guardian)) => Some(guardian),
Ok(None) => {
self.send_reply(&message, format!("❌ Guardian {} was not found.", &name))
.await;
None
}
Err(_) => {
self.send_reply(&message, "❌ Error querying guardian by name.")
.await;
None
}
};
if guardian.is_none() {
return;
}
guardian.unwrap()
};
if args.len() == 1 {
let info = format!(
"{clan}{name} {email} {admin} {rising}",
clan = guardian
.psn_clan
.clone()
.map(|s| format!("[{}] ", s))
.unwrap_or_default(),
name = guardian.telegram_name,
email = guardian.email.clone().unwrap_or("<no email>".into()),
admin = if guardian.is_superadmin {
"<superadmin>"
} else if guardian.is_admin {
"<admin>"
} else {
""
},
rising = guardian.format_destiny_rising_id(),
);
return self.send_reply(&message, info).await;
}
let command = args[1];
let value = args[2];
match command {
"psn" => {
let mut guardian: guardians::ActiveModel = guardian.into();
guardian.psn_name = Set(value.to_string());
if guardian.update(connection).await.is_err() {
return self.send_reply(&message, "❌ Failed to update PSN").await;
}
self.send_reply(&message, "✅ PSN updated successfully")
.await;
}
"clan" => {
let clan_value = if value == "delete" {
None
} else {
Some(value.to_string())
};
let mut guardian: guardians::ActiveModel = guardian.into();
guardian.psn_clan = Set(clan_value);
if guardian.update(connection).await.is_err() {
return self.send_reply(&message, "❌ Failed to update clan").await;
}
self.send_reply(&message, "✅ Updated guardian clan").await;
}
"rising" => {
if value.is_empty() {
return self
.send_reply(
&message,
"❌ Rising needs at least an UID, but better UID and nickname",
)
.await;
}
let split = value.split_once(' ');
if split.is_none() {
let Ok(uid) = value.parse::<i64>() else {
return self
.send_reply(&message, "❌ Destiny: Rising uid must be a number")
.await;
};
let mut guardian: guardians::ActiveModel = guardian.into();
guardian.rising_uid = Set(Some(uid));
if guardian.update(connection).await.is_err() {
return self
.send_reply(&message, "❌ Failed to update Destiny: Rising uid")
.await;
}
return self
.send_reply(&message, "✅ Updated guardian Destiny: Rising uid")
.await;
}
let (uid, nickname) = split.unwrap();
let Ok(uid) = uid.parse::<i64>() else {
return self
.send_reply(&message, "❌ Destiny: Rising uid must be a number")
.await;
};
let mut guardian: guardians::ActiveModel = guardian.into();
guardian.rising_uid = Set(Some(uid));
guardian.rising_nickname = Set(Some(nickname.to_string()));
if guardian.update(connection).await.is_err() {
return self
.send_reply(
&message,
"❌ Failed to update Destiny: Rising uid and nickname",
)
.await;
}
self.send_reply(
&message,
"✅ Updated guardian Destiny: Rising uid and nickname",
)
.await;
}
"email" => {
let email_value = if value == "delete" {
None
} else {
Some(value.to_string())
};
let mut guardian: guardians::ActiveModel = guardian.into();
guardian.email = Set(email_value);
if guardian.update(connection).await.is_err() {
return self.send_reply(&message, "❌ Failed to update email").await;
}
self.send_reply(&message, "✅ Updated guardian email").await;
}
_ => {
self.send_reply(&message, "⁉️ Unknown information field")
.await;
}
}
}
}
}