can now use reactions on lang command

This commit is contained in:
jellywx 2020-11-23 14:11:57 +00:00
parent 06d13ec810
commit 3c1fe1f091
3 changed files with 105 additions and 22 deletions

View File

@ -3,7 +3,7 @@ use regex_command_attr::command;
use serenity::{ use serenity::{
client::Context, client::Context,
framework::Framework, framework::Framework,
model::{channel::Message, id::ChannelId, id::RoleId}, model::{channel::Message, channel::ReactionType, id::ChannelId, id::RoleId},
}; };
use chrono_tz::{Tz, TZ_VARIANTS}; use chrono_tz::{Tz, TZ_VARIANTS};
@ -22,7 +22,7 @@ use crate::{
FrameworkCtx, SQLPool, FrameworkCtx, SQLPool,
}; };
use std::iter; use std::{iter, time::Duration};
#[command] #[command]
#[supports_dm(false)] #[supports_dm(false)]
@ -205,7 +205,7 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
.format("%H:%M") .format("%H:%M")
.to_string() .to_string()
), ),
false, true,
) )
}); });
@ -243,30 +243,103 @@ async fn language(ctx: &Context, msg: &Message, args: String) {
let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let mut user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
match lm.get_language(&args) { if !args.is_empty() {
Some(row) => { match lm.get_language(&args) {
user_data.language = row.to_string(); Some(lang) => {
user_data.language = lang.to_string();
user_data.commit_changes(&pool).await; user_data.commit_changes(&pool).await;
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, lm.get(&user_data.language, "lang/set_p")) .send_message(&ctx, |m| {
.await; m.embed(|e| {
e.title(lm.get(&user_data.language, "lang/set_p_title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/set_p"))
})
})
.await;
}
None => {
let language_codes = lm.all_languages().map(|(k, v)| {
(
format!("{} {}", lm.get(k, "flag"), v.to_title_case()),
format!("`$lang {}`", k.to_uppercase()),
true,
)
});
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(lm.get(&user_data.language, "lang/invalid_title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/invalid"))
.fields(language_codes)
})
})
.await;
}
} }
} else {
let language_codes = lm.all_languages().map(|(k, v)| {
(
format!("{} {}", lm.get(k, "flag"), v.to_title_case()),
format!("`$lang {}`", k.to_uppercase()),
true,
)
});
None => { let flags = lm
let language_codes = lm .all_languages()
.all_languages() .map(|(k, _)| ReactionType::Unicode(lm.get(k, "flag").to_string()));
.map(|(k, v)| format!("{} ({})", v.to_title_case(), k.to_uppercase()))
.collect::<Vec<String>>()
.join("\n");
let content = let reactor = msg
lm.get(&user_data.language, "lang/invalid") .channel_id
.replacen("{}", &language_codes, 1); .send_message(&ctx, |m| {
m.embed(|e| {
e.title(lm.get(&user_data.language, "lang/select_title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/select"))
.fields(language_codes)
})
.reactions(flags)
})
.await;
let _ = msg.channel_id.say(&ctx, content).await; if let Ok(sent_msg) = reactor {
let reaction_reply = sent_msg
.await_reaction(&ctx)
.timeout(Duration::from_secs(45))
.await;
if let Some(reaction_action) = reaction_reply {
if reaction_action.is_added() {
if let ReactionType::Unicode(emoji) = &reaction_action.as_inner_ref().emoji {
if let Some(lang) = lm.get_language_by_flag(emoji) {
user_data.language = lang.to_string();
user_data.commit_changes(&pool).await;
let _ = msg
.channel_id
.send_message(&ctx, |m| {
m.embed(|e| {
e.title(lm.get(&user_data.language, "lang/set_p_title"))
.color(*THEME_COLOR)
.description(lm.get(&user_data.language, "lang/set_p"))
})
})
.await;
}
}
}
}
let _ = sent_msg.delete_reactions(&ctx).await;
} }
} }
} }

View File

@ -47,6 +47,14 @@ impl LanguageManager {
.next() .next()
} }
pub fn get_language_by_flag(&self, flag: &str) -> Option<&str> {
self.languages
.iter()
.filter(|(k, _)| self.get(k, "flag") == flag)
.map(|(k, _)| k.as_str())
.next()
}
pub fn all_languages(&self) -> impl Iterator<Item = (&str, &str)> { pub fn all_languages(&self) -> impl Iterator<Item = (&str, &str)> {
self.languages.iter().map(|(k, v)| (k.as_str(), v.as_str())) self.languages.iter().map(|(k, v)| (k.as_str(), v.as_str()))
} }

View File

@ -217,7 +217,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.intents( .intents(
GatewayIntents::GUILD_MESSAGES GatewayIntents::GUILD_MESSAGES
| GatewayIntents::GUILDS | GatewayIntents::GUILDS
| GatewayIntents::DIRECT_MESSAGES, | GatewayIntents::GUILD_MESSAGE_REACTIONS
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::DIRECT_MESSAGE_REACTIONS,
) )
.event_handler(Handler) .event_handler(Handler)
.framework_arc(framework_arc.clone()) .framework_arc(framework_arc.clone())