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

11
.idea/dataSources.xml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="MySQL for 5.1 - soundfx@localhost" uuid="1067c1d0-1386-4a39-b3f5-6d48d6f279eb">
<driver-ref>mysql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:3306/soundfx</jdbc-url>
</data-source>
</component>
</project>

7
.idea/sqldialects.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/create.sql" dialect="GenericSQL" />
<file url="PROJECT" dialect="MySQL" />
</component>
</project>

35
create.sql Normal file
View File

@ -0,0 +1,35 @@
CREATE TABLE servers (
id BIGINT UNSIGNED NOT NULL,
name VARCHAR(100),
prefix VARCHAR(5) DEFAULT '?',
volume TINYINT DEFAULT 100,
allow_greets BOOL DEFAULT 1,
PRIMARY KEY (id)
);
CREATE TABLE sounds (
id INT UNSIGNED NOT NULL,
name VARCHAR(20),
plays INT UNSIGNED NOT NULL DEFAULT 0,
public BOOL NOT NULL DEFAULT 1,
src MEDIUMBLOB NOT NULL,
server_id BIGINT UNSIGNED NOT NULL,
uploader_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE roles (
guild_id BIGINT UNSIGNED NOT NULL,
role BIGINT UNSIGNED NOT NULL
);
CREATE TABLE users (
user BIGINT UNSIGNED NOT NULL,
join_sound_id INT UNSIGNED,
FOREIGN KEY users(join_sound_id) REFERENCES sounds(id) ON DELETE SET NULL ON UPDATE CASCADE
);

View File

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

View File

@ -257,10 +257,13 @@ impl EventHandler for Handler {
if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) { if let (Some(guild_id), Some(user_channel)) = (guild_id_opt, new.channel_id) {
if old.is_none() { if old.is_none() {
if let Some(guild) = ctx.cache.guild(guild_id).await {
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .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;
if let Some(guild_data) = guild_data_opt { if let Some(guild_data) = guild_data_opt {
if guild_data.allow_greets { if guild_data.allow_greets {
@ -300,8 +303,6 @@ SELECT name, id, plays, public, server_id, uploader_id
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) { if let Some(handler) = voice_manager.join(guild_id, user_channel) {
let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await; let _audio = play_audio(&mut sound, guild_data, handler, voice_guilds, pool).await;
} }
@ -315,6 +316,7 @@ SELECT name, id, plays, public, server_id, uploader_id
} }
} }
} }
}
async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashMap<GuildId, u8>>, pool: MySqlPool) async fn play_audio(sound: &mut Sound, guild: GuildData, handler: &mut VoiceHandler, mut voice_guilds: MutexGuard<'_, HashMap<GuildId, u8>>, pool: MySqlPool)
-> Result<(), Box<dyn std::error::Error>> { -> Result<(), Box<dyn std::error::Error>> {
@ -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) => { Some(mut guild_data) => {
let name = Some(guild.name); 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 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) { match voice_manager.join(guild_id, user_channel) {
Some(handler) => { Some(handler) => {
@ -589,7 +591,7 @@ async fn change_volume(ctx: &Context, msg: &Message, mut args: Args) -> CommandR
let pool = ctx.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data"); .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(); let mut guild_data = guild_data_opt.unwrap();
if args.len() == 1 { 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 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(); guild_data = guild_data_opt.unwrap();
} }
@ -1084,10 +1086,18 @@ async fn stop_playing(ctx: &Context, msg: &Message, _args: Args) -> CommandResul
#[command("allow_greet")] #[command("allow_greet")]
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.data.read().await let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not acquire SQL pool from data"); .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 { if let Some(mut guild_data) = guild_data_opt {
guild_data.allow_greets = !guild_data.allow_greets; guild_data.allow_greets = !guild_data.allow_greets;