feat: Setup preflight check to be sure that user is admin ++ add event
to set activity and get basic data like nbr of guild ++ add ascii art because why the hell not ?
This commit is contained in:
parent
eada83fe4a
commit
7f8aebb1e5
10
apps/bot/src/banner.art
Normal file
10
apps/bot/src/banner.art
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
███▄ ▄███▓ ██▓ ▒█████ ███▄ █ ▓█████ ▒█████ ██▓ ▄▄▄ ██████
|
||||||
|
▓██▒▀█▀ ██▒▓██▒▒██▒ ██▒ ██ ▀█ █ ▓█ ▀ ▒██▒ ██▒▓██▒ ▒████▄ ▒██ ▒
|
||||||
|
▓██ ▓██░▒██▒▒██░ ██▒▓██ ▀█ ██▒▒███ ▒██░ ██▒▒██░ ▒██ ▀█▄ ░ ▓██▄
|
||||||
|
▒██ ▒██ ░██░▒██ ██░▓██▒ ▐▌██▒▒▓█ ▄ ▒██ ██░▒██░ ░██▄▄▄▄██ ▒ ██▒
|
||||||
|
▒██▒ ░██▒░██░░ ████▓▒░▒██░ ▓██░░▒████▒░ ████▓▒░░██████▒▓█ ▓██▒▒██████▒▒
|
||||||
|
░ ▒░ ░ ░░▓ ░ ▒░▒░▒░ ░ ▒░ ▒ ▒ ░░ ▒░ ░░ ▒░▒░▒░ ░ ▒░▓ ░▒▒ ▓▒█░▒ ▒▓▒ ▒ ░
|
||||||
|
░ ░ ░ ▒ ░ ░ ▒ ▒░ ░ ░░ ░ ▒░ ░ ░ ░ ░ ▒ ▒░ ░ ░ ▒ ░ ▒ ▒▒ ░░ ░▒ ░ ░
|
||||||
|
░ ░ ▒ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░
|
||||||
|
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
|
||||||
|
|
29
apps/bot/src/bot/event/mod.rs
Normal file
29
apps/bot/src/bot/event/mod.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
use crate::bot::{Data, Error};
|
||||||
|
use poise::serenity_prelude::{self as serenity, ActivityData};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
pub async fn event_handler(
|
||||||
|
ctx: &serenity::Context,
|
||||||
|
event: &serenity::FullEvent,
|
||||||
|
_framework: poise::FrameworkContext<'_, Data, Error>,
|
||||||
|
data: &Data,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
match event {
|
||||||
|
serenity::FullEvent::Ready { data_about_bot, .. } => {
|
||||||
|
info!("Logged in as {}", data_about_bot.user.name);
|
||||||
|
info!("Guilds: {}", data_about_bot.guilds.len());
|
||||||
|
ctx.set_activity(Some(ActivityData {
|
||||||
|
name: data.config.default_status.clone(),
|
||||||
|
kind: serenity::ActivityType::Playing,
|
||||||
|
url: None,
|
||||||
|
state: None,
|
||||||
|
}));
|
||||||
|
info!(
|
||||||
|
"Bot is ready and set to default status: {}",
|
||||||
|
data.config.default_status
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
72
apps/bot/src/bot/helper/mod.rs
Normal file
72
apps/bot/src/bot/helper/mod.rs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
use clickhouse_pool::traits::Model;
|
||||||
|
use database::guild::Guild;
|
||||||
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
use crate::bot::{Context, Error};
|
||||||
|
|
||||||
|
/// Check if the guild has been initialized, if it is check if user is in list of admins
|
||||||
|
/// If not, check the user's permissions
|
||||||
|
/// If the user is not allowed dont allow the action
|
||||||
|
pub async fn is_user_admin_right(ctx: Context<'_>) -> Result<bool, Error> {
|
||||||
|
let guild_id = match ctx.guild_id() {
|
||||||
|
Some(id) => id.get(),
|
||||||
|
None => {
|
||||||
|
ctx.say("This command can only be used in a server.")
|
||||||
|
.await?;
|
||||||
|
info!(
|
||||||
|
"User {} tried to use a guild command outside of a guild.",
|
||||||
|
ctx.author().id
|
||||||
|
);
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if the user has the required permissions
|
||||||
|
let users_permissions = match ctx.author_member().await.unwrap().permissions {
|
||||||
|
Some(permissions) => permissions,
|
||||||
|
None => {
|
||||||
|
warn!("User permissions not found for user: {}", ctx.author().id);
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if users_permissions.administrator()
|
||||||
|
|| users_permissions.manage_guild()
|
||||||
|
|| users_permissions.manage_roles()
|
||||||
|
|| users_permissions.manage_channels()
|
||||||
|
{
|
||||||
|
info!(
|
||||||
|
"User {} has admin rights in guild {}.",
|
||||||
|
ctx.author().id,
|
||||||
|
guild_id
|
||||||
|
);
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
let manager_pool = ctx.data().datalake_config.clone();
|
||||||
|
|
||||||
|
let fetch_query = Guild::build_select_query(Some(&format!("id = {}", guild_id)), None, None);
|
||||||
|
let guilds = manager_pool
|
||||||
|
.execute_select_with_retry::<Guild>(&fetch_query)
|
||||||
|
.await?;
|
||||||
|
if guilds.is_empty() {
|
||||||
|
warn!("Guild with ID {} not found in database.", guild_id);
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
let guild = &guilds[0];
|
||||||
|
if guild.enrolled {
|
||||||
|
if guild.moderator.contains(&ctx.author().id.get()) {
|
||||||
|
info!(
|
||||||
|
"User {} is a moderator in guild {}.",
|
||||||
|
ctx.author().id,
|
||||||
|
guild_id
|
||||||
|
);
|
||||||
|
return Ok(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
info!(
|
||||||
|
"User {} does not have admin rights in guild {}.",
|
||||||
|
ctx.author().id,
|
||||||
|
guild_id
|
||||||
|
);
|
||||||
|
Ok(false)
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use clickhouse_pool::pool_manager::PoolManager;
|
use clickhouse_pool::pool_manager::PoolManager;
|
||||||
|
use event::event_handler;
|
||||||
use poise::serenity_prelude::GatewayIntents;
|
use poise::serenity_prelude::GatewayIntents;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use trivia::trivia;
|
use trivia::trivia;
|
||||||
@ -8,6 +9,8 @@ use utility::{age::age, help::help, server::servers};
|
|||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
|
pub mod event;
|
||||||
|
pub mod helper;
|
||||||
pub mod trivia;
|
pub mod trivia;
|
||||||
pub mod utility;
|
pub mod utility;
|
||||||
|
|
||||||
@ -40,6 +43,9 @@ pub async fn start_bot(config: Config, datalake_config: Arc<PoolManager>) {
|
|||||||
prefix: Some(prefix),
|
prefix: Some(prefix),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
event_handler: |ctx, event, framework, data| {
|
||||||
|
Box::pin(event_handler(ctx, event, framework, data))
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.setup(|ctx, _ready, framework| {
|
.setup(|ctx, _ready, framework| {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::bot::helper::is_user_admin_right;
|
||||||
use crate::bot::{Context, Error};
|
use crate::bot::{Context, Error};
|
||||||
use clickhouse_pool::traits::Model;
|
use clickhouse_pool::traits::Model;
|
||||||
use database::trivial::Trivial;
|
use database::trivial::Trivial;
|
||||||
@ -7,7 +8,13 @@ use tracing::{debug, info, instrument};
|
|||||||
/// Create a trivia game
|
/// Create a trivia game
|
||||||
/// Use the current channel by default, or specify a different channel.
|
/// Use the current channel by default, or specify a different channel.
|
||||||
#[instrument(name="trivia_create",skip(ctx), level = "info", fields(channel = ctx.channel_id().get(), guild = ?ctx.guild_id().unwrap().get()))]
|
#[instrument(name="trivia_create",skip(ctx), level = "info", fields(channel = ctx.channel_id().get(), guild = ?ctx.guild_id().unwrap().get()))]
|
||||||
#[poise::command(prefix_command, slash_command, guild_only, category = "Trivia")]
|
#[poise::command(
|
||||||
|
prefix_command,
|
||||||
|
slash_command,
|
||||||
|
guild_only,
|
||||||
|
category = "Trivia",
|
||||||
|
check = "is_user_admin_right"
|
||||||
|
)]
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "Name of the trivia game"] name: String,
|
#[description = "Name of the trivia game"] name: String,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::bot::helper::is_user_admin_right;
|
||||||
use crate::bot::{Context, Error};
|
use crate::bot::{Context, Error};
|
||||||
use clickhouse_pool::traits::Model;
|
use clickhouse_pool::traits::Model;
|
||||||
use database::trivial::Trivial;
|
use database::trivial::Trivial;
|
||||||
@ -29,7 +30,13 @@ pub fn trivia_list_embed(trivia: &Trivial, current_channel: bool) -> serenity_pr
|
|||||||
|
|
||||||
/// List trivia games in the current server and show if there are any in the current channel.
|
/// List trivia games in the current server and show if there are any in the current channel.
|
||||||
#[instrument(name="trivia_list",skip(ctx), level = "info", fields(channel = ctx.channel_id().get(), guild = ?ctx.guild_id().unwrap().get()))]
|
#[instrument(name="trivia_list",skip(ctx), level = "info", fields(channel = ctx.channel_id().get(), guild = ?ctx.guild_id().unwrap().get()))]
|
||||||
#[poise::command(prefix_command, slash_command, guild_only, category = "Trivia")]
|
#[poise::command(
|
||||||
|
prefix_command,
|
||||||
|
slash_command,
|
||||||
|
guild_only,
|
||||||
|
category = "Trivia",
|
||||||
|
check = "is_user_admin_right"
|
||||||
|
)]
|
||||||
pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
|
pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
let guild_id = match ctx.guild_id() {
|
let guild_id = match ctx.guild_id() {
|
||||||
Some(id) => id.get(),
|
Some(id) => id.get(),
|
||||||
|
@ -33,6 +33,7 @@ pub struct Config {
|
|||||||
pub prefix: String,
|
pub prefix: String,
|
||||||
pub tracing: Vec<Tracing>,
|
pub tracing: Vec<Tracing>,
|
||||||
pub persistence: PersistenceConfig,
|
pub persistence: PersistenceConfig,
|
||||||
|
pub default_status: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_local_config() -> Config {
|
pub fn parse_local_config() -> Config {
|
||||||
|
@ -8,6 +8,7 @@ pub mod dotenv;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
println!(include_str!("banner.art"));
|
||||||
let config = parse_local_config();
|
let config = parse_local_config();
|
||||||
tool_tracing::init::init_tracing(config.tracing.clone(), config.bot_name.clone());
|
tool_tracing::init::init_tracing(config.tracing.clone(), config.bot_name.clone());
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ env = "dev-che"
|
|||||||
port = 5437
|
port = 5437
|
||||||
token = ""
|
token = ""
|
||||||
prefix = "!"
|
prefix = "!"
|
||||||
|
default_status = "Cooking some nasty shit !"
|
||||||
|
|
||||||
[persistence]
|
[persistence]
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
|
Loading…
Reference in New Issue
Block a user