bumped sqlx -> 0.4, bumped reminder_rs -> 1.2.3, added a more descriptive message for not enough permissions. removed STRINGS_TABLE environment variable

This commit is contained in:
jellywx 2020-11-20 16:37:39 +00:00
parent 19754d3bcc
commit 1927d381ab
7 changed files with 666 additions and 329 deletions

899
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.2.2" version = "1.2.3"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"
@ -8,7 +8,7 @@ edition = "2018"
dotenv = "0.15" dotenv = "0.15"
tokio = { version = "0.2", features = ["process"] } tokio = { version = "0.2", features = ["process"] }
reqwest = { version = "0.10", features = ["rustls-tls"] } reqwest = { version = "0.10", features = ["rustls-tls"] }
sqlx = { version = "0.3", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal", "chrono"] } sqlx = { version = "0.4", default-features = false, features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal", "chrono"] }
regex = "1.3" regex = "1.3"
async-trait = "0.1" async-trait = "0.1"
log = "0.4" log = "0.4"

View File

@ -492,7 +492,7 @@ LIMIT
.await .await
} }
} else { } else {
sqlx::query_as!( sqlx::query_as_unchecked!(
LookReminder, LookReminder,
" "
SELECT SELECT
@ -584,7 +584,7 @@ async fn delete(ctx: &Context, msg: &Message, _args: String) {
.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()) {
sqlx::query_as!( sqlx::query_as_unchecked!(
LookReminder, LookReminder,
" "
SELECT SELECT
@ -1303,7 +1303,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
} }
} }
async fn create_reminder<T: TryInto<i64>, S: ToString + Type<MySql> + Encode<MySql>>( async fn create_reminder<'a, T: TryInto<i64>, S: ToString + Type<MySql> + Encode<'a, MySql>>(
ctx: impl CacheHttp, ctx: impl CacheHttp,
pool: &MySqlPool, pool: &MySqlPool,
user_id: u64, user_id: u64,

View File

@ -46,8 +46,6 @@ lazy_static! {
env::var("LOCAL_LANGUAGE").unwrap_or_else(|_| "EN".to_string()); env::var("LOCAL_LANGUAGE").unwrap_or_else(|_| "EN".to_string());
pub static ref PYTHON_LOCATION: String = pub static ref PYTHON_LOCATION: String =
env::var("PYTHON_LOCATION").unwrap_or_else(|_| "venv/bin/python3".to_string()); env::var("PYTHON_LOCATION").unwrap_or_else(|_| "venv/bin/python3".to_string());
pub static ref STRINGS_TABLE: String =
env::var("STRINGS_TABLE").unwrap_or_else(|_| "strings".to_string());
pub static ref DEFAULT_PREFIX: String = pub static ref DEFAULT_PREFIX: String =
env::var("DEFAULT_PREFIX").unwrap_or_else(|_| "$".to_string()); env::var("DEFAULT_PREFIX").unwrap_or_else(|_| "$".to_string());
pub static ref THEME_COLOR: u32 = env::var("THEME_COLOR").map_or( pub static ref THEME_COLOR: u32 = env::var("THEME_COLOR").map_or(

View File

@ -289,9 +289,9 @@ impl RegexFramework {
} }
enum PermissionCheck { enum PermissionCheck {
None, // No permissions None, // No permissions
Basic, // Send + Embed permissions (sufficient to reply) Basic(bool, bool), // Send + Embed permissions (sufficient to reply)
All, // Above + Manage Webhooks (sufficient to operate) All, // Above + Manage Webhooks (sufficient to operate)
} }
#[async_trait] #[async_trait]
@ -305,15 +305,18 @@ impl Framework for RegexFramework {
let user_id = ctx.cache.current_user_id().await; let user_id = ctx.cache.current_user_id().await;
let guild_perms = guild.member_permissions(&ctx, user_id).await?; let guild_perms = guild.member_permissions(&ctx, user_id).await?;
let perms = channel.permissions_for_user(ctx, user_id).await?; let channel_perms = channel.permissions_for_user(ctx, user_id).await?;
let basic_perms = perms.send_messages(); let basic_perms = channel_perms.send_messages();
Ok( Ok(
if basic_perms && guild_perms.manage_webhooks() && perms.embed_links() { if basic_perms && guild_perms.manage_webhooks() && channel_perms.embed_links() {
PermissionCheck::All PermissionCheck::All
} else if basic_perms { } else if basic_perms {
PermissionCheck::Basic PermissionCheck::Basic(
guild_perms.manage_webhooks(),
channel_perms.embed_links(),
)
} else { } else {
PermissionCheck::None PermissionCheck::None
}, },
@ -424,11 +427,20 @@ impl Framework for RegexFramework {
} }
} }
PermissionCheck::Basic => { PermissionCheck::Basic(manage_webhooks, embed_links) => {
let _ = msg let response = user_data
.channel_id .response(&pool, "no_perms_general")
.say(&ctx, user_data.response(&pool, "no_perms_general").await) .await
.await; .replace(
"{manage_webhooks}",
if manage_webhooks { "" } else { "" },
)
.replace(
"{embed_links}",
if embed_links { "" } else { "" },
);
let _ = msg.channel_id.say(&ctx, response).await;
} }
PermissionCheck::None => { PermissionCheck::None => {

View File

@ -23,10 +23,7 @@ use serenity::{
utils::shard_id, utils::shard_id,
}; };
use sqlx::{ use sqlx::mysql::MySqlPool;
mysql::{MySqlConnection, MySqlPool},
Pool,
};
use dotenv::dotenv; use dotenv::dotenv;
@ -45,7 +42,7 @@ use log::info;
struct SQLPool; struct SQLPool;
impl TypeMapKey for SQLPool { impl TypeMapKey for SQLPool {
type Value = Pool<MySqlConnection>; type Value = MySqlPool;
} }
struct ReqwestClient; struct ReqwestClient;
@ -226,7 +223,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.expect("Error occurred creating client"); .expect("Error occurred creating client");
{ {
let pool = MySqlPool::new( let pool = MySqlPool::connect(
&env::var("DATABASE_URL").expect("Missing DATABASE_URL from environment"), &env::var("DATABASE_URL").expect("Missing DATABASE_URL from environment"),
) )
.await .await

View File

@ -3,14 +3,14 @@ use serenity::{
model::{channel::Channel, guild::Guild, id::GuildId, user::User}, model::{channel::Channel, guild::Guild, id::GuildId, user::User},
}; };
use sqlx::{Cursor, MySqlPool, Row}; use sqlx::MySqlPool;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use chrono_tz::Tz; use chrono_tz::Tz;
use log::error; use log::error;
use crate::consts::{DEFAULT_PREFIX, LOCAL_LANGUAGE, LOCAL_TIMEZONE, STRINGS_TABLE}; use crate::consts::{DEFAULT_PREFIX, LOCAL_LANGUAGE, LOCAL_TIMEZONE};
pub struct GuildData { pub struct GuildData {
pub id: u32, pub id: u32,
@ -267,27 +267,24 @@ UPDATE users SET name = ?, language = ?, timezone = ? WHERE id = ?
} }
pub async fn response(&self, pool: &MySqlPool, name: &str) -> String { pub async fn response(&self, pool: &MySqlPool, name: &str) -> String {
let query_str = &format!( struct StringRow {
value: String,
}
sqlx::query_as!(
StringRow,
" "
SELECT value FROM {} WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ? SELECT value FROM strings WHERE (language = ? OR language = ?) AND name = ? ORDER BY language = ?
", ",
*STRINGS_TABLE self.language,
); &*LOCAL_LANGUAGE,
name,
let mut query = sqlx::query(&query_str) &*LOCAL_LANGUAGE
.bind(&self.language) )
.bind(&*LOCAL_LANGUAGE) .fetch_one(pool)
.bind(name) .await
.bind(&*LOCAL_LANGUAGE) .unwrap()
.fetch(pool); .value
let row = query
.next()
.await
.unwrap_or_else(|e| panic!("Database error: {:?}", e))
.unwrap_or_else(|| panic!("No string with that name: {}", name));
row.get::<String, &str>("value").clone()
} }
pub fn timezone(&self) -> Tz { pub fn timezone(&self) -> Tz {