Compare commits
5 Commits
fee9cf488f
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
bdfa7edf52
|
|||
|
12f3400164
|
|||
|
3fe5b47856
|
|||
|
40c8203849
|
|||
|
93c57f69df
|
Generated
+1
@@ -1029,6 +1029,7 @@ dependencies = [
|
|||||||
"poise",
|
"poise",
|
||||||
"serenity",
|
"serenity",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
"thiserror 2.0.18",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -8,5 +8,6 @@ dotenvy = "0.15.7"
|
|||||||
poise = "0.6.2"
|
poise = "0.6.2"
|
||||||
serenity = { version = "0.12.5", features = ["cache"] }
|
serenity = { version = "0.12.5", features = ["cache"] }
|
||||||
sqlx = { version = "0.8.6", features = ["any", "mysql", "runtime-tokio", "sqlite"] }
|
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"] }
|
tokio = { version = "1.52.3", features = ["macros", "rt-multi-thread"] }
|
||||||
tracing-subscriber = "0.3.23"
|
tracing-subscriber = "0.3.23"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
-- Add migration script here
|
-- Add migration script here
|
||||||
create TABLE `Guild` (
|
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` (
|
create TABLE `User` (
|
||||||
@@ -9,7 +10,8 @@ create TABLE `User` (
|
|||||||
|
|
||||||
create TABLE `Channel` (
|
create TABLE `Channel` (
|
||||||
`id` INT UNSIGNED NOT NULL PRIMARY KEY,
|
`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` (
|
create TABLE `Logs` (
|
||||||
@@ -20,10 +22,27 @@ create TABLE `Logs` (
|
|||||||
`message_update` BOOLEAN DEFAULT FALSE,
|
`message_update` BOOLEAN DEFAULT FALSE,
|
||||||
`message_delete` BOOLEAN DEFAULT FALSE,
|
`message_delete` BOOLEAN DEFAULT FALSE,
|
||||||
`voice_join` 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` (
|
create TABLE `Auto_Channel` (
|
||||||
`channel_id` INT UNSIGNED NOT NULL PRIMARY KEY,
|
`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
@@ -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
|
/// Show this help menu
|
||||||
#[poise::command(slash_command)]
|
#[poise::command(slash_command)]
|
||||||
pub async fn help(
|
pub async fn help(
|
||||||
@@ -18,3 +47,141 @@ pub async fn help(
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
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
@@ -1,9 +1,10 @@
|
|||||||
use std::env::var;
|
use std::env::var;
|
||||||
|
|
||||||
|
use serenity::model::id::{GuildId, RoleId};
|
||||||
use sqlx::any::install_default_drivers;
|
use sqlx::any::install_default_drivers;
|
||||||
use sqlx::migrate::Migrator;
|
use sqlx::migrate::Migrator;
|
||||||
use sqlx::sqlite::SqlitePoolOptions;
|
use sqlx::sqlite::SqlitePoolOptions;
|
||||||
use sqlx::{Error, SqlitePool};
|
use sqlx::{Error, SqlitePool, query};
|
||||||
|
|
||||||
static MIGRATOR: Migrator = sqlx::migrate!();
|
static MIGRATOR: Migrator = sqlx::migrate!();
|
||||||
|
|
||||||
@@ -24,4 +25,31 @@ impl DataBase {
|
|||||||
|
|
||||||
Ok(Self { pool: pool })
|
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
@@ -2,7 +2,7 @@ use poise::serenity_prelude as serenity;
|
|||||||
|
|
||||||
use crate::{Data, Error};
|
use crate::{Data, Error};
|
||||||
|
|
||||||
async fn event_handler(
|
pub async fn event_handler(
|
||||||
ctx: &serenity::Context,
|
ctx: &serenity::Context,
|
||||||
event: &serenity::FullEvent,
|
event: &serenity::FullEvent,
|
||||||
_framework: poise::FrameworkContext<'_, Data, Error>,
|
_framework: poise::FrameworkContext<'_, Data, Error>,
|
||||||
@@ -10,7 +10,7 @@ async fn event_handler(
|
|||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
match event {
|
match event {
|
||||||
serenity::FullEvent::Ready { data_about_bot, .. } => {
|
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
@@ -2,16 +2,21 @@ mod commands;
|
|||||||
mod database;
|
mod database;
|
||||||
mod events;
|
mod events;
|
||||||
|
|
||||||
|
use ::serenity::{gateway::ActivityData, model::id::UserId};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use poise::serenity_prelude as serenity;
|
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
|
// Types used by all command functions
|
||||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||||
|
|
||||||
// Custom user data passed to all command functions
|
// 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>) {
|
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
||||||
// This is our custom error handler
|
// This is our custom error handler
|
||||||
@@ -35,10 +40,26 @@ async fn main() {
|
|||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
let database = database::DataBase::setup_database()
|
let database = DataBase::setup_database()
|
||||||
.await
|
.await
|
||||||
.expect("Failed setup the database");
|
.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
|
// FrameworkOptions contains all of poise's configuration option in one struct
|
||||||
// Every option can be omitted to use its default value
|
// Every option can be omitted to use its default value
|
||||||
let options = poise::FrameworkOptions {
|
let options = poise::FrameworkOptions {
|
||||||
@@ -46,15 +67,19 @@ async fn main() {
|
|||||||
// The global error handler for all error cases that may occur
|
// The global error handler for all error cases that may occur
|
||||||
on_error: |error| Box::pin(on_error(error)),
|
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()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let framework = poise::Framework::builder()
|
let framework = poise::Framework::builder()
|
||||||
.setup(move |ctx, ready, framework| {
|
.setup(move |ctx, ready, framework| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
println!("Logged in as {}", ready.user.name);
|
|
||||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||||
Ok(Data {})
|
Ok(Data { database })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.options(options)
|
.options(options)
|
||||||
@@ -66,9 +91,12 @@ async fn main() {
|
|||||||
let intents =
|
let intents =
|
||||||
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
|
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)
|
.framework(framework)
|
||||||
.await;
|
.status(serenity::OnlineStatus::Idle)
|
||||||
|
.activity(ActivityData::playing("next station ⭐ /help"))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
client.unwrap().start().await.unwrap()
|
client.start().await.unwrap()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user