diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml
new file mode 100644
index 0000000..dec1c89
--- /dev/null
+++ b/.idea/dataSources.xml
@@ -0,0 +1,11 @@
+
+
+
+
+ mysql
+ true
+ com.mysql.jdbc.Driver
+ jdbc:mysql://localhost:3306/soundfx
+
+
+
\ No newline at end of file
diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml
new file mode 100644
index 0000000..ea20568
--- /dev/null
+++ b/.idea/sqldialects.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/create.sql b/create.sql
new file mode 100644
index 0000000..d61ed5a
--- /dev/null
+++ b/create.sql
@@ -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
+);
diff --git a/src/guilddata.rs b/src/guilddata.rs
index 38203a8..e071914 100644
--- a/src/guilddata.rs
+++ b/src/guilddata.rs
@@ -10,23 +10,27 @@ pub struct GuildData {
}
impl GuildData {
- pub async fn get_from_id(guild_id: u64, db_pool: MySqlPool) -> Option {
- let guild = sqlx::query_as_unchecked!(
+ pub async fn get_from_id(guild: Guild, db_pool: MySqlPool) -> Option {
+ 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()
diff --git a/src/main.rs b/src/main.rs
index f382d32..3c41a91 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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::().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::().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::().cloned().expect("Could not get VoiceManager from data");
+ let voice_manager_lock = ctx.data.read().await
+ .get::().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::().cloned().expect("Could not get VoiceGuilds from data");
+ let voice_guilds_lock = ctx.data.read().await
+ .get::().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> {
}
};
- 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::().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::().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;