Compare commits
No commits in common. "43c081cc312b3387be771c6ea7b51b85a88390f5" and "37eac33485069e49a84e0a56c2b84933a561f12d" have entirely different histories.
43c081cc31
...
37eac33485
10 changed files with 98 additions and 83 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use anyhow::Result;
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
use serenity::all::{CommandInteraction, Context, CreateCommand};
|
use serenity::all::{CommandInteraction, Context, CreateCommand};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
|
@ -10,7 +11,6 @@ include!("./mod_gen.rs");
|
||||||
pub enum CommandCategory {
|
pub enum CommandCategory {
|
||||||
Moderation,
|
Moderation,
|
||||||
Utils,
|
Utils,
|
||||||
Gestion,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandCategory {
|
impl CommandCategory {
|
||||||
|
|
@ -18,7 +18,6 @@ impl CommandCategory {
|
||||||
match self {
|
match self {
|
||||||
Self::Utils => "🌐",
|
Self::Utils => "🌐",
|
||||||
Self::Moderation => "🛡️",
|
Self::Moderation => "🛡️",
|
||||||
Self::Gestion => "👑",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,7 +25,6 @@ impl CommandCategory {
|
||||||
match self {
|
match self {
|
||||||
Self::Utils => "Utils",
|
Self::Utils => "Utils",
|
||||||
Self::Moderation => "Moderation",
|
Self::Moderation => "Moderation",
|
||||||
Self::Gestion => "Gestion",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -36,6 +34,15 @@ pub trait SlashCommand: Send + Sync {
|
||||||
fn name(&self) -> &'static str;
|
fn name(&self) -> &'static str;
|
||||||
fn description(&self) -> &'static str;
|
fn description(&self) -> &'static str;
|
||||||
fn category(&self) -> &'static CommandCategory;
|
fn category(&self) -> &'static CommandCategory;
|
||||||
|
fn command_id_ref(&self) -> &AtomicU64;
|
||||||
|
|
||||||
|
fn get_id(&self) -> u64 {
|
||||||
|
self.command_id_ref().load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_id(&self, id: u64) {
|
||||||
|
self.command_id_ref().store(id, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
fn register(&self) -> CreateCommand;
|
fn register(&self) -> CreateCommand;
|
||||||
|
|
||||||
|
|
@ -45,7 +52,7 @@ pub trait SlashCommand: Send + Sync {
|
||||||
command: &CommandInteraction,
|
command: &CommandInteraction,
|
||||||
database: &PgPool,
|
database: &PgPool,
|
||||||
_emoji: &EmojiConfig,
|
_emoji: &EmojiConfig,
|
||||||
) -> Result<()>;
|
) -> Result<(), serenity::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CommandEntry {
|
pub struct CommandEntry {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
sync::atomic::{
|
||||||
|
AtomicU64,
|
||||||
|
},
|
||||||
time::Instant,
|
time::Instant,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -14,10 +17,19 @@ use serenity::all::{
|
||||||
CreateInteractionResponseMessage, EditInteractionResponse,
|
CreateInteractionResponseMessage, EditInteractionResponse,
|
||||||
};
|
};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use tracing::{debug, info};
|
use tracing::info;
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
pub struct Ping;
|
pub struct Ping {
|
||||||
|
pub command_id: AtomicU64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ping {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
command_id: AtomicU64::new(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[serenity::async_trait]
|
#[serenity::async_trait]
|
||||||
impl SlashCommand for Ping {
|
impl SlashCommand for Ping {
|
||||||
|
|
@ -33,6 +45,10 @@ impl SlashCommand for Ping {
|
||||||
&CommandCategory::Utils
|
&CommandCategory::Utils
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn command_id_ref(&self) -> &AtomicU64 {
|
||||||
|
&self.command_id
|
||||||
|
}
|
||||||
|
|
||||||
fn register(&self) -> CreateCommand {
|
fn register(&self) -> CreateCommand {
|
||||||
info!("\t✅ | {}", self.name());
|
info!("\t✅ | {}", self.name());
|
||||||
CreateCommand::new(self.name()).description(self.description())
|
CreateCommand::new(self.name()).description(self.description())
|
||||||
|
|
@ -44,8 +60,7 @@ impl SlashCommand for Ping {
|
||||||
command: &CommandInteraction,
|
command: &CommandInteraction,
|
||||||
_database: &PgPool,
|
_database: &PgPool,
|
||||||
_emoji: &EmojiConfig,
|
_emoji: &EmojiConfig,
|
||||||
) -> Result<()> {
|
) -> Result<(), serenity::Error> {
|
||||||
debug!("Ping command called");
|
|
||||||
let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new()
|
let message: CreateInteractionResponseMessage = CreateInteractionResponseMessage::new()
|
||||||
.content("🏓 | Pong!")
|
.content("🏓 | Pong!")
|
||||||
.ephemeral(true);
|
.ephemeral(true);
|
||||||
|
|
@ -66,5 +81,5 @@ impl SlashCommand for Ping {
|
||||||
}
|
}
|
||||||
|
|
||||||
inventory::submit! {
|
inventory::submit! {
|
||||||
CommandEntry { create: || Box::new(Ping) }
|
CommandEntry { create: || Box::new(Ping::new()) }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,16 @@
|
||||||
use sqlx::{PgPool, query, query_as};
|
use sqlx::{PgPool, query, query_as};
|
||||||
use crate::models::bot::{DbBot, BotPresence, BotActivity};
|
use crate::models::bot::{DbBot, BotPresence, BotActivity};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
const BOT_ID: i32 = 1;
|
const BOT_ID: i32 = 1;
|
||||||
|
|
||||||
pub async fn init(db: &PgPool) -> Result<()> {
|
pub async fn init(db: &PgPool) -> Result<(), sqlx::Error> {
|
||||||
query!("INSERT INTO bots (id) VALUES ($1) ON CONFLICT DO NOTHING", BOT_ID)
|
query!("INSERT INTO bots (id) VALUES ($1) ON CONFLICT DO NOTHING", BOT_ID)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(db: &PgPool) -> Result<Option<DbBot>> {
|
pub async fn get(db: &PgPool) -> Result<Option<DbBot>, sqlx::Error> {
|
||||||
let bot: Option<DbBot> = query_as!(
|
let bot: Option<DbBot> = query_as!(
|
||||||
DbBot,
|
DbBot,
|
||||||
r#"SELECT status, activity_type as "activity_type: BotActivity", presence as "presence: BotPresence" FROM bots WHERE id = $1"#,
|
r#"SELECT status, activity_type as "activity_type: BotActivity", presence as "presence: BotPresence" FROM bots WHERE id = $1"#,
|
||||||
|
|
@ -22,21 +21,21 @@ pub async fn get(db: &PgPool) -> Result<Option<DbBot>> {
|
||||||
Ok(bot)
|
Ok(bot)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_status(db: &PgPool, status: &str) -> Result<()> {
|
pub async fn set_status(db: &PgPool, status: &str) -> Result<(), sqlx::Error> {
|
||||||
query!("UPDATE bots SET status = $1 WHERE id = $2", status, BOT_ID)
|
query!("UPDATE bots SET status = $1 WHERE id = $2", status, BOT_ID)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_activity(db: &PgPool, activity: BotActivity) -> Result<()> {
|
pub async fn set_activity(db: &PgPool, activity: BotActivity) -> Result<(), sqlx::Error> {
|
||||||
query!("UPDATE bots SET activity_type = $1::bot_activity WHERE id = $2", activity as BotActivity, BOT_ID)
|
query!("UPDATE bots SET activity_type = $1::bot_activity WHERE id = $2", activity as BotActivity, BOT_ID)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_presence(db: &PgPool, presence: BotPresence) -> Result<()> {
|
pub async fn set_presence(db: &PgPool, presence: BotPresence) -> Result<(), sqlx::Error> {
|
||||||
query!("UPDATE bots SET presence = $1::bot_presence WHERE id = $2", presence as BotPresence, BOT_ID)
|
query!("UPDATE bots SET presence = $1::bot_presence WHERE id = $2", presence as BotPresence, BOT_ID)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ use sqlx::{
|
||||||
query_scalar,
|
query_scalar,
|
||||||
};
|
};
|
||||||
use crate::models::DbGuild;
|
use crate::models::DbGuild;
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
pub enum LogChannel {
|
pub enum LogChannel {
|
||||||
Bot,
|
Bot,
|
||||||
|
|
@ -49,21 +48,21 @@ fn protect_select(asked: Protect) -> &'static str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(db: &PgPool, guild_id: &str) -> Result<()> {
|
pub async fn create(db: &PgPool, guild_id: &str) -> Result<(), sqlx::Error> {
|
||||||
query!("INSERT INTO guilds (guild_id) VALUES ($1) ON CONFLICT DO NOTHING", guild_id)
|
query!("INSERT INTO guilds (guild_id) VALUES ($1) ON CONFLICT DO NOTHING", guild_id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(db: &PgPool, guild_id: &str) -> Result<()> {
|
pub async fn delete(db: &PgPool, guild_id: &str) -> Result<(), sqlx::Error> {
|
||||||
query!("DELETE FROM guilds WHERE guild_id = $1", guild_id)
|
query!("DELETE FROM guilds WHERE guild_id = $1", guild_id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(db: &PgPool, guild_id: &str) -> Result<Option<DbGuild>> {
|
pub async fn get(db: &PgPool, guild_id: &str) -> Result<Option<DbGuild>, sqlx::Error> {
|
||||||
let guild: Option<DbGuild> = query_as!(
|
let guild: Option<DbGuild> = query_as!(
|
||||||
DbGuild,
|
DbGuild,
|
||||||
"SELECT * FROM guilds WHERE guild_id = $1",
|
"SELECT * FROM guilds WHERE guild_id = $1",
|
||||||
|
|
@ -74,7 +73,7 @@ pub async fn get(db: &PgPool, guild_id: &str) -> Result<Option<DbGuild>> {
|
||||||
Ok(guild)
|
Ok(guild)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_log(db: &PgPool, guild_id: &str, asked: LogChannel) -> Result<Option<String>> {
|
pub async fn get_log(db: &PgPool, guild_id: &str, asked: LogChannel) -> Result<Option<String>, sqlx::Error> {
|
||||||
let sql: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", log_select(asked));
|
let sql: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", log_select(asked));
|
||||||
let channel_id: Option<String> = query_scalar(&sql)
|
let channel_id: Option<String> = query_scalar(&sql)
|
||||||
.bind(guild_id)
|
.bind(guild_id)
|
||||||
|
|
@ -83,7 +82,7 @@ pub async fn get_log(db: &PgPool, guild_id: &str, asked: LogChannel) -> Result<O
|
||||||
Ok(channel_id)
|
Ok(channel_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_log(db: &PgPool, guild_id: &str, asked: LogChannel, value: &str) -> Result<()> {
|
pub async fn set_log(db: &PgPool, guild_id: &str, asked: LogChannel, value: &str) -> Result<(), sqlx::Error> {
|
||||||
let sql: String = format!("UPDATE guilds set {} = $1 WHERE guild_id = $2", log_select(asked));
|
let sql: String = format!("UPDATE guilds set {} = $1 WHERE guild_id = $2", log_select(asked));
|
||||||
query(&sql)
|
query(&sql)
|
||||||
.bind(value)
|
.bind(value)
|
||||||
|
|
@ -93,7 +92,7 @@ pub async fn set_log(db: &PgPool, guild_id: &str, asked: LogChannel, value: &str
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_protect(db: &PgPool, guild_id: &str, asked: Protect) -> Result<Option<bool>> {
|
pub async fn get_protect(db: &PgPool, guild_id: &str, asked: Protect) -> Result<Option<bool>, sqlx::Error> {
|
||||||
let sql: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", protect_select(asked));
|
let sql: String = format!("SELECT {} FROM guilds WHERE guild_id = $1", protect_select(asked));
|
||||||
let state: Option<bool> = query_scalar(&sql)
|
let state: Option<bool> = query_scalar(&sql)
|
||||||
.bind(guild_id)
|
.bind(guild_id)
|
||||||
|
|
@ -102,7 +101,7 @@ pub async fn get_protect(db: &PgPool, guild_id: &str, asked: Protect) -> Result<
|
||||||
Ok(state)
|
Ok(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_protect(db: &PgPool, guild_id: &str, asked: Protect, value: &str) -> Result<()> {
|
pub async fn set_protect(db: &PgPool, guild_id: &str, asked: Protect, value: &str) -> Result<(), sqlx::Error> {
|
||||||
let sql: String = format!("UPDATE guilds set {} = $1 WHERE guild_id = $2", protect_select(asked));
|
let sql: String = format!("UPDATE guilds set {} = $1 WHERE guild_id = $2", protect_select(asked));
|
||||||
query(&sql)
|
query(&sql)
|
||||||
.bind(value)
|
.bind(value)
|
||||||
|
|
@ -112,9 +111,9 @@ pub async fn set_protect(db: &PgPool, guild_id: &str, asked: Protect, value: &st
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_or_create(db: &PgPool, guild_id: &str) -> Result<DbGuild> {
|
pub async fn get_or_create(db: &PgPool, guild_id: &str) -> Result<DbGuild, sqlx::Error> {
|
||||||
create(db, guild_id).await?;
|
create(db, guild_id).await?;
|
||||||
get(db, guild_id)
|
get(db, guild_id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| sqlx::Error::RowNotFound.into())
|
.ok_or_else(|| sqlx::Error::RowNotFound)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
use sqlx::{PgPool, query, query_as, query_scalar};
|
use sqlx::{PgPool, query, query_as, query_scalar};
|
||||||
use crate::models::guild_user::DbGuildUser;
|
use crate::models::guild_user::DbGuildUser;
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<Option<DbGuildUser>> {
|
) -> Result<Option<DbGuildUser>, sqlx::Error> {
|
||||||
let guild_user = query_as!(
|
let guild_user = query_as!(
|
||||||
DbGuildUser,
|
DbGuildUser,
|
||||||
"SELECT * FROM guild_users WHERE user_id = $1 AND guild_id = $2",
|
"SELECT * FROM guild_users WHERE user_id = $1 AND guild_id = $2",
|
||||||
|
|
@ -22,7 +21,7 @@ pub async fn create(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"INSERT INTO guild_users (user_id, guild_id) \
|
"INSERT INTO guild_users (user_id, guild_id) \
|
||||||
VALUES ($1, $2) \
|
VALUES ($1, $2) \
|
||||||
|
|
@ -39,7 +38,7 @@ pub async fn delete(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"DELETE FROM guild_users WHERE user_id = $1 AND guild_id = $2",
|
"DELETE FROM guild_users WHERE user_id = $1 AND guild_id = $2",
|
||||||
user_id,
|
user_id,
|
||||||
|
|
@ -53,7 +52,7 @@ pub async fn delete(
|
||||||
pub async fn delete_all_in_guild(
|
pub async fn delete_all_in_guild(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!("DELETE FROM guild_users WHERE guild_id = $1", guild_id)
|
query!("DELETE FROM guild_users WHERE guild_id = $1", guild_id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -65,7 +64,7 @@ pub async fn add_xp(
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
amount: i32,
|
amount: i32,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE guild_users SET xp = xp + $1 \
|
"UPDATE guild_users SET xp = xp + $1 \
|
||||||
WHERE user_id = $2 AND guild_id = $3",
|
WHERE user_id = $2 AND guild_id = $3",
|
||||||
|
|
@ -83,7 +82,7 @@ pub async fn set_level(
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
level: i32,
|
level: i32,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE guild_users SET level = $1 \
|
"UPDATE guild_users SET level = $1 \
|
||||||
WHERE user_id = $2 AND guild_id = $3",
|
WHERE user_id = $2 AND guild_id = $3",
|
||||||
|
|
@ -100,7 +99,7 @@ pub async fn get_xp(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<Option<i32>> {
|
) -> Result<Option<i32>, sqlx::Error> {
|
||||||
let xp: Option<i32> = query_scalar(
|
let xp: Option<i32> = query_scalar(
|
||||||
"SELECT xp FROM guild_users WHERE user_id = $1 AND guild_id = $2",
|
"SELECT xp FROM guild_users WHERE user_id = $1 AND guild_id = $2",
|
||||||
)
|
)
|
||||||
|
|
@ -115,7 +114,7 @@ pub async fn get_level(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<Option<i32>> {
|
) -> Result<Option<i32>, sqlx::Error> {
|
||||||
let level: Option<i32> = query_scalar("SELECT level FROM guild_users WHERE user_id = $1 AND guild_id = $2")
|
let level: Option<i32> = query_scalar("SELECT level FROM guild_users WHERE user_id = $1 AND guild_id = $2")
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.bind(guild_id)
|
.bind(guild_id)
|
||||||
|
|
@ -129,7 +128,7 @@ pub async fn set_wl(
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
value: bool,
|
value: bool,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE guild_users SET is_wl_user = $1 \
|
"UPDATE guild_users SET is_wl_user = $1 \
|
||||||
WHERE user_id = $2 AND guild_id = $3",
|
WHERE user_id = $2 AND guild_id = $3",
|
||||||
|
|
@ -145,7 +144,7 @@ pub async fn set_wl(
|
||||||
pub async fn get_all_wl(
|
pub async fn get_all_wl(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<Vec<DbGuildUser>> {
|
) -> Result<Vec<DbGuildUser>, sqlx::Error> {
|
||||||
let users = query_as!(
|
let users = query_as!(
|
||||||
DbGuildUser,
|
DbGuildUser,
|
||||||
"SELECT * FROM guild_users \
|
"SELECT * FROM guild_users \
|
||||||
|
|
@ -162,7 +161,7 @@ pub async fn set_invited_by(
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
inviter_id: &str,
|
inviter_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE guild_users SET invited_by = $1 \
|
"UPDATE guild_users SET invited_by = $1 \
|
||||||
WHERE user_id = $2 AND guild_id = $3",
|
WHERE user_id = $2 AND guild_id = $3",
|
||||||
|
|
@ -179,7 +178,7 @@ pub async fn increment_invitations(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE guild_users SET invitation_count = invitation_count + 1 \
|
"UPDATE guild_users SET invitation_count = invitation_count + 1 \
|
||||||
WHERE user_id = $1 AND guild_id = $2",
|
WHERE user_id = $1 AND guild_id = $2",
|
||||||
|
|
@ -195,7 +194,7 @@ pub async fn decrement_invitations(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
user_id: &str,
|
user_id: &str,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<()> {
|
) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE guild_users SET invitation_count = GREATEST(invitation_count - 1, 0) \
|
"UPDATE guild_users SET invitation_count = GREATEST(invitation_count - 1, 0) \
|
||||||
WHERE user_id = $1 AND guild_id = $2",
|
WHERE user_id = $1 AND guild_id = $2",
|
||||||
|
|
@ -211,7 +210,7 @@ pub async fn leaderboard_xp(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
limit: i64,
|
limit: i64,
|
||||||
) -> Result<Vec<DbGuildUser>> {
|
) -> Result<Vec<DbGuildUser>, sqlx::Error> {
|
||||||
let users = query_as!(
|
let users = query_as!(
|
||||||
DbGuildUser,
|
DbGuildUser,
|
||||||
"SELECT * FROM guild_users \
|
"SELECT * FROM guild_users \
|
||||||
|
|
@ -230,7 +229,7 @@ pub async fn leaderboard_invitations(
|
||||||
db: &PgPool,
|
db: &PgPool,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
limit: i64,
|
limit: i64,
|
||||||
) -> Result<Vec<DbGuildUser>> {
|
) -> Result<Vec<DbGuildUser>, sqlx::Error> {
|
||||||
let users = query_as!(
|
let users = query_as!(
|
||||||
DbGuildUser,
|
DbGuildUser,
|
||||||
"SELECT * FROM guild_users \
|
"SELECT * FROM guild_users \
|
||||||
|
|
@ -244,9 +243,9 @@ pub async fn leaderboard_invitations(
|
||||||
.await?;
|
.await?;
|
||||||
Ok(users)
|
Ok(users)
|
||||||
}
|
}
|
||||||
pub async fn get_or_create(db: &PgPool, user_id: &str, guild_id: &str) -> Result<DbGuildUser> {
|
pub async fn get_or_create(db: &PgPool, user_id: &str, guild_id: &str) -> Result<DbGuildUser, sqlx::Error> {
|
||||||
create(db, user_id, guild_id).await?;
|
create(db, user_id, guild_id).await?;
|
||||||
get(db, user_id, guild_id)
|
get(db, user_id, guild_id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| sqlx::Error::RowNotFound.into())
|
.ok_or_else(|| sqlx::Error::RowNotFound)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ use sqlx::{
|
||||||
query_as,
|
query_as,
|
||||||
};
|
};
|
||||||
use crate::models::DbUser;
|
use crate::models::DbUser;
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
/// Adding the user (if exist do nothing)
|
/// Adding the user (if exist do nothing)
|
||||||
///
|
///
|
||||||
|
|
@ -13,8 +12,8 @@ use anyhow::Result;
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns `Error` if the query fails.
|
/// Returns `sqlx::Error` if the query fails.
|
||||||
pub async fn create(db: &PgPool, user_id: &str) -> Result<()> {
|
pub async fn create(db: &PgPool, user_id: &str) -> Result<(), sqlx::Error> {
|
||||||
query!("INSERT INTO users (user_id) VALUES ($1) ON CONFLICT DO NOTHING", user_id)
|
query!("INSERT INTO users (user_id) VALUES ($1) ON CONFLICT DO NOTHING", user_id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -33,8 +32,8 @@ pub async fn create(db: &PgPool, user_id: &str) -> Result<()> {
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns `Error` if the query fails.
|
/// Returns `sqlx::Error` if the query fails.
|
||||||
pub async fn get(db: &PgPool, user_id: &str) -> Result<Option<DbUser>> {
|
pub async fn get(db: &PgPool, user_id: &str) -> Result<Option<DbUser>, sqlx::Error> {
|
||||||
let user: Option<DbUser> = query_as!(
|
let user: Option<DbUser> = query_as!(
|
||||||
DbUser,
|
DbUser,
|
||||||
"SELECT * FROM users WHERE user_id = $1",
|
"SELECT * FROM users WHERE user_id = $1",
|
||||||
|
|
@ -53,8 +52,8 @@ pub async fn get(db: &PgPool, user_id: &str) -> Result<Option<DbUser>> {
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns `Error` if the query fails.
|
/// Returns `sqlx::Error` if the query fails.
|
||||||
pub async fn set_owner(db: &PgPool, user_id: &str, value: bool) -> Result<()> {
|
pub async fn set_owner(db: &PgPool, user_id: &str, value: bool) -> Result<(), sqlx::Error> {
|
||||||
query!("UPDATE users set is_owner = $1 WHERE user_id = $2", value, user_id)
|
query!("UPDATE users set is_owner = $1 WHERE user_id = $2", value, user_id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
@ -69,8 +68,8 @@ pub async fn set_owner(db: &PgPool, user_id: &str, value: bool) -> Result<()> {
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns `Error` if the query fails.
|
/// Returns `sqlx::Error` if the query fails.
|
||||||
pub async fn set_buyer(db: &PgPool, user_id: &str, value: bool) -> Result<()> {
|
pub async fn set_buyer(db: &PgPool, user_id: &str, value: bool) -> Result<(), sqlx::Error> {
|
||||||
query!(
|
query!(
|
||||||
"UPDATE users set is_buyer = $1 WHERE user_id = $2",
|
"UPDATE users set is_buyer = $1 WHERE user_id = $2",
|
||||||
value,
|
value,
|
||||||
|
|
@ -81,9 +80,9 @@ pub async fn set_buyer(db: &PgPool, user_id: &str, value: bool) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_or_create(db: &PgPool, user_id: &str) -> Result<DbUser> {
|
pub async fn get_or_create(db: &PgPool, user_id: &str) -> Result<DbUser, sqlx::Error> {
|
||||||
create(db, user_id).await?;
|
create(db, user_id).await?;
|
||||||
get(db, user_id)
|
get(db, user_id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| anyhow::anyhow!("Not able to get or create the user"))
|
.ok_or_else(|| sqlx::Error::RowNotFound)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use serenity::all::{Context, Interaction};
|
use serenity::all::*;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use crate::commands::SlashCommand;
|
use crate::commands::SlashCommand;
|
||||||
use crate::config::EmojiConfig;
|
use crate::config::EmojiConfig;
|
||||||
|
|
|
||||||
|
|
@ -11,14 +11,13 @@ use crate::models::bot::{
|
||||||
BotActivity,
|
BotActivity,
|
||||||
BotPresence
|
BotPresence
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
pub struct ReadyHandler;
|
pub struct ReadyHandler;
|
||||||
|
|
||||||
async fn fetch_all_members(
|
async fn fetch_all_members(
|
||||||
ctx: &Context,
|
ctx: &Context,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
) -> Result<Vec<Member>> {
|
) -> Result<Vec<Member>, serenity::Error> {
|
||||||
let mut all_members: Vec<Member> = Vec::new();
|
let mut all_members: Vec<Member> = Vec::new();
|
||||||
let mut last_id: Option<UserId> = None;
|
let mut last_id: Option<UserId> = None;
|
||||||
|
|
||||||
|
|
|
||||||
14
src/main.rs
14
src/main.rs
|
|
@ -5,22 +5,22 @@ mod events;
|
||||||
mod models;
|
mod models;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use dotenvy::dotenv;
|
|
||||||
use events::Bot;
|
use events::Bot;
|
||||||
use serenity::Client;
|
use serenity::all::*;
|
||||||
use serenity::all::GatewayIntents;
|
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use sqlx::{Pool, Postgres, migrate};
|
use sqlx::{Pool, Postgres, migrate};
|
||||||
use std::env;
|
use std::env;
|
||||||
use tracing::{error, info};
|
use tracing::{
|
||||||
use tracing_subscriber::fmt;
|
info,
|
||||||
|
error
|
||||||
|
};
|
||||||
|
|
||||||
use self::config::emoji::EmojiConfig;
|
use self::config::emoji::EmojiConfig;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
dotenv().ok();
|
dotenvy::dotenv().ok();
|
||||||
|
|
||||||
let token: String =
|
let token: String =
|
||||||
env::var("DISCORD_TOKEN").expect("❌ | DISCORD_TOKEN missing (check the env file)");
|
env::var("DISCORD_TOKEN").expect("❌ | DISCORD_TOKEN missing (check the env file)");
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
use anyhow::Result;
|
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
PgPool,
|
PgPool,
|
||||||
query_scalar,
|
query_scalar,
|
||||||
};
|
};
|
||||||
use tracing::warn;
|
|
||||||
|
|
||||||
pub async fn is_whitelist(db: &PgPool, user_id: &str, guild_id: &str) -> Result<bool> {
|
pub async fn is_whitelist(db: &PgPool, user_id: &str, guild_id: &str) -> Result<bool, sqlx::Error> {
|
||||||
let result: Option<bool> = query_scalar(
|
let result: Option<bool> = query_scalar(
|
||||||
"SELECT TRUE FROM guild_users gu \
|
"SELECT TRUE FROM guild_users gu \
|
||||||
LEFT JOIN users u ON u.user_id = gu.user_id \
|
LEFT JOIN users u ON u.user_id = gu.user_id \
|
||||||
|
|
@ -24,19 +22,9 @@ pub async fn is_whitelist(db: &PgPool, user_id: &str, guild_id: &str) -> Result<
|
||||||
Ok(result.is_some())
|
Ok(result.is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn is_owner(db: &PgPool, user_id: &str) -> Result<bool> {
|
pub async fn is_owner(db: &PgPool, user_id: &str) -> Result<bool, sqlx::Error> {
|
||||||
let result: bool = query_scalar(
|
|
||||||
"SELECT is_dev, is_buyer, is_owner FROM users WHERE user_id = $1 AND (is_dev = true OR is_buyer = true OR is_owner = true)",
|
|
||||||
)
|
|
||||||
.bind(user_id)
|
|
||||||
.fetch_one(db)
|
|
||||||
.await?;
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn is_buyer(db: &PgPool, user_id: &str) -> Result<bool> {
|
|
||||||
let result: Option<bool> = query_scalar(
|
let result: Option<bool> = query_scalar(
|
||||||
"SELECT is_dev, is_buyer FROM users WHERE user_id = $1 AND (is_dev = true OR is_buyer = true)",
|
"SELECT TRUE FROM users WHERE user_id = $1 AND is_dev = true OR is_buyer = true OR is_owner = true",
|
||||||
)
|
)
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
|
|
@ -44,9 +32,19 @@ pub async fn is_buyer(db: &PgPool, user_id: &str) -> Result<bool> {
|
||||||
Ok(result.is_some())
|
Ok(result.is_some())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn is_dev(db: &PgPool, user_id: &str) -> Result<bool> {
|
pub async fn is_buyer(db: &PgPool, user_id: &str) -> Result<bool, sqlx::Error> {
|
||||||
let result: Option<bool> = query_scalar(
|
let result: Option<bool> = query_scalar(
|
||||||
"SELECT is_dev FROM users WHERE user_id = $1 AND is_dev = true",
|
"SELECT TRUE FROM users WHERE user_id = $1 AND is_dev = true OR is_buyer = true",
|
||||||
|
)
|
||||||
|
.bind(user_id)
|
||||||
|
.fetch_optional(db)
|
||||||
|
.await?;
|
||||||
|
Ok(result.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn is_dev(db: &PgPool, user_id: &str) -> Result<bool, sqlx::Error> {
|
||||||
|
let result: Option<bool> = query_scalar(
|
||||||
|
"SELECT TRUE FROM users WHERE user_id = $1 AND is_dev = true",
|
||||||
)
|
)
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue