get guild by id now creates a data entry if not available
This commit is contained in:
parent
4fa8313c7b
commit
302e0d0254
11
.idea/dataSources.xml
generated
Normal file
11
.idea/dataSources.xml
generated
Normal 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
generated
Normal file
7
.idea/sqldialects.xml
generated
Normal 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
35
create.sql
Normal 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
|
||||
);
|
@ -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()
|
||||
|
90
src/main.rs
90
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::<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;
|
||||
|
Loading…
Reference in New Issue
Block a user