get guild by id now creates a data entry if not available

This commit is contained in:
jude
2020-06-12 13:33:26 +01:00
parent 4fa8313c7b
commit 302e0d0254
5 changed files with 113 additions and 46 deletions

View File

@ -10,23 +10,27 @@ pub struct GuildData {
}
impl GuildData {
pub async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Option<GuildData> {
let guild = sqlx::query_as_unchecked!(
pub async fn get_from_id(guild: Guild, db_pool: MySqlPool) -> Option<GuildData> {
let guild_data = sqlx::query_as_unchecked!(
GuildData,
"
SELECT id, name, prefix, volume, allow_greets
FROM servers
WHERE id = ?
", guild_id
", guild.id.as_u64()
)
.fetch_one(&db_pool)
.await;
match guild {
match guild_data {
Ok(g) => Some(g),
Err(sqlx::Error::RowNotFound) => {
Self::create_from_guild(guild, db_pool).await.ok()
}
Err(e) => {
println!("Guild query issue: {:?}", e);
println!("{:?}", e);
None
}
@ -45,7 +49,7 @@ INSERT INTO servers (id, name)
sqlx::query!(
"
INSERT INTO roles (guild_id, role)
INSERT IGNORE INTO roles (guild_id, role)
VALUES (?, ?)
",
guild.id.as_u64(), guild.id.as_u64()

View File

@ -257,57 +257,59 @@ impl EventHandler for Handler {
if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) {
if old.is_none() {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let guild_data_opt = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await;
if let Some(guild) = ctx.cache.guild(guild_id).await {
if let Some(guild_data) = guild_data_opt {
if guild_data.allow_greets {
let join_id_res = sqlx::query!(
"
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
if let Some(guild_data) = guild_data_opt {
if guild_data.allow_greets {
let join_id_res = sqlx::query!(
"
SELECT join_sound_id
FROM users
WHERE user = ? AND join_sound_id IS NOT NULL
",
new.user_id.as_u64()
)
.fetch_one(&pool)
.await;
",
new.user_id.as_u64()
)
.fetch_one(&pool)
.await;
match join_id_res {
Ok(join_id_record) => {
let join_id = join_id_record.join_sound_id;
let mut sound = sqlx::query_as_unchecked!(
Sound,
"
match join_id_res {
Ok(join_id_record) => {
let join_id = join_id_record.join_sound_id;
let mut sound = sqlx::query_as_unchecked!(
Sound,
"
SELECT name, id, plays, public, server_id, uploader_id
FROM sounds
WHERE id = ?
",
join_id
)
.fetch_one(&pool)
.await.unwrap();
",
join_id
)
.fetch_one(&pool)
.await.unwrap();
let voice_manager_lock = ctx.data.read().await
.get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data");
let voice_manager_lock = ctx.data.read().await
.get::<VoiceManager>().cloned().expect("Could not get VoiceManager from data");
let mut voice_manager = voice_manager_lock.lock().await;
let mut voice_manager = voice_manager_lock.lock().await;
let voice_guilds_lock = ctx.data.read().await
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let voice_guilds_lock = ctx.data.read().await
.get::<VoiceGuilds>().cloned().expect("Could not get VoiceGuilds from data");
let voice_guilds = voice_guilds_lock.lock().await;
let voice_guilds = voice_guilds_lock.lock().await;
let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap();
if let Some(handler) = voice_manager.join(guild_id, user_channel) {
let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await;
if let Some(handler) = voice_manager.join(guild_id, user_channel) {
let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await;
}
}
}
Err(_) => {}
Err(_) => {}
}
}
}
}
@ -377,7 +379,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};
match GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool.clone()).await {
match GuildData::get_from_id(guild.clone(), pool.clone()).await {
Some(mut guild_data) => {
let name = Some(guild.name);
@ -515,7 +517,7 @@ async fn play(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let voice_guilds = voice_guilds_lock.lock().await;
let guild_data = GuildData::get_from_id(*guild_id.as_u64(), pool.clone()).await.unwrap();
let guild_data = GuildData::get_from_id(guild, pool.clone()).await.unwrap();
match voice_manager.join(guild_id, user_channel) {
Some(handler) => {
@ -589,7 +591,7 @@ async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let guild_data_opt = GuildData::get_from_id(*guild.id.as_u64(), pool.clone()).await;
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
let mut guild_data = guild_data_opt.unwrap();
if args.len() == 1 {
@ -634,7 +636,7 @@ async fn change_prefix(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
let mut guild_data;
{
let guild_data_opt = GuildData::get_from_id(*guild.id.as_u64(), pool.clone()).await;
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
guild_data = guild_data_opt.unwrap();
}
@ -1084,10 +1086,18 @@ async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResul
#[command("allow_greet")]
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.data.read().await
.get::<SQLPool>().cloned().expect("Could not acquire SQL pool from data");
let guild_data_opt = GuildData::get_from_id(*msg.guild_id.unwrap().as_u64(), pool.clone()).await;
let guild_data_opt = GuildData::get_from_id(guild, pool.clone()).await;
if let Some(mut guild_data) = guild_data_opt {
guild_data.allow_greets = !guild_data.allow_greets;