Merge pull request #11 from minneelyyyy/main

merge main
This commit is contained in:
mins
2024-12-12 16:21:07 -05:00
committed by GitHub
5 changed files with 26 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
use crate::{Context, Error}; use crate::{Context, Error};
use poise::serenity_prelude::UserId; use poise::serenity_prelude::UserId;
use sqlx::{types::chrono::{DateTime, Local, TimeZone}, PgExecutor, Row}; use sqlx::{types::chrono::{DateTime, Utc, TimeZone}, PgExecutor, Row};
use std::time::Duration; use std::time::Duration;
@@ -31,7 +31,7 @@ where
Ok(()) Ok(())
} }
async fn get_last<'a, E>(db: E, user: UserId) -> Result<Option<DateTime<Local>>, Error> async fn get_last<'a, E>(db: E, user: UserId) -> Result<Option<DateTime<Utc>>, Error>
where where
E: PgExecutor<'a>, E: PgExecutor<'a>,
{ {
@@ -45,7 +45,7 @@ where
} }
} }
async fn set_last<'a, E>(db: E, user: UserId, last: DateTime<Local>) -> Result<(), Error> async fn set_last<'a, E>(db: E, user: UserId, last: DateTime<Utc>) -> Result<(), Error>
where where
E: PgExecutor<'a>, E: PgExecutor<'a>,
{ {
@@ -57,6 +57,7 @@ where
Ok(()) Ok(())
} }
/// Tells you what your current daily streak is
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn streak(ctx: Context<'_>) -> Result<(), Error> { pub async fn streak(ctx: Context<'_>) -> Result<(), Error> {
let db = &ctx.data().database; let db = &ctx.data().database;
@@ -72,9 +73,9 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> {
let last = get_last(&mut *tx, user).await?; let last = get_last(&mut *tx, user).await?;
let existed = last.is_some(); let existed = last.is_some();
let last = last.unwrap_or(Local.timestamp_opt(0, 0).unwrap()); let last = last.unwrap_or(Utc.timestamp_opt(0, 0).unwrap());
let now = Local::now(); let now = Utc::now();
let next_daily = last + Duration::from_secs(24 * 60 * 60); let next_daily = last + Duration::from_secs(24 * 60 * 60);
let time_to_redeem = next_daily + Duration::from_secs(24 * 60 * 60); let time_to_redeem = next_daily + Duration::from_secs(24 * 60 * 60);
@@ -114,12 +115,13 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> {
ctx.reply(format!("{begin}**{payout}** tokens were added to your balance.{end}")).await?; ctx.reply(format!("{begin}**{payout}** tokens were added to your balance.{end}")).await?;
} else { } else {
ctx.reply(format!("Your next daily is not available! It will be available on {}.", next_daily.to_rfc2822())).await?; ctx.reply(format!("Your next daily is not available! It will be available <t:{}:R>.", next_daily.timestamp())).await?;
} }
Ok(()) Ok(())
} }
// Redeem daily tokens.
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn claim(ctx: Context<'_>) -> Result<(), Error> { pub async fn claim(ctx: Context<'_>) -> Result<(), Error> {
do_claim(ctx).await do_claim(ctx).await

View File

@@ -69,9 +69,9 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col
Color::from_rgb(rgb.r, rgb.g, rgb.b) Color::from_rgb(rgb.r, rgb.g, rgb.b)
}; };
if let Some(guild) = ctx.guild_id() {
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
if let Some(guild) = ctx.guild_id() {
match super::get_user_role(ctx.author().id, guild, &mut *tx).await? { match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
Some(role) => { Some(role) => {
guild.edit_role(ctx, role, EditRole::new().colour(color)).await?; guild.edit_role(ctx, role, EditRole::new().colour(color)).await?;
@@ -93,12 +93,13 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col
.bind(guild.get() as i64) .bind(guild.get() as i64)
.execute(&mut *tx).await?; .execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, ctx.author().id).await?; let member = guild.member(ctx, ctx.author().id).await?;
member.add_role(ctx, role.clone()).await?; member.add_role(ctx, role.clone()).await?;
ctx.reply(format!("You have been given the {} role!", role)).await?; ctx.reply(format!("You have been given the {} role!", role)).await?;
tx.commit().await?;
return Ok(()); return Ok(());
} }
} }

View File

@@ -6,9 +6,9 @@ use poise::serenity_prelude::EditRole;
/// Change the name of your personal role /// Change the name of your personal role
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> { pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
if let Some(guild) = ctx.guild_id() {
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
if let Some(guild) = ctx.guild_id() {
let role = match super::get_user_role(ctx.author().id, guild, &mut *tx).await? { let role = match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
Some(role) => role, Some(role) => role,
None => { None => {
@@ -20,11 +20,11 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
.bind(guild.get() as i64) .bind(guild.get() as i64)
.execute(&mut *tx).await?; .execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, ctx.author().id).await?; let member = guild.member(ctx, ctx.author().id).await?;
member.add_role(ctx, role.clone()).await?; member.add_role(ctx, role.clone()).await?;
tx.commit().await?;
ctx.reply(format!("You've been given the {} role!", role)).await?; ctx.reply(format!("You've been given the {} role!", role)).await?;
return Ok(()); return Ok(());

View File

@@ -6,9 +6,9 @@ use poise::serenity_prelude as serenity;
/// Register an existing role as a user's custom role /// Register an existing role as a user's custom role
#[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")] #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")]
pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> { pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> {
if let Some(guild) = ctx.guild_id() {
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
if let Some(guild) = ctx.guild_id() {
match super::get_user_role(user.id, guild, &mut *tx).await? { match super::get_user_role(user.id, guild, &mut *tx).await? {
Some(role) => { Some(role) => {
let role = guild.role(ctx, role).await?; let role = guild.role(ctx, role).await?;
@@ -21,6 +21,8 @@ pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Ro
.bind(role.id.get() as i64) .bind(role.id.get() as i64)
.execute(&mut *tx).await?; .execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, user.id).await?; let member = guild.member(ctx, user.id).await?;
member.add_role(ctx, role.id).await?; member.add_role(ctx, role.id).await?;
@@ -31,6 +33,5 @@ pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Ro
ctx.reply("This command can only be run in a guild!").await?; ctx.reply("This command can only be run in a guild!").await?;
} }
tx.commit().await?;
Ok(()) Ok(())
} }

View File

@@ -53,9 +53,9 @@ impl Inventory {
let x = sqlx::query_as( let x = sqlx::query_as(
r#" r#"
SELECT id, name, game, item, data FROM items SELECT id, name, game, item, data FROM items
where item = $1 where item = $1 AND owner = $2
"# "#
).bind(item as i64).fetch_one(db).await.ok(); ).bind(item as i64).bind(self.user.get() as i64).fetch_one(db).await.ok();
Ok(x) Ok(x)
} }
@@ -67,9 +67,9 @@ impl Inventory {
let x = sqlx::query_as( let x = sqlx::query_as(
r#" r#"
SELECT id, name, game, item, data FROM items SELECT id, name, game, item, data FROM items
where name = $1 where name = $1 AND user = $2
"# "#
).bind(name).fetch_one(db).await.ok(); ).bind(name).bind(self.user.get() as i64).fetch_one(db).await.ok();
Ok(x) Ok(x)
} }