removed all remaining instances of UserData::response. languagemanager now loads from string compiled into executable. languagemanager falls back properly. removed unnecessary param from language_of

This commit is contained in:
jellywx 2020-11-22 01:31:50 +00:00
parent 6a7491d094
commit d8bf0240e4
9 changed files with 371 additions and 346 deletions

View File

@ -16,7 +16,9 @@ Reminder Bot can be built by running `cargo build --release` in the top level di
#### Compilation environment variables #### Compilation environment variables
These environment variables must be provided when compiling the bot These environment variables must be provided when compiling the bot
* `DATABASE_URL` - the URL of your MySQL database (`mysql://user[:password]@domain/database`)
* `WEBHOOK_AVATAR` - accepts the name of an image file located in `$CARGO_MANIFEST_DIR/assets/` to be used as the avatar when creating webhooks. **IMPORTANT: image file must be 128x128 or smaller in size** * `WEBHOOK_AVATAR` - accepts the name of an image file located in `$CARGO_MANIFEST_DIR/assets/` to be used as the avatar when creating webhooks. **IMPORTANT: image file must be 128x128 or smaller in size**
* `STRINGS_FILE` - accepts the name of a compiled strings file located in `$CARGO_MANIFEST_DIR/assets/` to be used for creating messages. Compiled string files can be generated with `compile.py` at https://github.com/reminder-bot/languages
### Setting up Python ### Setting up Python
Reminder Bot by default looks for a venv within it's working directory to run Python out of. To set up a venv, install `python3-venv` and run `python3 -m venv venv`. Then, run `source venv/bin/activate` to activate the venv, and do `pip install dateparser` to install the required library Reminder Bot by default looks for a venv within it's working directory to run Python out of. To set up a venv, install `python3-venv` and run `python3 -m venv venv`. Then, run `source venv/bin/activate` to activate the venv, and do `pip install dateparser` to install the required library

View File

