Skip to content

Commit 62333cc

Browse files
PeroSarcyberknight777
authored andcommitted
src: Improve command handler to not hardcode username
Signed-off-by: PeroSar <perosar1111@gmail.com>
1 parent 8b68122 commit 62333cc

2 files changed

Lines changed: 55 additions & 43 deletions

File tree

src/init.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub async fn async_main() -> Result {
3939
log::info!("Signed in!");
4040
}
4141

42+
let me = client.get_me().await?;
43+
let bot_username = me.username().unwrap_or("").to_string();
44+
4245
log::info!("Waiting for messages...");
4346

4447
let mut updates = client
@@ -52,8 +55,9 @@ pub async fn async_main() -> Result {
5255
};
5356

5457
let handle = client.clone();
58+
let bot_username = bot_username.clone();
5559
task::spawn(async move {
56-
match plugins::handle_update(handle, update).await {
60+
match plugins::handle_update(handle, update, &bot_username).await {
5761
Ok(_) => {}
5862
Err(e) => log::error!("Error handling updates!: {}", e),
5963
}

src/plugins/mod.rs

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -79,60 +79,59 @@ enum Command {
7979
Yaap(String),
8080
}
8181

82-
pub async fn handle_update(client: Client, update: Update) -> Result {
82+
pub async fn handle_update(client: Client, update: Update, bot_username: &str) -> Result {
8383
let config = Arc::new(cfg::Config::read().expect("cannot read the config"));
8484
match update {
8585
Update::NewMessage(message)
86-
if check_msg(&message) || check_cmd(&message, config.clone().admin_id) =>
86+
if check_msg(&message, bot_username)
87+
|| check_cmd(&message, config.clone().admin_id) =>
8788
{
8889
log::info!(
8990
"Responding to {}",
9091
message.peer().and_then(|p| p.name()).unwrap_or("")
9192
);
92-
handle_msg(client, &message).await?
93+
handle_msg(client, &message, bot_username).await?
9394
}
9495
_ => {}
9596
}
9697

9798
Ok(())
9899
}
99100

100-
pub async fn handle_msg(client: Client, message: &Message) -> Result {
101+
pub async fn handle_msg(client: Client, message: &Message, bot_username: &str) -> Result {
101102
let msg = message.text();
102103
let cmd = msg.split_whitespace().next().unwrap_or("");
103104
let args = msg.split_whitespace().skip(1).collect::<Vec<_>>();
104-
let cmd = match cmd {
105-
"/anyone" | "/anyone@ThekNIGHT_bot" => Command::Anyone,
106-
"/aur" | "/aur@ThekNIGHT_bot" => Command::Aur(args.join(" ")),
107-
"/cat" | "/cat@ThekNIGHT_bot" => Command::Cat(args.join(" ").parse().unwrap_or_default()),
105+
let cmd = match bot_command(cmd, bot_username).unwrap_or(cmd) {
106+
"/anyone" => Command::Anyone,
107+
"/aur" => Command::Aur(args.join(" ")),
108+
"/cat" => Command::Cat(args.join(" ").parse().unwrap_or_default()),
108109
"k.dl" => Command::Dl(args.join(" ")),
109-
"/dog" | "/dog@ThekNIGHT_bot" => Command::Dog(args.join(" ").parse().unwrap_or_default()),
110-
"/eightball" | "/eightball@ThekNIGHT_bot" => Command::EightBall,
111-
"/flipcoin" | "/flipcoin@ThekNIGHT_bot" => Command::FlipCoin,
112-
"/help" | "/help@ThekNIGHT_bot" => Command::Help,
113-
"/ipa" | "/ipa@ThekNIGHT_bot" => Command::Ipa(args.join(" ")),
114-
"/link" | "/link@ThekNIGHT_bot" => Command::Link(args.join(" ")),
115-
"/lpaste" | "/lpaste@ThekNIGHT_bot" => Command::Lpaste(args.join(" ")),
116-
"/luck" | "/luck@ThekNIGHT_bot" => Command::Luck,
117-
"/magisk" | "/magisk@ThekNIGHT_bot" => Command::Magisk,
118-
"/man" | "/man@ThekNIGHT_bot" => Command::Man(args.join(" ")),
119-
"/msg" | "/msg@ThekNIGHT_bot" => Command::Msg(args.join(" ")),
120-
"/neo" | "/neo@ThekNIGHT_bot" => Command::Neo,
121-
"/ping" | "/ping@ThekNIGHT_bot" => Command::Ping,
122-
"/paste" | "/paste@ThekNIGHT_bot" => Command::Paste(args.join(" ")),
123-
"/plant" | "/plant@ThekNIGHT_bot" => {
124-
Command::Plant(args.join(" ").parse().unwrap_or_default())
125-
}
126-
"/rtfm" | "/rtfm@ThekNIGHT_bot" => Command::Rtfm,
127-
"/run" | "/run@ThekNIGHT_bot" => Command::Run,
128-
"/sauce" | "/sauce@ThekNIGHT_bot" => Command::Sauce,
129-
"/smsg" | "/smsg@ThekNIGHT_bot" => Command::Smsg(args.join(" ")),
130-
"/start" | "/start@ThekNIGHT_bot" => Command::Start,
131-
"/uid" | "/uid@ThekNIGHT_bot" => Command::Uid,
110+
"/dog" => Command::Dog(args.join(" ").parse().unwrap_or_default()),
111+
"/eightball" => Command::EightBall,
112+
"/flipcoin" => Command::FlipCoin,
113+
"/help" => Command::Help,
114+
"/ipa" => Command::Ipa(args.join(" ")),
115+
"/link" => Command::Link(args.join(" ")),
116+
"/lpaste" => Command::Lpaste(args.join(" ")),
117+
"/luck" => Command::Luck,
118+
"/magisk" => Command::Magisk,
119+
"/man" => Command::Man(args.join(" ")),
120+
"/msg" => Command::Msg(args.join(" ")),
121+
"/neo" => Command::Neo,
122+
"/ping" => Command::Ping,
123+
"/paste" => Command::Paste(args.join(" ")),
124+
"/plant" => Command::Plant(args.join(" ").parse().unwrap_or_default()),
125+
"/rtfm" => Command::Rtfm,
126+
"/run" => Command::Run,
127+
"/sauce" => Command::Sauce,
128+
"/smsg" => Command::Smsg(args.join(" ")),
129+
"/start" => Command::Start,
130+
"/uid" => Command::Uid,
132131
"k.ul" => Command::Ul(args.join(" ")),
133-
"/urb" | "/urb@ThekNIGHT_bot" => Command::Urb(args.join(" ")),
134-
"/whois" | "/whois@ThekNIGHT_bot" => Command::Whois(args.join(" ")),
135-
"/yaap" | "/yaap@ThekNIGHT_bot" => Command::Yaap(args.join(" ")),
132+
"/urb" => Command::Urb(args.join(" ")),
133+
"/whois" => Command::Whois(args.join(" ")),
134+
"/yaap" => Command::Yaap(args.join(" ")),
136135
"k.sh" => Command::Sh(args.join(" ").parse().unwrap_or_default()),
137136
"k.mot" => Command::Mot(
138137
args.get(0).unwrap_or(&"").to_string(),
@@ -181,15 +180,24 @@ pub async fn handle_msg(client: Client, message: &Message) -> Result {
181180
Ok(())
182181
}
183182

184-
fn check_msg(message: &Message) -> bool {
183+
fn bot_command<'a>(cmd: &'a str, bot_username: &str) -> Option<&'a str> {
184+
let Some((command, username)) = cmd.split_once('@') else {
185+
return Some(cmd);
186+
};
187+
188+
username
189+
.eq_ignore_ascii_case(bot_username)
190+
.then_some(command)
191+
}
192+
193+
fn check_msg(message: &Message, bot_username: &str) -> bool {
194+
let text = message.text();
195+
let cmd = text.split_whitespace().next().unwrap_or("");
196+
185197
return !message.outgoing()
186-
&& message.text().starts_with('/')
187-
&& !message.text().starts_with("/ ")
188-
|| (message.text().ends_with("@ThekNIGHT_bot")
189-
&& !message.text().starts_with("k.sh")
190-
&& !message.text().starts_with("k.mot")
191-
&& !message.text().starts_with("k.ul")
192-
&& !message.text().starts_with("k.dl"));
198+
&& text.starts_with('/')
199+
&& !text.starts_with("/ ")
200+
&& bot_command(cmd, bot_username).is_some();
193201
}
194202

195203
fn check_cmd(message: &Message, admin_id: i64) -> bool {

0 commit comments

Comments
 (0)