Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
Cleaned up postgre helpers (#4)
Browse files Browse the repository at this point in the history
* Change status if not main

* Moved postgre functions
  • Loading branch information
timcole committed Jul 12, 2020
1 parent 0325137 commit c01a9d8
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 82 deletions.
12 changes: 10 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use std::process::Command;

fn main() {
let output = Command::new("git")
let commit = Command::new("git")
.args(&["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
let branch = Command::new("git")
.args(&["rev-parse", "--abbrev-ref", "HEAD"])
.output()
.unwrap();

let git_hash = String::from_utf8(commit.stdout).unwrap();
let git_branch = String::from_utf8(branch.stdout).unwrap();

println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rustc-env=GIT_BRANCH={}", git_branch);
}
19 changes: 11 additions & 8 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::postgres::store;
use crate::postgres::{delete, get, store};
use crate::twitch::automod;
use crate::utils::mention;
use serenity::{
Expand Down Expand Up @@ -29,12 +29,15 @@ impl EventHandler for Handler {
shard[1],
);
}

let mut status = format!("Version: {}", &env!("GIT_HASH")[0..7]);
if &env!("GIT_BRANCH") != &"main" {
status = format!("Branch: {}", env!("GIT_BRANCH"));
}

ctx
.set_presence(
Some(Activity::listening(&format!(
"Version: {}",
&env!("GIT_HASH")[0..7]
))),
Some(Activity::listening(&status)),
OnlineStatus::DoNotDisturb,
)
.await;
Expand Down Expand Up @@ -73,7 +76,7 @@ impl EventHandler for Handler {
}
}
async fn guild_create(&self, ctx: Context, guild: Guild, is_new: bool) {
if !is_new && !store::is_new_guild(ctx.clone(), guild.id.clone()).await {
if !is_new && !get::is_new_guild(ctx.clone(), guild.id.clone()).await {
return;
}

Expand Down Expand Up @@ -106,7 +109,7 @@ impl EventHandler for Handler {
role_id: RoleId,
_: Option<Role>,
) {
store::del_role(ctx, guild_id, role_id).await;
delete::role(ctx, guild_id, role_id).await;
}
async fn guild_role_update(&self, ctx: Context, guild_id: GuildId, _: Option<Role>, new: Role) {
store::role(ctx, guild_id, &new).await;
Expand All @@ -127,6 +130,6 @@ impl EventHandler for Handler {
user: User,
_: Option<Member>,
) {
store::del_member(ctx, guild_id, user.id).await;
delete::member(ctx, guild_id, user.id).await;
}
}
56 changes: 56 additions & 0 deletions src/postgres/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::PostgresPool;
use serenity::{
client::Context,
model::id::{GuildId, RoleId, UserId},
};
use std::convert::TryFrom;

pub async fn role(ctx: Context, guild_id: GuildId, role_id: RoleId) {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Some(v) => v.get().await.unwrap(),
None => {
log::error!("Error getting the postgres pool.");
return;
}
};

match pool
.query(
"DELETE FROM roles WHERE id = $1 AND guild_id = $2",
&[
&i64::try_from(*role_id.as_u64()).unwrap(),
&i64::try_from(*guild_id.as_u64()).unwrap(),
],
)
.await
{
Ok(_) => log::debug!("Deleted role {} in {}", role_id, guild_id),
Err(e) => log::error!("{:#?}", e),
};
}

pub async fn member(ctx: Context, guild_id: GuildId, user_id: UserId) {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Some(v) => v.get().await.unwrap(),
None => {
log::error!("Error getting the postgres pool.");
return;
}
};

match pool
.query(
"DELETE FROM members WHERE id = $1 AND guild_id = $2",
&[
&i64::try_from(*user_id.as_u64()).unwrap(),
&i64::try_from(*guild_id.as_u64()).unwrap(),
],
)
.await
{
Ok(_) => log::debug!("Deleted member {} in {}", user_id, guild_id),
Err(e) => log::error!("{:#?}", e),
};
}
24 changes: 24 additions & 0 deletions src/postgres/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::PostgresPool;
use serenity::{client::Context, model::id::GuildId};
use std::convert::TryFrom;

pub async fn is_new_guild(ctx: Context, guild_id: GuildId) -> bool {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Some(v) => v.get().await.unwrap(),
None => {
log::error!("Error getting the postgres pool.");
return false;
}
};

let has_guild = pool
.query(
"SELECT 1 FROM guilds WHERE id = $1",
&[&i64::try_from(*guild_id.as_u64()).unwrap()],
)
.await
.ok();

has_guild.unwrap().get(0).is_none()
}
2 changes: 2 additions & 0 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod connection;
pub mod delete;
pub mod get;
pub mod store;
73 changes: 1 addition & 72 deletions src/postgres/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,11 @@ use serenity::{
model::{
channel::Message,
guild::{Guild, Member, PartialGuild, Role},
id::{GuildId, RoleId, UserId},
id::GuildId,
},
};
use std::convert::TryFrom;

pub async fn is_new_guild(ctx: Context, guild_id: GuildId) -> bool {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Some(v) => v.get().await.unwrap(),
None => {
log::error!("Error getting the postgres pool.");
return false;
}
};

let has_guild = pool
.query(
"SELECT 1 FROM guilds WHERE id = $1",
&[&i64::try_from(*guild_id.as_u64()).unwrap()],
)
.await
.ok();

has_guild.unwrap().get(0).is_none()
}

pub async fn del_role(ctx: Context, guild_id: GuildId, role_id: RoleId) {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Some(v) => v.get().await.unwrap(),
None => {
log::error!("Error getting the postgres pool.");
return;
}
};

match pool
.query(
"DELETE FROM roles WHERE id = $1 AND guild_id = $2",
&[
&i64::try_from(*role_id.as_u64()).unwrap(),
&i64::try_from(*guild_id.as_u64()).unwrap(),
],
)
.await
{
Ok(_) => log::debug!("Deleted role {} in {}", role_id, guild_id),
Err(e) => log::error!("{:#?}", e),
};
}

pub async fn del_member(ctx: Context, guild_id: GuildId, user_id: UserId) {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Some(v) => v.get().await.unwrap(),
None => {
log::error!("Error getting the postgres pool.");
return;
}
};

match pool
.query(
"DELETE FROM members WHERE id = $1 AND guild_id = $2",
&[
&i64::try_from(*user_id.as_u64()).unwrap(),
&i64::try_from(*guild_id.as_u64()).unwrap(),
],
)
.await
{
Ok(_) => log::debug!("Deleted member {} in {}", user_id, guild_id),
Err(e) => log::error!("{:#?}", e),
};
}

pub async fn message(ctx: Context, msg: Message, permitted: bool) {
let data = ctx.data.read().await;
let pool = match data.get::<PostgresPool>() {
Expand Down

0 comments on commit c01a9d8

Please sign in to comment.