@@ -1,7 +1,7 @@
|
||||
use crate::{Context, Error};
|
||||
|
||||
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;
|
||||
|
||||
@@ -31,7 +31,7 @@ where
|
||||
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
|
||||
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
|
||||
E: PgExecutor<'a>,
|
||||
{
|
||||
@@ -57,6 +57,7 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tells you what your current daily streak is
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn streak(ctx: Context<'_>) -> Result<(), Error> {
|
||||
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 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 time_to_redeem = next_daily + Duration::from_secs(24 * 60 * 60);
|
||||
|
||||
@@ -103,7 +104,7 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> {
|
||||
}
|
||||
|
||||
let payout = 50 + 10 * streak.min(7);
|
||||
|
||||
|
||||
let balance = super::get_balance(user, &mut *tx).await?;
|
||||
super::change_balance(user, balance + payout, &mut *tx).await?;
|
||||
|
||||
@@ -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?;
|
||||
} 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(())
|
||||
}
|
||||
|
||||
// Redeem daily tokens.
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn claim(ctx: Context<'_>) -> Result<(), Error> {
|
||||
do_claim(ctx).await
|
||||
|
||||
@@ -69,9 +69,9 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col
|
||||
Color::from_rgb(rgb.r, rgb.g, rgb.b)
|
||||
};
|
||||
|
||||
let mut tx = ctx.data().database.begin().await?;
|
||||
|
||||
if let Some(guild) = ctx.guild_id() {
|
||||
let mut tx = ctx.data().database.begin().await?;
|
||||
|
||||
match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
|
||||
Some(role) => {
|
||||
guild.edit_role(ctx, role, EditRole::new().colour(color)).await?;
|
||||
@@ -92,13 +92,14 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col
|
||||
.bind(role.id.get() as i64)
|
||||
.bind(guild.get() as i64)
|
||||
.execute(&mut *tx).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
let member = guild.member(ctx, ctx.author().id).await?;
|
||||
member.add_role(ctx, role.clone()).await?;
|
||||
|
||||
ctx.reply(format!("You have been given the {} role!", role)).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ use poise::serenity_prelude::EditRole;
|
||||
/// Change the name of your personal role
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
|
||||
let mut tx = ctx.data().database.begin().await?;
|
||||
|
||||
if let Some(guild) = ctx.guild_id() {
|
||||
let mut tx = ctx.data().database.begin().await?;
|
||||
|
||||
let role = match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
|
||||
Some(role) => role,
|
||||
None => {
|
||||
@@ -20,11 +20,11 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
|
||||
.bind(guild.get() as i64)
|
||||
.execute(&mut *tx).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
let member = guild.member(ctx, ctx.author().id).await?;
|
||||
member.add_role(ctx, role.clone()).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
ctx.reply(format!("You've been given the {} role!", role)).await?;
|
||||
|
||||
return Ok(());
|
||||
|
||||
@@ -5,10 +5,10 @@ use poise::serenity_prelude as serenity;
|
||||
|
||||
/// Register an existing role as a user's custom role
|
||||
#[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")]
|
||||
pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> {
|
||||
let mut tx = ctx.data().database.begin().await?;
|
||||
|
||||
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?;
|
||||
|
||||
match super::get_user_role(user.id, guild, &mut *tx).await? {
|
||||
Some(role) => {
|
||||
let role = guild.role(ctx, role).await?;
|
||||
@@ -20,6 +20,8 @@ pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Ro
|
||||
.bind(guild.get() as i64)
|
||||
.bind(role.id.get() as i64)
|
||||
.execute(&mut *tx).await?;
|
||||
|
||||
tx.commit().await?;
|
||||
|
||||
let member = guild.member(ctx, user.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?;
|
||||
}
|
||||
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ impl Inventory {
|
||||
let x = sqlx::query_as(
|
||||
r#"
|
||||
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)
|
||||
}
|
||||
@@ -67,9 +67,9 @@ impl Inventory {
|
||||
let x = sqlx::query_as(
|
||||
r#"
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user