guild data caching to stop querying db
This commit is contained in:
parent
0991d42525
commit
11f388c65a
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1840,6 +1840,7 @@ dependencies = [
|
|||||||
name = "soundfx-rs"
|
name = "soundfx-rs"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"dashmap",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
@ -16,6 +16,7 @@ songbird = { git = "https://github.com/serenity-rs/songbird", branch = "current"
|
|||||||
regex = "1.4"
|
regex = "1.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
dashmap = "4.0"
|
||||||
|
|
||||||
[dependencies.regex_command_attr]
|
[dependencies.regex_command_attr]
|
||||||
path = "./regex_command_attr"
|
path = "./regex_command_attr"
|
||||||
|
@ -19,7 +19,7 @@ use regex::{Match, Regex, RegexBuilder};
|
|||||||
|
|
||||||
use std::{collections::HashMap, fmt};
|
use std::{collections::HashMap, fmt};
|
||||||
|
|
||||||
use crate::{guild_data::GuildData, MySQL};
|
use crate::{guild_data::CtxGuildData, MySQL};
|
||||||
use serenity::framework::standard::{CommandResult, Delimiter};
|
use serenity::framework::standard::{CommandResult, Delimiter};
|
||||||
|
|
||||||
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, Args) -> BoxFuture<'fut, CommandResult>;
|
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, Args) -> BoxFuture<'fut, CommandResult>;
|
||||||
@ -252,24 +252,11 @@ impl Framework for RegexFramework {
|
|||||||
|
|
||||||
async fn check_prefix(ctx: &Context, guild: &Guild, prefix_opt: Option<Match<'_>>) -> bool {
|
async fn check_prefix(ctx: &Context, guild: &Guild, prefix_opt: Option<Match<'_>>) -> bool {
|
||||||
if let Some(prefix) = prefix_opt {
|
if let Some(prefix) = prefix_opt {
|
||||||
let pool = ctx
|
match ctx.get_from_id(guild.id).await {
|
||||||
.data
|
Ok(guild_data) => prefix.as_str() == guild_data.read().await.prefix,
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.get::<MySQL>()
|
|
||||||
.cloned()
|
|
||||||
.expect("Could not get SQLPool from data");
|
|
||||||
|
|
||||||
let guild_prefix = match GuildData::get_from_id(guild.clone(), pool.clone()).await {
|
Err(_) => prefix.as_str() == "?",
|
||||||
Some(guild_data) => guild_data.prefix,
|
}
|
||||||
|
|
||||||
None => {
|
|
||||||
GuildData::create_from_guild(guild, pool).await.unwrap();
|
|
||||||
String::from("?")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
guild_prefix.as_str() == prefix.as_str()
|
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
use serenity::model::guild::Guild;
|
use crate::{GuildDataCache, MySQL};
|
||||||
|
use serenity::{async_trait, model::id::GuildId, prelude::Context};
|
||||||
use sqlx::mysql::MySqlPool;
|
use sqlx::mysql::MySqlPool;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct GuildData {
|
pub struct GuildData {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub prefix: String,
|
pub prefix: String,
|
||||||
@ -8,8 +12,59 @@ pub struct GuildData {
|
|||||||
pub allow_greets: bool,
|
pub allow_greets: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait CtxGuildData {
|
||||||
|
async fn get_from_id<G: Into<GuildId> + Send + Sync>(
|
||||||
|
&self,
|
||||||
|
guild_id: G,
|
||||||
|
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl CtxGuildData for Context {
|
||||||
|
async fn get_from_id<G: Into<GuildId> + Send + Sync>(
|
||||||
|
&self,
|
||||||
|
guild_id: G,
|
||||||
|
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error> {
|
||||||
|
let guild_id = guild_id.into();
|
||||||
|
|
||||||
|
let guild_cache = self
|
||||||
|
.data
|
||||||
|
.read()
|
||||||
|
.await
|
||||||
|
.get::<GuildDataCache>()
|
||||||
|
.cloned()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let x = if let Some(guild_data) = guild_cache.get(&guild_id) {
|
||||||
|
Ok(guild_data.clone())
|
||||||
|
} else {
|
||||||
|
let pool = self.data.read().await.get::<MySQL>().cloned().unwrap();
|
||||||
|
|
||||||
|
match GuildData::get_from_id(guild_id, pool).await {
|
||||||
|
Ok(d) => {
|
||||||
|
let lock = Arc::new(RwLock::new(d));
|
||||||
|
|
||||||
|
guild_cache.insert(guild_id, lock.clone());
|
||||||
|
|
||||||
|
Ok(lock)
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl GuildData {
|
impl GuildData {
|
||||||
pub async fn get_from_id(guild: Guild, db_pool: MySqlPool) -> Option<GuildData> {
|
pub async fn get_from_id<G: Into<GuildId>>(
|
||||||
|
guild_id: G,
|
||||||
|
db_pool: MySqlPool,
|
||||||
|
) -> Result<GuildData, sqlx::Error> {
|
||||||
|
let guild_id = guild_id.into();
|
||||||
|
|
||||||
let guild_data = sqlx::query_as_unchecked!(
|
let guild_data = sqlx::query_as_unchecked!(
|
||||||
GuildData,
|
GuildData,
|
||||||
"
|
"
|
||||||
@ -17,35 +72,32 @@ SELECT id, prefix, volume, allow_greets
|
|||||||
FROM servers
|
FROM servers
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
",
|
",
|
||||||
guild.id.as_u64()
|
guild_id.as_u64()
|
||||||
)
|
)
|
||||||
.fetch_one(&db_pool)
|
.fetch_one(&db_pool)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match guild_data {
|
match guild_data {
|
||||||
Ok(g) => Some(g),
|
Err(sqlx::error::Error::RowNotFound) => {
|
||||||
|
Self::create_from_guild(guild_id, db_pool).await
|
||||||
Err(sqlx::Error::RowNotFound) => Self::create_from_guild(&guild, db_pool).await.ok(),
|
|
||||||
|
|
||||||
Err(e) => {
|
|
||||||
println!("{:?}", e);
|
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d => d,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_from_guild(
|
async fn create_from_guild<G: Into<GuildId>>(
|
||||||
guild: &Guild,
|
guild_id: G,
|
||||||
db_pool: MySqlPool,
|
db_pool: MySqlPool,
|
||||||
) -> Result<GuildData, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<GuildData, sqlx::Error> {
|
||||||
|
let guild_id = guild_id.into();
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"
|
"
|
||||||
INSERT INTO servers (id, name)
|
INSERT INTO servers (id)
|
||||||
VALUES (?, ?)
|
VALUES (?)
|
||||||
",
|
",
|
||||||
guild.id.as_u64(),
|
guild_id.as_u64()
|
||||||
guild.name
|
|
||||||
)
|
)
|
||||||
.execute(&db_pool)
|
.execute(&db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
@ -55,14 +107,14 @@ INSERT INTO servers (id, name)
|
|||||||
INSERT IGNORE INTO roles (guild_id, role)
|
INSERT IGNORE INTO roles (guild_id, role)
|
||||||
VALUES (?, ?)
|
VALUES (?, ?)
|
||||||
",
|
",
|
||||||
guild.id.as_u64(),
|
guild_id.as_u64(),
|
||||||
guild.id.as_u64()
|
guild_id.as_u64()
|
||||||
)
|
)
|
||||||
.execute(&db_pool)
|
.execute(&db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(GuildData {
|
Ok(GuildData {
|
||||||
id: guild.id.as_u64().to_owned(),
|
id: guild_id.as_u64().to_owned(),
|
||||||
prefix: String::from("?"),
|
prefix: String::from("?"),
|
||||||
volume: 100,
|
volume: 100,
|
||||||
allow_greets: true,
|
allow_greets: true,
|
||||||
|
153
src/main.rs
153
src/main.rs
@ -41,9 +41,10 @@ use sqlx::mysql::MySqlPool;
|
|||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
use crate::framework::RegexFramework;
|
use crate::{framework::RegexFramework, guild_data::CtxGuildData};
|
||||||
|
use dashmap::DashMap;
|
||||||
use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration};
|
use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration};
|
||||||
use tokio::sync::MutexGuard;
|
use tokio::sync::{MutexGuard, RwLock};
|
||||||
|
|
||||||
struct MySQL;
|
struct MySQL;
|
||||||
|
|
||||||
@ -63,6 +64,12 @@ impl TypeMapKey for AudioIndex {
|
|||||||
type Value = Arc<HashMap<String, String>>;
|
type Value = Arc<HashMap<String, String>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct GuildDataCache;
|
||||||
|
|
||||||
|
impl TypeMapKey for GuildDataCache {
|
||||||
|
type Value = Arc<DashMap<GuildId, Arc<RwLock<GuildData>>>>;
|
||||||
|
}
|
||||||
|
|
||||||
const THEME_COLOR: u32 = 0x00e0f3;
|
const THEME_COLOR: u32 = 0x00e0f3;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
@ -145,10 +152,20 @@ impl EventHandler for Handler {
|
|||||||
.cloned()
|
.cloned()
|
||||||
.expect("Could not get SQLPool from data");
|
.expect("Could not get SQLPool from data");
|
||||||
|
|
||||||
let guild_data_opt = GuildData::get_from_id(guild.clone(), pool.clone()).await;
|
let guild_data_opt = ctx.get_from_id(guild.id).await;
|
||||||
|
|
||||||
if let Some(guild_data) = guild_data_opt {
|
if let Ok(guild_data) = guild_data_opt {
|
||||||
if guild_data.allow_greets {
|
let volume;
|
||||||
|
let allowed_greets;
|
||||||
|
|
||||||
|
{
|
||||||
|
let read = guild_data.read().await;
|
||||||
|
|
||||||
|
volume = read.volume;
|
||||||
|
allowed_greets = read.allow_greets;
|
||||||
|
}
|
||||||
|
|
||||||
|
if allowed_greets {
|
||||||
let join_id_res = sqlx::query!(
|
let join_id_res = sqlx::query!(
|
||||||
"
|
"
|
||||||
SELECT join_sound_id
|
SELECT join_sound_id
|
||||||
@ -180,7 +197,7 @@ SELECT name, id, plays, public, server_id, uploader_id
|
|||||||
|
|
||||||
let _ = play_audio(
|
let _ = play_audio(
|
||||||
&mut sound,
|
&mut sound,
|
||||||
guild_data,
|
volume,
|
||||||
&mut handler.lock().await,
|
&mut handler.lock().await,
|
||||||
pool,
|
pool,
|
||||||
false,
|
false,
|
||||||
@ -197,7 +214,7 @@ SELECT name, id, plays, public, server_id, uploader_id
|
|||||||
|
|
||||||
async fn play_audio(
|
async fn play_audio(
|
||||||
sound: &mut Sound,
|
sound: &mut Sound,
|
||||||
guild: GuildData,
|
volume: u8,
|
||||||
call_handler: &mut MutexGuard<'_, Call>,
|
call_handler: &mut MutexGuard<'_, Call>,
|
||||||
mysql_pool: MySqlPool,
|
mysql_pool: MySqlPool,
|
||||||
loop_: bool,
|
loop_: bool,
|
||||||
@ -206,7 +223,7 @@ async fn play_audio(
|
|||||||
let (track, track_handler) =
|
let (track, track_handler) =
|
||||||
create_player(sound.store_sound_source(mysql_pool.clone()).await?.into());
|
create_player(sound.store_sound_source(mysql_pool.clone()).await?.into());
|
||||||
|
|
||||||
let _ = track_handler.set_volume(guild.volume as f32 / 100.0);
|
let _ = track_handler.set_volume(volume as f32 / 100.0);
|
||||||
|
|
||||||
if loop_ {
|
if loop_ {
|
||||||
let _ = track_handler.enable_loop();
|
let _ = track_handler.enable_loop();
|
||||||
@ -240,12 +257,31 @@ async fn join_channel(
|
|||||||
let call_opt = songbird.get(guild.id);
|
let call_opt = songbird.get(guild.id);
|
||||||
|
|
||||||
if let Some(call) = call_opt {
|
if let Some(call) = call_opt {
|
||||||
|
{
|
||||||
|
// set call to deafen
|
||||||
|
let _ = call.lock().await.deafen(true).await;
|
||||||
|
}
|
||||||
|
|
||||||
(call, Ok(()))
|
(call, Ok(()))
|
||||||
} else {
|
} else {
|
||||||
songbird.join(guild.id, channel_id).await
|
let (call, res) = songbird.join(guild.id, channel_id).await;
|
||||||
|
|
||||||
|
{
|
||||||
|
// set call to deafen
|
||||||
|
let _ = call.lock().await.deafen(true).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
(call, res)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
songbird.join(guild.id, channel_id).await
|
let (call, res) = songbird.join(guild.id, channel_id).await;
|
||||||
|
|
||||||
|
{
|
||||||
|
// set call to deafen
|
||||||
|
let _ = call.lock().await.deafen(true).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
(call, res)
|
||||||
};
|
};
|
||||||
|
|
||||||
(call, res)
|
(call, res)
|
||||||
@ -335,8 +371,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
let guild_data_cache = Arc::new(DashMap::new());
|
||||||
|
|
||||||
let mut data = client.data.write().await;
|
let mut data = client.data.write().await;
|
||||||
|
|
||||||
|
data.insert::<GuildDataCache>(guild_data_cache);
|
||||||
data.insert::<MySQL>(mysql_pool);
|
data.insert::<MySQL>(mysql_pool);
|
||||||
|
|
||||||
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
|
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
|
||||||
@ -408,11 +447,18 @@ async fn play_cmd(ctx: &Context, msg: &Message, args: Args, loop_: bool) -> Comm
|
|||||||
let (call_handler, _) =
|
let (call_handler, _) =
|
||||||
join_channel(ctx, guild.clone(), user_channel).await;
|
join_channel(ctx, guild.clone(), user_channel).await;
|
||||||
|
|
||||||
let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap();
|
let guild_data = ctx.get_from_id(guild).await.unwrap();
|
||||||
|
|
||||||
let mut lock = call_handler.lock().await;
|
let mut lock = call_handler.lock().await;
|
||||||
|
|
||||||
play_audio(sound, guild_data, &mut lock, pool, loop_).await?;
|
play_audio(
|
||||||
|
sound,
|
||||||
|
guild_data.read().await.volume,
|
||||||
|
&mut lock,
|
||||||
|
pool,
|
||||||
|
loop_,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
@ -474,13 +520,12 @@ async fn play_ambience(ctx: &Context, msg: &Message, args: Args) -> CommandResul
|
|||||||
Some(user_channel) => {
|
Some(user_channel) => {
|
||||||
let search_name = args.rest().to_lowercase();
|
let search_name = args.rest().to_lowercase();
|
||||||
let audio_index = ctx.data.read().await.get::<AudioIndex>().cloned().unwrap();
|
let audio_index = ctx.data.read().await.get::<AudioIndex>().cloned().unwrap();
|
||||||
let pool = ctx.data.read().await.get::<MySQL>().cloned().unwrap();
|
|
||||||
|
|
||||||
if let Some(filename) = audio_index.get(&search_name) {
|
if let Some(filename) = audio_index.get(&search_name) {
|
||||||
{
|
{
|
||||||
let (call_handler, _) = join_channel(ctx, guild.clone(), user_channel).await;
|
let (call_handler, _) = join_channel(ctx, guild.clone(), user_channel).await;
|
||||||
|
|
||||||
let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap();
|
let guild_data = ctx.get_from_id(guild).await.unwrap();
|
||||||
|
|
||||||
let mut lock = call_handler.lock().await;
|
let mut lock = call_handler.lock().await;
|
||||||
|
|
||||||
@ -495,7 +540,7 @@ async fn play_ambience(ctx: &Context, msg: &Message, args: Args) -> CommandResul
|
|||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = track_handler.set_volume(guild_data.volume as f32 / 100.0);
|
let _ = track_handler.set_volume(guild_data.read().await.volume as f32 / 100.0);
|
||||||
let _ = track_handler.add_event(
|
let _ = track_handler.add_event(
|
||||||
Event::Periodic(
|
Event::Periodic(
|
||||||
track_handler.metadata().duration.unwrap() - Duration::from_millis(500),
|
track_handler.metadata().duration.unwrap() - Duration::from_millis(500),
|
||||||
@ -604,14 +649,6 @@ There is a maximum sound limit per user. This can be removed by donating at http
|
|||||||
#[command]
|
#[command]
|
||||||
#[permission_level(Managed)]
|
#[permission_level(Managed)]
|
||||||
async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let guild = match msg.guild(&ctx.cache).await {
|
|
||||||
Some(guild) => guild,
|
|
||||||
|
|
||||||
None => {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let pool = ctx
|
let pool = ctx
|
||||||
.data
|
.data
|
||||||
.read()
|
.read()
|
||||||
@ -620,15 +657,15 @@ async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
.cloned()
|
.cloned()
|
||||||
.expect("Could not get SQLPool from data");
|
.expect("Could not get SQLPool from data");
|
||||||
|
|
||||||
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
|
let guild_data_opt = ctx.get_from_id(msg.guild_id.unwrap()).await;
|
||||||
let mut guild_data = guild_data_opt.unwrap();
|
let guild_data = guild_data_opt.unwrap();
|
||||||
|
|
||||||
if args.len() == 1 {
|
if args.len() == 1 {
|
||||||
match args.single::<u8>() {
|
match args.single::<u8>() {
|
||||||
Ok(volume) => {
|
Ok(volume) => {
|
||||||
guild_data.volume = volume;
|
guild_data.write().await.volume = volume;
|
||||||
|
|
||||||
guild_data.commit(pool).await?;
|
guild_data.read().await.commit(pool).await?;
|
||||||
|
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
.say(&ctx, format!("Volume changed to {}%", volume))
|
.say(&ctx, format!("Volume changed to {}%", volume))
|
||||||
@ -636,15 +673,19 @@ async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
}
|
}
|
||||||
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
let read = guild_data.read().await;
|
||||||
|
|
||||||
msg.channel_id.say(&ctx,
|
msg.channel_id.say(&ctx,
|
||||||
format!("Current server volume: {vol}%. Change the volume with ```{prefix}volume <new volume>```",
|
format!("Current server volume: {vol}%. Change the volume with ```{prefix}volume <new volume>```",
|
||||||
vol = guild_data.volume, prefix = guild_data.prefix)).await?;
|
vol = read.volume, prefix = read.prefix)).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
let read = guild_data.read().await;
|
||||||
|
|
||||||
msg.channel_id.say(&ctx,
|
msg.channel_id.say(&ctx,
|
||||||
format!("Current server volume: {vol}%. Change the volume with ```{prefix}volume <new volume>```",
|
format!("Current server volume: {vol}%. Change the volume with ```{prefix}volume <new volume>```",
|
||||||
vol = guild_data.volume, prefix = guild_data.prefix)).await?;
|
vol = read.volume, prefix = read.prefix)).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -653,14 +694,6 @@ async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
#[command]
|
#[command]
|
||||||
#[permission_level(Restricted)]
|
#[permission_level(Restricted)]
|
||||||
async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
|
||||||
let guild = match msg.guild(&ctx.cache).await {
|
|
||||||
Some(guild) => guild,
|
|
||||||
|
|
||||||
None => {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let pool = ctx
|
let pool = ctx
|
||||||
.data
|
.data
|
||||||
.read()
|
.read()
|
||||||
@ -669,10 +702,10 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
.cloned()
|
.cloned()
|
||||||
.expect("Could not get SQLPool from data");
|
.expect("Could not get SQLPool from data");
|
||||||
|
|
||||||
let mut guild_data;
|
let guild_data;
|
||||||
|
|
||||||
{
|
{
|
||||||
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
|
let guild_data_opt = ctx.get_from_id(msg.guild_id.unwrap()).await;
|
||||||
|
|
||||||
guild_data = guild_data_opt.unwrap();
|
guild_data = guild_data_opt.unwrap();
|
||||||
}
|
}
|
||||||
@ -681,13 +714,19 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
match args.single::<String>() {
|
match args.single::<String>() {
|
||||||
Ok(prefix) => {
|
Ok(prefix) => {
|
||||||
if prefix.len() <= 5 {
|
if prefix.len() <= 5 {
|
||||||
guild_data.prefix = prefix;
|
let reply = format!("Prefix changed to `{}`", prefix);
|
||||||
|
|
||||||
guild_data.commit(pool).await?;
|
{
|
||||||
|
guild_data.write().await.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
msg.channel_id
|
{
|
||||||
.say(&ctx, format!("Prefix changed to `{}`", guild_data.prefix))
|
let read = guild_data.read().await;
|
||||||
.await?;
|
|
||||||
|
read.commit(pool).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.channel_id.say(&ctx, reply).await?;
|
||||||
} else {
|
} else {
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
.say(&ctx, "Prefix must be less than 5 characters long")
|
.say(&ctx, "Prefix must be less than 5 characters long")
|
||||||
@ -701,7 +740,7 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
&ctx,
|
&ctx,
|
||||||
format!(
|
format!(
|
||||||
"Usage: `{prefix}prefix <new prefix>`",
|
"Usage: `{prefix}prefix <new prefix>`",
|
||||||
prefix = guild_data.prefix
|
prefix = guild_data.read().await.prefix
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
@ -713,7 +752,7 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
|
|||||||
&ctx,
|
&ctx,
|
||||||
format!(
|
format!(
|
||||||
"Usage: `{prefix}prefix <new prefix>`",
|
"Usage: `{prefix}prefix <new prefix>`",
|
||||||
prefix = guild_data.prefix
|
prefix = guild_data.read().await.prefix
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
@ -1265,14 +1304,6 @@ WHERE
|
|||||||
#[command]
|
#[command]
|
||||||
#[permission_level(Managed)]
|
#[permission_level(Managed)]
|
||||||
async fn allow_greet_sounds(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
async fn allow_greet_sounds(ctx: &Context, msg: &Message, _args: Args) -> CommandResult {
|
||||||
let guild = match msg.guild(&ctx.cache).await {
|
|
||||||
Some(guild) => guild,
|
|
||||||
|
|
||||||
None => {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let pool = ctx
|
let pool = ctx
|
||||||
.data
|
.data
|
||||||
.read()
|
.read()
|
||||||
@ -1281,19 +1312,23 @@ async fn allow_greet_sounds(ctx: &Context, msg: &Message, _args: Args) -> Comman
|
|||||||
.cloned()
|
.cloned()
|
||||||
.expect("Could not acquire SQL pool from data");
|
.expect("Could not acquire SQL pool from data");
|
||||||
|
|
||||||
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
|
let guild_data_opt = ctx.get_from_id(msg.guild_id.unwrap()).await;
|
||||||
|
|
||||||
if let Some(mut guild_data) = guild_data_opt {
|
if let Ok(guild_data) = guild_data_opt {
|
||||||
guild_data.allow_greets = !guild_data.allow_greets;
|
let current = guild_data.read().await.allow_greets;
|
||||||
|
|
||||||
guild_data.commit(pool).await?;
|
{
|
||||||
|
guild_data.write().await.allow_greets = !current;
|
||||||
|
}
|
||||||
|
|
||||||
|
guild_data.read().await.commit(pool).await?;
|
||||||
|
|
||||||
msg.channel_id
|
msg.channel_id
|
||||||
.say(
|
.say(
|
||||||
&ctx,
|
&ctx,
|
||||||
format!(
|
format!(
|
||||||
"Greet sounds have been {}abled in this server",
|
"Greet sounds have been {}abled in this server",
|
||||||
if guild_data.allow_greets { "en" } else { "dis" }
|
if !current { "en" } else { "dis" }
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
Loading…
Reference in New Issue
Block a user