@ -41,7 +41,7 @@ async fn help(ctx: &Context, msg: &Message, _args: String) {
let lm = data.get::<LanguageManager>().unwrap(); let lm = data.get::<LanguageManager>().unwrap();
let language = UserData::language_of(&msg.author, &ctx, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let desc = lm.get(&language, "help"); let desc = lm.get(&language, "help");
@ -75,7 +75,7 @@ async fn info(ctx: &Context, msg: &Message, _args: String) {
let lm = data.get::<LanguageManager>().unwrap(); let lm = data.get::<LanguageManager>().unwrap();
let language = UserData::language_of(&msg.author, &ctx, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
.await .await
.unwrap(); .unwrap();
@ -116,7 +116,7 @@ async fn donate(ctx: &Context, msg: &Message, _args: String) {
let lm = data.get::<LanguageManager>().unwrap(); let lm = data.get::<LanguageManager>().unwrap();
let language = UserData::language_of(&msg.author, &ctx, &pool).await; let language = UserData::language_of(&msg.author, &pool).await;
let desc = lm.get(&language, "donate"); let desc = lm.get(&language, "donate");
let _ = msg let _ = msg
@ -161,28 +161,26 @@ async fn dashboard(ctx: &Context, msg: &Message, _args: String) {
#[command] #[command]
async fn clock(ctx: &Context, msg: &Message, args: String) { async fn clock(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let data = ctx.data.read().await;
.data
.read() let pool = data
.await
.get::<SQLPool>() .get::<SQLPool>()
.cloned() .cloned()
.expect("Could not get SQLPool from data"); .expect("Could not get SQLPool from data");
let lm = data.get::<LanguageManager>().unwrap();
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let now = Utc::now().with_timezone(&user_data.timezone()); let now = Utc::now().with_timezone(&user_data.timezone());
let clock_display = lm.get(&user_data.language, "clock/time");
if args == "12" { if args == "12" {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(
&ctx, &ctx,
user_data.response(&pool, "clock/time").await.replacen( clock_display.replacen("{}", &now.format("%I:%M:%S %p").to_string(), 1),
"{}",
&now.format("%I:%M:%S %p").to_string(),
1,
),
) )
.await; .await;
} else { } else {
@ -190,11 +188,7 @@ async fn clock(ctx: &Context, msg: &Message, args: String) {
.channel_id .channel_id
.say( .say(
&ctx, &ctx,
user_data.response(&pool, "clock/time").await.replacen( clock_display.replacen("{}", &now.format("%H:%M:%S").to_string(), 1),
"{}",
&now.format("%H:%M:%S").to_string(),
1,
),
) )
.await; .await;
} }

View File

@ -19,6 +19,7 @@ use crate::{
FrameworkCtx, SQLPool, FrameworkCtx, SQLPool,
}; };
use crate::language_manager::LanguageManager;
use serenity::model::id::ChannelId; use serenity::model::id::ChannelId;
use std::iter; use std::iter;
@ -27,13 +28,19 @@ use std::iter;
#[permission_level(Restricted)] #[permission_level(Restricted)]
#[can_blacklist(false)] #[can_blacklist(false)]
async fn blacklist(ctx: &Context, msg: &Message, args: String) { async fn blacklist(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -64,30 +71,24 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) {
if local { if local {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "blacklist/added").await) .say(&ctx, lm.get(&user_data.language, "blacklist/added"))
.await; .await;
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(&ctx, lm.get(&user_data.language, "blacklist/added_from"))
&ctx,
user_data.response(&pool, "blacklist/added_from").await,
)
.await; .await;
} }
} else { } else {
if local { if local {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "blacklist/removed").await) .say(&ctx, lm.get(&user_data.language, "blacklist/removed"))
.await; .await;
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(&ctx, lm.get(&user_data.language, "blacklist/removed_from"))
&ctx,
user_data.response(&pool, "blacklist/removed_from").await,
)
.await; .await;
} }
} }
@ -95,13 +96,19 @@ async fn blacklist(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
async fn timezone(ctx: &Context, msg: &Message, args: String) { async fn timezone(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
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();
@ -113,9 +120,8 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
let now = Utc::now().with_timezone(&user_data.timezone()); let now = Utc::now().with_timezone(&user_data.timezone());
let content = user_data let content = lm
.response(&pool, "timezone/set_p") .get(&user_data.language, "timezone/set_p")
.await
.replacen("{timezone}", &user_data.timezone, 1) .replacen("{timezone}", &user_data.timezone, 1)
.replacen("{time}", &now.format("%H:%M").to_string(), 1); .replacen("{time}", &now.format("%H:%M").to_string(), 1);
@ -125,17 +131,13 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
Err(_) => { Err(_) => {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(&ctx, lm.get(&user_data.language, "timezone/no_timezone"))
&ctx,
user_data.response(&pool, "timezone/no_timezone").await,
)
.await; .await;
} }
} }
} else { } else {
let content = user_data let content = lm
.response(&pool, "timezone/no_argument") .get(&user_data.language, "timezone/no_argument")
.await
.replace( .replace(
"{prefix}", "{prefix}",
&GuildData::prefix_from_id(msg.guild_id, &pool).await, &GuildData::prefix_from_id(msg.guild_id, &pool).await,
@ -148,57 +150,43 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
async fn language(ctx: &Context, msg: &Message, args: String) { async fn language(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
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 sqlx::query!( match lm.get_language(&args) {
" Some(row) => {
SELECT code FROM languages WHERE code = ? OR name = ? user_data.language = row.to_string();
",
args,
args
)
.fetch_one(&pool)
.await
{
Ok(row) => {
user_data.language = row.code;
user_data.commit_changes(&pool).await; user_data.commit_changes(&pool).await;
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "lang/set_p").await) .say(&ctx, lm.get(&user_data.language, "lang/set_p"))
.await; .await;
} }
Err(_) => { None => {
let language_codes = sqlx::query!("SELECT name, code FROM languages") let language_codes = lm
.fetch_all(&pool) .all_languages()
.await .map(|(k, v)| format!("{} ({})", v.to_title_case(), k.to_uppercase()))
.unwrap()
.iter()
.map(|language| {
format!(
"{} ({})",
language.name.to_title_case(),
language.code.to_uppercase()
)
})
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"); .join("\n");
let content = let content =
user_data lm.get(&user_data.language, "lang/invalid")
.response(&pool, "lang/invalid")
.await
.replacen("{}", &language_codes, 1); .replacen("{}", &language_codes, 1);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
@ -210,13 +198,19 @@ SELECT code FROM languages WHERE code = ? OR name = ?
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn prefix(ctx: &Context, msg: &Message, args: String) { async fn prefix(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let mut guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
.await .await
@ -226,18 +220,18 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) {
if args.len() > 5 { if args.len() > 5 {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "prefix/too_long").await) .say(&ctx, lm.get(&user_data.language, "prefix/too_long"))
.await; .await;
} else if args.is_empty() { } else if args.is_empty() {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "prefix/no_argument").await) .say(&ctx, lm.get(&user_data.language, "prefix/no_argument"))
.await; .await;
} else { } else {
guild_data.prefix = args; guild_data.prefix = args;
guild_data.commit_changes(&pool).await; guild_data.commit_changes(&pool).await;
let content = user_data.response(&pool, "prefix/success").await.replacen( let content = lm.get(&user_data.language, "prefix/success").replacen(
"{prefix}", "{prefix}",
&guild_data.prefix, &guild_data.prefix,
1, 1,
@ -251,13 +245,19 @@ async fn prefix(ctx: &Context, msg: &Message, args: String) {
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn restrict(ctx: &Context, msg: &Message, args: String) { async fn restrict(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool) let guild_data = GuildData::from_guild(msg.guild(&ctx).await.unwrap(), &pool)
@ -292,7 +292,7 @@ DELETE FROM command_restrictions WHERE role_id = (SELECT id FROM roles WHERE rol
if commands.is_empty() { if commands.is_empty() {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "restrict/disabled").await) .say(&ctx, lm.get(&user_data.language, "restrict/disabled"))
.await; .await;
} else { } else {
let _ = sqlx::query!( let _ = sqlx::query!(
@ -317,10 +317,11 @@ INSERT INTO command_restrictions (role_id, command) VALUES ((SELECT id FROM role
if res.is_err() { if res.is_err() {
println!("{:?}", res); println!("{:?}", res);
let content = user_data let content = lm.get(&user_data.language, "restrict/failure").replacen(
.response(&pool, "restrict/failure") "{command}",
.await &command,
.replacen("{command}", &command, 1); 1,
);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
} }
@ -328,7 +329,7 @@ INSERT INTO command_restrictions (role_id, command) VALUES ((SELECT id FROM role
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "restrict/enabled").await) .say(&ctx, lm.get(&user_data.language, "restrict/enabled"))
.await; .await;
} }
} }
@ -359,16 +360,15 @@ WHERE
.map(|row| format!("<@&{}> can use {}", row.role, row.command)) .map(|row| format!("<@&{}> can use {}", row.role, row.command))
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"); .join("\n");
let display = user_data let display =
.response(&pool, "restrict/allowed") lm.get(&user_data.language, "restrict/allowed")
.await .replacen("{}", &display_inner, 1);
.replacen("{}", &display_inner, 1);
let _ = msg.channel_id.say(&ctx, display).await; let _ = msg.channel_id.say(&ctx, display).await;
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "restrict/help").await) .say(&ctx, lm.get(&user_data.language, "restrict/help"))
.await; .await;
} }
} }
@ -377,13 +377,19 @@ WHERE
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn alias(ctx: &Context, msg: &Message, args: String) { async fn alias(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -437,16 +443,15 @@ DELETE FROM command_aliases WHERE name = ? AND guild_id = (SELECT id FROM guilds
.await .await
.unwrap(); .unwrap();
let content = user_data let content = lm
.response(&pool, "alias/removed") .get(&user_data.language, "alias/removed")
.await
.replace("{count}", &deleted_count.count.to_string()); .replace("{count}", &deleted_count.count.to_string());
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "alias/help").await) .say(&ctx, lm.get(&user_data.language, "alias/help"))
.await; .await;
} }
} }
@ -470,9 +475,8 @@ UPDATE command_aliases SET command = ? WHERE guild_id = (SELECT id FROM guilds W
.unwrap(); .unwrap();
} }
let content = user_data let content = lm
.response(&pool, "alias/created") .get(&user_data.language, "alias/created")
.await
.replace("{name}", name); .replace("{name}", name);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
@ -495,7 +499,7 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER
}, },
Err(_) => { Err(_) => {
let content = user_data.response(&pool, "alias/not_found").await.replace("{name}", name); let content = lm.get(&user_data.language, "alias/not_found").replace("{name}", name);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
}, },
@ -505,9 +509,8 @@ SELECT command FROM command_aliases WHERE guild_id = (SELECT id FROM guilds WHER
} }
} else { } else {
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
let content = user_data let content = lm
.response(&pool, "alias/help") .get(&user_data.language, "alias/help")
.await
.replace("{prefix}", &prefix); .replace("{prefix}", &prefix);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;

View File

@ -50,6 +50,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };
use crate::language_manager::LanguageManager;
use regex::RegexBuilder; use regex::RegexBuilder;
fn shorthand_displacement(seconds: u64) -> String { fn shorthand_displacement(seconds: u64) -> String {
@ -113,13 +114,19 @@ async fn create_webhook(
#[supports_dm(false)] #[supports_dm(false)]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn pause(ctx: &Context, msg: &Message, args: String) { async fn pause(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool)
@ -135,19 +142,16 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
if channel.paused { if channel.paused {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(&ctx, lm.get(&user_data.language, "pause/paused_indefinite"))
&ctx,
user_data.response(&pool, "pause/paused_indefinite").await,
)
.await; .await;
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "pause/unpaused").await) .say(&ctx, lm.get(&user_data.language, "pause/unpaused"))
.await; .await;
} }
} else { } else {
let parser = TimeParser::new(args, user_data.timezone.parse().unwrap()); let parser = TimeParser::new(args, user_data.timezone());
let pause_until = parser.timestamp(); let pause_until = parser.timestamp();
match pause_until { match pause_until {
@ -159,17 +163,14 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
channel.commit_changes(&pool).await; channel.commit_changes(&pool).await;
let content = user_data let content = lm.get(&user_data.language, "pause/paused_until").replace(
.response(&pool, "pause/paused_until") "{}",
.await &user_data
.replace( .timezone()
"{}", .timestamp(timestamp, 0)
&user_data .format("%Y-%m-%d %H:%M:%S")
.timezone() .to_string(),
.timestamp(timestamp, 0) );
.format("%Y-%m-%d %H:%M:%S")
.to_string(),
);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
} }
@ -177,7 +178,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
Err(_) => { Err(_) => {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "pause/invalid_time").await) .say(&ctx, lm.get(&user_data.language, "pause/invalid_time"))
.await; .await;
} }
} }
@ -187,20 +188,26 @@ async fn pause(ctx: &Context, msg: &Message, args: String) {
#[command] #[command]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn offset(ctx: &Context, msg: &Message, args: String) { async fn offset(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
if args.is_empty() { if args.is_empty() {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "offset/help").await) .say(&ctx, lm.get(&user_data.language, "offset/help"))
.await; .await;
} else { } else {
let parser = TimeParser::new(args, user_data.timezone()); let parser = TimeParser::new(args, user_data.timezone());
@ -237,7 +244,7 @@ UPDATE reminders SET `time` = `time` + ? WHERE reminders.channel_id = ?
.unwrap(); .unwrap();
} }
let response = user_data.response(&pool, "offset/success").await.replacen( let response = lm.get(&user_data.language, "offset/success").replacen(
"{}", "{}",
&displacement.to_string(), &displacement.to_string(),
1, 1,
@ -247,7 +254,7 @@ UPDATE reminders SET `time` = `time` + ? WHERE reminders.channel_id = ?
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "offset/invalid_time").await) .say(&ctx, lm.get(&user_data.language, "offset/invalid_time"))
.await; .await;
} }
} }
@ -256,13 +263,19 @@ UPDATE reminders SET `time` = `time` + ? WHERE reminders.channel_id = ?
#[command] #[command]
#[permission_level(Restricted)] #[permission_level(Restricted)]
async fn nudge(ctx: &Context, msg: &Message, args: String) { async fn nudge(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool) let mut channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), &pool)
@ -270,9 +283,8 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) {
.unwrap(); .unwrap();
if args.is_empty() { if args.is_empty() {
let content = user_data let content = lm
.response(&pool, "nudge/no_argument") .get(&user_data.language, "nudge/no_argument")
.await
.replace("{nudge}", &format!("{}s", &channel.nudge.to_string())); .replace("{nudge}", &format!("{}s", &channel.nudge.to_string()));
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
@ -285,14 +297,14 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) {
if displacement < i16::MIN as i64 || displacement > i16::MAX as i64 { if displacement < i16::MIN as i64 || displacement > i16::MAX as i64 {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "nudge/invalid_time").await) .say(&ctx, lm.get(&user_data.language, "nudge/invalid_time"))
.await; .await;
} else { } else {
channel.nudge = displacement as i16; channel.nudge = displacement as i16;
channel.commit_changes(&pool).await; channel.commit_changes(&pool).await;
let response = user_data.response(&pool, "nudge/success").await.replacen( let response = lm.get(&user_data.language, "nudge/success").replacen(
"{}", "{}",
&displacement.to_string(), &displacement.to_string(),
1, 1,
@ -305,7 +317,7 @@ async fn nudge(ctx: &Context, msg: &Message, args: String) {
Err(_) => { Err(_) => {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "nudge/invalid_time").await) .say(&ctx, lm.get(&user_data.language, "nudge/invalid_time"))
.await; .await;
} }
} }
@ -399,13 +411,19 @@ impl LookReminder {
#[command("look")] #[command("look")]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn look(ctx: &Context, msg: &Message, args: String) { async fn look(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -531,10 +549,10 @@ LIMIT
if reminders.is_empty() { if reminders.is_empty() {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "look/no_reminders").await) .say(&ctx, lm.get(&user_data.language, "look/no_reminders"))
.await; .await;
} else { } else {
let inter = user_data.response(&pool, "look/inter").await; let inter = lm.get(&user_data.language, "look/inter");
let display = reminders.iter().map(|reminder| { let display = reminders.iter().map(|reminder| {
let time_display = match flags.time_display { let time_display = match flags.time_display {
@ -568,19 +586,25 @@ LIMIT
#[command("del")] #[command("del")]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn delete(ctx: &Context, msg: &Message, _args: String) { async fn delete(ctx: &Context, msg: &Message, _args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "del/listing").await) .say(&ctx, lm.get(&user_data.language, "del/listing"))
.await; .await;
let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) { let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) {
@ -658,7 +682,7 @@ WHERE
let _ = msg.channel_id.say_lines(&ctx, enumerated_reminders).await; let _ = msg.channel_id.say_lines(&ctx, enumerated_reminders).await;
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "del/listed").await) .say(&ctx, lm.get(&user_data.language, "del/listed"))
.await; .await;
let reply = msg let reply = msg
@ -721,7 +745,7 @@ INSERT INTO events (event_name, bulk_count, guild_id, user_id) VALUES ('delete',
.await; .await;
} }
let content = user_data.response(&pool, "del/count").await.replacen( let content = lm.get(&user_data.language, "del/count").replacen(
"{}", "{}",
&count_row.count.to_string(), &count_row.count.to_string(),
1, 1,
@ -729,9 +753,8 @@ INSERT INTO events (event_name, bulk_count, guild_id, user_id) VALUES ('delete',
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
} else { } else {
let content = user_data let content = lm
.response(&pool, "del/count") .get(&user_data.language, "del/count")
.await
.replacen("{}", "0", 1); .replacen("{}", "0", 1);
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
@ -757,15 +780,21 @@ async fn timer(ctx: &Context, msg: &Message, args: String) {
format!("{:02}:{:02}:{:02}", hours, minutes, seconds) format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
} }
let pool = ctx let pool;
.data let lm;
.read()
.await
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); {
let data = ctx.data.read().await;
pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let language = UserData::language_of(&msg.author, &pool).await;
let mut args_iter = args.splitn(2, ' '); let mut args_iter = args.splitn(2, ' ');
@ -796,7 +825,7 @@ async fn timer(ctx: &Context, msg: &Message, args: String) {
if count >= 25 { if count >= 25 {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "timer/limit").await) .say(&ctx, lm.get(&language, "timer/limit"))
.await; .await;
} else { } else {
let name = args_iter let name = args_iter
@ -808,7 +837,7 @@ async fn timer(ctx: &Context, msg: &Message, args: String) {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "timer/success").await) .say(&ctx, lm.get(&language, "timer/success"))
.await; .await;
} }
} }
@ -839,18 +868,18 @@ DELETE FROM timers WHERE owner = ? AND name = ?
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "timer/deleted").await) .say(&ctx, lm.get(&language, "timer/deleted"))
.await; .await;
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "timer/not_found").await) .say(&ctx, lm.get(&language, "timer/not_found"))
.await; .await;
} }
} else { } else {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "timer/help").await) .say(&ctx, lm.get(&language, "timer/help"))
.await; .await;
} }
} }
@ -858,7 +887,7 @@ DELETE FROM timers WHERE owner = ? AND name = ?
_ => { _ => {
let _ = msg let _ = msg
.channel_id .channel_id
.say(&ctx, user_data.response(&pool, "timer/help").await) .say(&ctx, lm.get(&language, "timer/help"))
.await; .await;
} }
} }
@ -1026,13 +1055,19 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
} }
} }
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
@ -1092,9 +1127,8 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
.flatten() .flatten()
.unwrap_or(0) as u64; .unwrap_or(0) as u64;
let str_response = user_data let str_response = lm
.response(&pool, &response.to_response()) .get(&user_data.language, &response.to_response())
.await
.replace( .replace(
"{prefix}", "{prefix}",
&GuildData::prefix_from_id(msg.guild_id, &pool).await, &GuildData::prefix_from_id(msg.guild_id, &pool).await,
@ -1110,13 +1144,19 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
#[command("natural")] #[command("natural")]
#[permission_level(Managed)] #[permission_level(Managed)]
async fn natural(ctx: &Context, msg: &Message, args: String) { async fn natural(ctx: &Context, msg: &Message, args: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let now = SystemTime::now(); let now = SystemTime::now();
let since_epoch = now let since_epoch = now
@ -1125,9 +1165,9 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let send_str = user_data.response(&pool, "natural/send").await; let send_str = lm.get(&user_data.language, "natural/send");
let to_str = user_data.response(&pool, "natural/to").await; let to_str = lm.get(&user_data.language, "natural/to");
let every_str = user_data.response(&pool, "natural/every").await; let every_str = lm.get(&user_data.language, "natural/every");
let mut args_iter = args.splitn(2, &send_str); let mut args_iter = args.splitn(2, &send_str);
@ -1241,9 +1281,8 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
let offset = timestamp as u64 - since_epoch.as_secs(); let offset = timestamp as u64 - since_epoch.as_secs();
let str_response = user_data let str_response = lm
.response(&pool, &res.to_response_natural()) .get(&user_data.language, &res.to_response_natural())
.await
.replace( .replace(
"{prefix}", "{prefix}",
&GuildData::prefix_from_id(msg.guild_id, &pool).await, &GuildData::prefix_from_id(msg.guild_id, &pool).await,
@ -1275,9 +1314,8 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
} }
} }
let content = user_data let content = lm
.response(&pool, "natural/bulk_set") .get(&user_data.language, "natural/bulk_set")
.await
.replace("{}", &ok_count.to_string()); .replace("{}", &ok_count.to_string());
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
@ -1291,9 +1329,8 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
} else { } else {
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
let resp = user_data let resp = lm
.response(&pool, "natural/no_argument") .get(&user_data.language, "natural/no_argument")
.await
.replace("{prefix}", &prefix); .replace("{prefix}", &prefix);
let _ = msg let _ = msg

View File

@ -18,6 +18,7 @@ use crate::{
use sqlx::MySqlPool; use sqlx::MySqlPool;
use std::convert::TryFrom; use std::convert::TryFrom;
use crate::language_manager::LanguageManager;
use async_trait::async_trait; use async_trait::async_trait;
#[derive(Debug)] #[derive(Debug)]
@ -235,13 +236,19 @@ DELETE FROM todos WHERE user_id = (SELECT id FROM users WHERE user = ?) AND guil
} }
async fn execute(&self, ctx: &Context, msg: &Message, subcommand: SubCommand, extra: String) { async fn execute(&self, ctx: &Context, msg: &Message, subcommand: SubCommand, extra: String) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
@ -279,9 +286,8 @@ DELETE FROM todos WHERE user_id = (SELECT id FROM users WHERE user = ?) AND guil
} }
SubCommand::Add => { SubCommand::Add => {
let content = user_data let content = lm
.response(&pool, "todo/added") .get(&user_data.language, "todo/added")
.await
.replacen("{name}", &extra, 1); .replacen("{name}", &extra, 1);
self.add(extra, pool).await.unwrap(); self.add(extra, pool).await.unwrap();
@ -290,35 +296,35 @@ DELETE FROM todos WHERE user_id = (SELECT id FROM users WHERE user = ?) AND guil
} }
SubCommand::Remove => { SubCommand::Remove => {
let _ = if let Ok(num) = extra.parse::<usize>() { if let Ok(num) = extra.parse::<usize>() {
if let Ok(todo) = self.remove(num - 1, &pool).await { if let Ok(todo) = self.remove(num - 1, &pool).await {
let content = user_data.response(&pool, "todo/removed").await.replacen( let content = lm.get(&user_data.language, "todo/removed").replacen(
"{}", "{}",
&todo.value, &todo.value,
1, 1,
); );
msg.channel_id.say(&ctx, content) let _ = msg.channel_id.say(&ctx, content).await;
} else { } else {
msg.channel_id let _ = msg
.say(&ctx, user_data.response(&pool, "todo/error_index").await) .channel_id
.say(&ctx, lm.get(&user_data.language, "todo/error_index"))
.await;
} }
} else { } else {
let content = user_data let content = lm
.response(&pool, "todo/error_value") .get(&user_data.language, "todo/error_value")
.await
.replacen("{prefix}", &prefix, 1) .replacen("{prefix}", &prefix, 1)
.replacen("{command}", &self.command(Some(subcommand)), 1); .replacen("{command}", &self.command(Some(subcommand)), 1);
msg.channel_id.say(&ctx, content) let _ = msg.channel_id.say(&ctx, content).await;
} }
.await;
} }
SubCommand::Clear => { SubCommand::Clear => {
self.clear(&pool).await.unwrap(); self.clear(&pool).await.unwrap();
let content = user_data.response(&pool, "todo/cleared").await; let content = lm.get(&user_data.language, "todo/cleared");
let _ = msg.channel_id.say(&ctx, content).await; let _ = msg.channel_id.say(&ctx, content).await;
} }
@ -435,20 +441,25 @@ async fn todo_guild(ctx: &Context, msg: &Message, args: String) {
} }
async fn show_help(ctx: &Context, msg: &Message, target: Option<TodoTarget>) { async fn show_help(ctx: &Context, msg: &Message, target: Option<TodoTarget>) {
let pool = ctx let pool;
.data let lm;
.read()
.await {
.get::<SQLPool>() let data = ctx.data.read().await;
.cloned()
.expect("Could not get SQLPool from data"); pool = data
.get::<SQLPool>()
.cloned()
.expect("Could not get SQLPool from data");
lm = data.get::<LanguageManager>().cloned().unwrap();
}
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await; let prefix = GuildData::prefix_from_id(msg.guild_id, &pool).await;
let content = user_data let content = lm
.response(&pool, "todo/help") .get(&user_data.language, "todo/help")
.await
.replace("{prefix}", &prefix) .replace("{prefix}", &prefix)
.replace( .replace(
"{command}", "{command}",

View File

@ -20,6 +20,7 @@ use regex::{Match, Regex, RegexBuilder};
use std::{collections::HashMap, fmt}; use std::{collections::HashMap, fmt};
use crate::language_manager::LanguageManager;
use crate::models::{GuildData, UserData}; use crate::models::{GuildData, UserData};
use crate::{models::ChannelData, SQLPool}; use crate::{models::ChannelData, SQLPool};
@ -354,16 +355,16 @@ impl Framework for RegexFramework {
{ {
if let Some(full_match) = self.command_matcher.captures(&msg.content) { if let Some(full_match) = self.command_matcher.captures(&msg.content) {
if check_prefix(&ctx, &guild, full_match.name("prefix")).await { if check_prefix(&ctx, &guild, full_match.name("prefix")).await {
let pool = ctx let data = ctx.data.read().await;
.data
.read() let pool = data
.await
.get::<SQLPool>() .get::<SQLPool>()
.cloned() .cloned()
.expect("Could not get SQLPool from data"); .expect("Could not get SQLPool from data");
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap(); let lm = data.get::<LanguageManager>().unwrap();
let guild_data = GuildData::from_guild(guild.clone(), &pool).await.unwrap();
let language = UserData::language_of(&msg.author, &pool).await;
match check_self_permissions(&ctx, &guild, &channel).await { match check_self_permissions(&ctx, &guild, &channel).await {
Ok(perms) => match perms { Ok(perms) => match perms {
@ -380,9 +381,6 @@ impl Framework for RegexFramework {
.await .await
.unwrap(); .unwrap();
// required due to a small bug resulting in some channels being detached from their guild ids
channel_data.update_guild_id(guild_data.id, &pool).await;
if !command.can_blacklist || !channel_data.blacklisted { if !command.can_blacklist || !channel_data.blacklisted {
let args = full_match let args = full_match
.name("args") .name("args")
@ -398,29 +396,18 @@ impl Framework for RegexFramework {
{ {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(&ctx, lm.get(&language, "no_perms_restricted"))
&ctx,
user_data
.response(&pool, "no_perms_restricted")
.await,
)
.await; .await;
} else if command.required_perms == PermissionLevel::Managed { } else if command.required_perms == PermissionLevel::Managed {
let _ = msg let _ = msg
.channel_id .channel_id
.say( .say(
&ctx, &ctx,
user_data lm.get(&language, "no_perms_managed").replace(
.response(&pool, "no_perms_managed") "{prefix}",
.await &GuildData::prefix_from_id(msg.guild_id, &pool)
.replace(
"{prefix}",
&GuildData::prefix_from_id(
msg.guild_id,
&pool,
)
.await, .await,
), ),
) )
.await; .await;
} }
@ -428,9 +415,8 @@ impl Framework for RegexFramework {
} }
PermissionCheck::Basic(manage_webhooks, embed_links) => { PermissionCheck::Basic(manage_webhooks, embed_links) => {
let response = user_data let response = lm
.response(&pool, "no_perms_general") .get(&language, "no_perms_general")
.await
.replace( .replace(
"{manage_webhooks}", "{manage_webhooks}",
if manage_webhooks { "" } else { "" }, if manage_webhooks { "" } else { "" },

View File

@ -1,12 +1,10 @@
use std::collections::HashMap;
use serde::Deserialize; use serde::Deserialize;
use serde_json::from_reader; use serde_json::from_str;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use std::error::Error;
use std::fs::File; use std::{collections::HashMap, error::Error, sync::Arc};
use std::io::BufReader;
use std::path::Path; use crate::consts::LOCAL_LANGUAGE;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct LanguageManager { pub struct LanguageManager {
@ -15,34 +13,41 @@ pub struct LanguageManager {
} }
impl LanguageManager { impl LanguageManager {
pub(crate) fn from_compiled<P>(path: P) -> Result<Self, Box<dyn Error + Send + Sync>> pub fn from_compiled(content: &'static str) -> Result<Self, Box<dyn Error + Send + Sync>> {
where let new: Self = from_str(content.as_ref())?;
P: AsRef<Path>,
{
let file = File::open(path)?;
let reader = BufReader::new(file);
let new: Self = from_reader(reader)?;
Ok(new) Ok(new)
} }
pub(crate) fn get(&self, language: &str, name: &'static str) -> &str { pub fn get(&self, language: &str, name: &str) -> &str {
self.strings self.strings
.get(language) .get(language)
.map(|sm| sm.get(name)) .map(|sm| sm.get(name))
.expect(&format!(r#"Language does not exist: "{}""#, language)) .expect(&format!(r#"Language does not exist: "{}""#, language))
.expect(&format!(r#"String does not exist: "{}""#, name)) .unwrap_or_else(|| {
self.strings
.get(&*LOCAL_LANGUAGE)
.map(|sm| {
sm.get(name)
.expect(&format!(r#"String does not exist: "{}""#, name))
})
.expect("LOCAL_LANGUAGE is not available")
})
} }
fn all_languages(&self) -> Vec<(&str, &str)> { pub fn get_language(&self, language: &str) -> Option<&str> {
self.languages self.languages
.iter() .iter()
.map(|(k, v)| (k.as_str(), v.as_str())) .filter(|(k, v)| k.to_lowercase() == language || v.to_lowercase() == language)
.collect() .map(|(k, _)| k.as_str())
.next()
}
pub fn all_languages(&self) -> impl Iterator<Item = (&str, &str)> {
self.languages.iter().map(|(k, v)| (k.as_str(), v.as_str()))
} }
} }
impl TypeMapKey for LanguageManager { impl TypeMapKey for LanguageManager {
type Value = Self; type Value = Arc<Self>;
} }

View File

@ -231,14 +231,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.await .await
.unwrap(); .unwrap();
let language_manager = LanguageManager::from_compiled("out.json")?; let language_manager = LanguageManager::from_compiled(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/",
env!("STRINGS_FILE")
)))?;
let mut data = client.data.write().await; let mut data = client.data.write().await;
data.insert::<SQLPool>(pool); data.insert::<SQLPool>(pool);
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new())); data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
data.insert::<FrameworkCtx>(framework_arc); data.insert::<FrameworkCtx>(framework_arc);
data.insert::<LanguageManager>(language_manager) data.insert::<LanguageManager>(Arc::new(language_manager))
} }
if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| { if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| {

View File

@ -158,19 +158,6 @@ SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_u
} }
} }
pub async fn update_guild_id(&self, id: u32, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE channels SET guild_id = ? WHERE id = ?
",
id,
self.id
)
.execute(pool)
.await
.unwrap();
}
pub async fn commit_changes(&self, pool: &MySqlPool) { pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!( sqlx::query!(
" "
@ -191,7 +178,7 @@ pub struct UserData {
} }
impl UserData { impl UserData {
pub async fn language_of(user: &User, ctx: impl CacheHttp, pool: &MySqlPool) -> String { pub async fn language_of(user: &User, pool: &MySqlPool) -> String {
let user_id = user.id.as_u64().to_owned(); let user_id = user.id.as_u64().to_owned();
match sqlx::query!( match sqlx::query!(
@ -285,10 +272,6 @@ UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
.unwrap(); .unwrap();
} }
pub async fn response(&self, _pool: &MySqlPool, _name: &str) -> String {
unimplemented!()
}
pub fn timezone(&self) -> Tz { pub fn timezone(&self) -> Tz {
self.timezone.parse().unwrap() self.timezone.parse().unwrap()
} }