Compare commits

..

5 Commits

Author SHA1 Message Date
Azur bdfa7edf52 feature shadow ban 2026-05-14 15:18:02 +02:00
Azur 12f3400164 change help command description 2026-05-11 21:48:52 +02:00
Azur 3fe5b47856 Add status and activity to the bot 2026-05-11 21:32:15 +02:00
Azur 40c8203849 add database connection and event_handler to the client 2026-05-11 21:30:10 +02:00
Azur 93c57f69df add foreign ref to database 2026-05-11 21:26:24 +02:00
7 changed files with 261 additions and 17 deletions
Generated
+1
View File
@@ -1029,6 +1029,7 @@ dependencies = [
"poise",
"serenity",
"sqlx",
"thiserror 2.0.18",
"tokio",
"tracing-subscriber",
]
+1
View File
@@ -8,5 +8,6 @@ dotenvy = "0.15.7"
poise = "0.6.2"
serenity = { version = "0.12.5", features = ["cache"] }
sqlx = { version = "0.8.6", features = ["any", "mysql", "runtime-tokio", "sqlite"] }
thiserror = "2.0.18"
tokio = { version = "1.52.3", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = "0.3.23"
+24 -5
View File
@@ -1,6 +1,7 @@
-- Add migration script here
create TABLE `Guild` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY
`id` INT UNSIGNED NOT NULL PRIMARY KEY,
`shadow_ban_role` INT UNSIGNED
);
create TABLE `User` (
@@ -9,7 +10,8 @@ create TABLE `User` (
create TABLE `Channel` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY,
`guild_id` INT UNSIGNED NOT NULL
`guild_id` INT UNSIGNED NOT NULL,
FOREIGN KEY(guild_id) REFERENCES Guild(id)
);
create TABLE `Logs` (
@@ -20,10 +22,27 @@ create TABLE `Logs` (
`message_update` BOOLEAN DEFAULT FALSE,
`message_delete` BOOLEAN DEFAULT FALSE,
`voice_join` BOOLEAN DEFAULT FALSE,
`voice_quit` BOOLEAN DEFAULT FALSE
`voice_quit` BOOLEAN DEFAULT FALSE,
FOREIGN KEY(guild_id) REFERENCES Guild(id),
FOREIGN KEY(channel_id) REFERENCES Channel(id)
);
create TABLE `Auto_Channel` (
`channel_id` INT UNSIGNED NOT NULL PRIMARY KEY,
`category_id` INT UNSIGNED
)
`category_id` INT UNSIGNED,
FOREIGN KEY(channel_id) REFERENCES User(id)
);
create TABLE `Shadow_Ban` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`guild_id` INT UNSIGNED NOT NULL,
`user_id` INT UNSIGNED NOT NULL,
FOREIGN KEY(guild_id) REFERENCES Guild(id),
FOREIGN KEY(user_id) REFERENCES User(id)
);
create TABLE `Shadow_Ban_Role` (
`shadow_ban_id` INTEGER NOT NULL,
`role_id` INT UNSIGNED NOT NULL,
FOREIGN KEY(shadow_ban_id) REFERENCES Shadow_Ban(id)
);
+168 -1
View File
@@ -1,5 +1,34 @@
use crate::{Context, Error};
use std::convert::Infallible;
use poise::CreateReply;
use serenity::{
builder::{CreateEmbed, EditRole},
cache::CacheRef,
futures::future::join_all,
http::Http,
model::{
self, Permissions,
channel::{Embed, PermissionOverwrite, PermissionOverwriteType},
guild::Guild,
id::{GuildId, RoleId},
user::User,
},
};
use thiserror::Error;
use tokio::join;
use crate::{Context, Error, database::DataBase};
#[derive(Error, Debug)]
pub enum BotError {
#[error("Error related to the database")]
DataBaseError(#[from] sqlx::Error),
#[error("Error related to serenity")]
SerenityError(#[from] serenity::Error),
}
/// ️ Display all commands and their uses
///
/// Show this help menu
#[poise::command(slash_command)]
pub async fn help(
@@ -18,3 +47,141 @@ pub async fn help(
.await?;
Ok(())
}
#[poise::command(
slash_command,
subcommands("shadowban_ban", "shadowban_unban", "shadowban_setup")
)]
pub async fn shadowban(_ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}
#[poise::command(
slash_command,
required_permissions = "BAN_MEMBERS",
rename = "ban",
guild_only
)]
/// Remove the access of all channels to an user
pub async fn shadowban_ban(
ctx: Context<'_>,
#[description = "The user to shadow ban"] user: User,
) -> Result<(), Error> {
if let Some(guild) = ctx.guild_id() {
let role_id = ctx.data().database.get_shadow_ban_role(guild).await;
let role = if let Some(role_id) = role_id {
role_id
} else {
shadowban_setup_internal(&ctx.data().database, ctx.http(), guild, None).await?
};
guild
.member(ctx.http(), user.id)
.await?
.add_role(ctx.http(), role)
.await?;
} else {
let embed = CreateEmbed::new()
.title("Error")
.description("This command must execute in a guild.");
ctx.send(CreateReply {
embeds: vec![embed],
..Default::default()
})
.await?;
}
Ok(())
}
/// Remove a previous shadow ban
#[poise::command(
slash_command,
required_permissions = "BAN_MEMBERS",
rename = "unban",
guild_only
)]
pub async fn shadowban_unban(
ctx: Context<'_>,
#[description = "The user to remove the shadow ban role"] user: User,
) -> Result<(), Error> {
if let Some(guild) = ctx.guild_id() {
let role_id = ctx.data().database.get_shadow_ban_role(guild).await;
let role = if let Some(role_id) = role_id {
role_id
} else {
shadowban_setup_internal(&ctx.data().database, ctx.http(), guild, None).await?
};
guild
.member(ctx.http(), user.id)
.await?
.remove_role(ctx.http(), role)
.await?;
} else {
let embed = CreateEmbed::new()
.title("Error")
.description("This command must execute in a guild.");
ctx.send(CreateReply {
embeds: vec![embed],
..Default::default()
})
.await?;
}
Ok(())
}
#[poise::command(
slash_command,
required_permissions = "BAN_MEMBERS",
rename = "setup",
guild_only
)]
pub async fn shadowban_setup(ctx: Context<'_>, role: Option<RoleId>) -> Result<(), Error> {
ctx.defer_ephemeral().await?;
if let Some(guild) = ctx.guild_id() {
shadowban_setup_internal(&ctx.data().database, ctx.http(), guild, role).await?;
} else {
let embed = CreateEmbed::new()
.title("Error")
.description("This command must execute in a guild.");
ctx.send(CreateReply {
embeds: vec![embed],
..Default::default()
})
.await?;
}
Ok(())
}
async fn shadowban_setup_internal(
database: &DataBase,
http: &Http,
guild: GuildId,
role: Option<RoleId>,
) -> Result<RoleId, BotError> {
let role_id = if let Some(role_id) = role {
role_id
} else {
let role = EditRole::new()
.colour(0x000000)
.name("Shadow Ban")
.hoist(true)
.mentionable(false)
.permissions(Permissions::empty());
let role = guild.create_role(http, role).await?;
role.id
};
database.set_shadow_ban_role(guild, role_id).await?;
let channels = guild.channels(http).await?;
let perms = PermissionOverwrite {
allow: Permissions::empty(),
deny: Permissions::VIEW_CHANNEL.union(Permissions::SEND_MESSAGES),
kind: PermissionOverwriteType::Role(role_id),
};
for (_, channel) in channels {
channel.create_permission(http, perms.clone()).await.ok();
}
Ok(role_id)
}
+29 -1
View File
@@ -1,9 +1,10 @@
use std::env::var;
use serenity::model::id::{GuildId, RoleId};
use sqlx::any::install_default_drivers;
use sqlx::migrate::Migrator;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::{Error, SqlitePool};
use sqlx::{Error, SqlitePool, query};
static MIGRATOR: Migrator = sqlx::migrate!();
@@ -24,4 +25,31 @@ impl DataBase {
Ok(Self { pool: pool })
}
pub async fn set_shadow_ban_role(&self, guild: GuildId, role: RoleId) -> Result<(), Error> {
let guild = guild.get() as i64;
let role = role.get() as i64;
query(
"INSERT INTO Guild (id, shadow_ban_role)
VALUES (?1,?2)
ON CONFLICT(id) DO UPDATE SET
shadow_ban_role = excluded.shadow_ban_role;",
)
.bind(guild)
.bind(role)
.execute(&self.pool)
.await?;
Ok(())
}
pub async fn get_shadow_ban_role(&self, guild: GuildId) -> Option<RoleId> {
let guild = guild.get() as i64;
let role = query!("SELECT (shadow_ban_role) FROM Guild where id = ?;", guild)
.fetch_one(&self.pool)
.await
.map(|x| x.shadow_ban_role)
.unwrap_or(None);
role.map(|x| RoleId::new(x as u64))
}
}
+2 -2
View File
@@ -2,7 +2,7 @@ use poise::serenity_prelude as serenity;
use crate::{Data, Error};
async fn event_handler(
pub async fn event_handler(
ctx: &serenity::Context,
event: &serenity::FullEvent,
_framework: poise::FrameworkContext<'_, Data, Error>,
@@ -10,7 +10,7 @@ async fn event_handler(
) -> Result<(), Error> {
match event {
serenity::FullEvent::Ready { data_about_bot, .. } => {
println!("Logged in as {}", data_about_bot.user.name);
println!("Logged in as {} :3", data_about_bot.user.name);
}
_ => {}
}
+36 -8
View File
@@ -2,16 +2,21 @@ mod commands;
mod database;
mod events;
use ::serenity::{gateway::ActivityData, model::id::UserId};
use dotenvy::dotenv;
use poise::serenity_prelude as serenity;
use std::env::var;
use std::{collections::HashSet, env::var};
use crate::{database::DataBase, events::event_handler};
// Types used by all command functions
type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
// Custom user data passed to all command functions
pub struct Data {}
pub struct Data {
database: DataBase,
}
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
// This is our custom error handler
@@ -35,10 +40,26 @@ async fn main() {
tracing_subscriber::fmt::init();
dotenv().ok();
let database = database::DataBase::setup_database()
let database = DataBase::setup_database()
.await
.expect("Failed setup the database");
let owners = var("OWNERS");
if let Err(_) = owners {
println!("⚠️ Missing `OWNERS` env var, see README for more information.");
}
let owners = owners.unwrap_or(String::from(""));
let owners: HashSet<UserId> = owners
.split(" ")
.filter_map(|s| {
let some_number = s.parse().ok();
some_number.map(UserId::new)
})
.collect();
// FrameworkOptions contains all of poise's configuration option in one struct
// Every option can be omitted to use its default value
let options = poise::FrameworkOptions {
@@ -46,15 +67,19 @@ async fn main() {
// The global error handler for all error cases that may occur
on_error: |error| Box::pin(on_error(error)),
event_handler: |ctx, event, framework, data| {
Box::pin(event_handler(ctx, event, framework, data))
},
owners: owners,
..Default::default()
};
let framework = poise::Framework::builder()
.setup(move |ctx, ready, framework| {
Box::pin(async move {
println!("Logged in as {}", ready.user.name);
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {})
Ok(Data { database })
})
})
.options(options)
@@ -66,9 +91,12 @@ async fn main() {
let intents =
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
let client = serenity::ClientBuilder::new(token, intents)
let mut client = serenity::ClientBuilder::new(token, intents)
.framework(framework)
.await;
.status(serenity::OnlineStatus::Idle)
.activity(ActivityData::playing("next station ⭐ /help"))
.await
.unwrap();
client.unwrap().start().await.unwrap()
client.start().await.unwrap()
}