From 1b3da60f76d0c3dceb28c82dae7869f2fab783e0 Mon Sep 17 00:00:00 2001 From: ByteDice Date: Mon, 2 Mar 2026 21:25:09 +0100 Subject: [PATCH] updated gitignore cuz it didnt include "debug/" --- .gitignore | 1 - src/cmds/debug/guild_invite.rs | 35 +++++++++++++++++++++++++ src/cmds/debug/leave_guild.rs | 19 ++++++++++++++ src/cmds/debug/main_cmd.rs | 48 ++++++++++++++++++++++++++++++++++ src/cmds/debug/ping.rs | 8 ++++++ src/cmds/debug/reload_cfg.rs | 33 +++++++++++++++++++++++ src/cmds/debug/save.rs | 12 +++++++++ src/cmds/debug/stop.rs | 35 +++++++++++++++++++++++++ src/cmds/debug/view_guilds.rs | 29 ++++++++++++++++++++ src/cmds/debug/whoami.rs | 13 +++++++++ 10 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/cmds/debug/guild_invite.rs create mode 100644 src/cmds/debug/leave_guild.rs create mode 100644 src/cmds/debug/main_cmd.rs create mode 100644 src/cmds/debug/ping.rs create mode 100644 src/cmds/debug/reload_cfg.rs create mode 100644 src/cmds/debug/save.rs create mode 100644 src/cmds/debug/stop.rs create mode 100644 src/cmds/debug/view_guilds.rs create mode 100644 src/cmds/debug/whoami.rs diff --git a/.gitignore b/.gitignore index 2e745ad..49d3cc5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -debug/ target/ Cargo.lock **/*.rs.bk diff --git a/src/cmds/debug/guild_invite.rs b/src/cmds/debug/guild_invite.rs new file mode 100644 index 0000000..634e07d --- /dev/null +++ b/src/cmds/debug/guild_invite.rs @@ -0,0 +1,35 @@ +use poise::serenity_prelude::{CreateInvite, GuildId}; + +use crate::{Context, Error, lang, messages::send_msg}; + + +pub async fn cmd(ctx: Context<'_>, guild_id: u64) -> Result<(), Error> { + let id = GuildId::from(guild_id); + let guild = id.to_partial_guild(ctx.http()).await?; + + if !ctx.serenity_context().cache.guilds().contains(&id) { + send_msg(ctx, lang!("dc_msg_not_in_guild"), true, true).await; + return Ok(()); + } + + let ch = guild.channels(ctx.http()).await? + .values() + .find(|c| c.is_text_based()) + .cloned(); + + if let Some(channel) = ch { + let bot_member = &guild.member(ctx.http(), ctx.framework().bot_id).await?; + let perms = guild.user_permissions_in(&channel, bot_member); + + if !perms.create_instant_invite() { + send_msg(ctx, lang!("dc_msg_no_perms", "CreateInvite"), true, true).await; + return Ok(()); + } + + let inv_builder = CreateInvite::new().max_age(0).max_uses(0); + let inv = channel.id.create_invite(ctx.http(), inv_builder).await?; + send_msg(ctx, inv.url(), true, true).await; + } + + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds/debug/leave_guild.rs b/src/cmds/debug/leave_guild.rs new file mode 100644 index 0000000..c86e790 --- /dev/null +++ b/src/cmds/debug/leave_guild.rs @@ -0,0 +1,19 @@ +use poise::serenity_prelude::GuildId; + +use crate::{Context, Error, lang, messages::send_msg}; + + +pub async fn cmd(ctx: Context<'_>, guild_id: u64) -> Result<(), Error> { + let id = GuildId::from(guild_id); + let guild = id.to_partial_guild(ctx.http()).await?; + + if !ctx.serenity_context().cache.guilds().contains(&id) { + send_msg(ctx, lang!("dc_msg_not_in_guild"), true, true).await; + return Ok(()); + } + + guild.leave(ctx.http()).await?; + send_msg(ctx, lang!("success"), true, true).await; + + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds/debug/main_cmd.rs b/src/cmds/debug/main_cmd.rs new file mode 100644 index 0000000..0e25af3 --- /dev/null +++ b/src/cmds/debug/main_cmd.rs @@ -0,0 +1,48 @@ +use crate::{Context, Error, cmds::debug::{guild_invite, leave_guild, ping, save, stop, view_guilds, whoami}}; + + +#[derive(poise::ChoiceParameter, PartialEq)] +pub enum Subcommands { + GuildInvite, + LeaveGuild, + Ping, + //ReloadCfg, + Save, + Stop, + ViewGuilds, + WhoAmI +} + + +#[poise::command( + slash_command, + prefix_command, + category = "owner", + rename = "debug", + owners_only, + required_bot_permissions = "SEND_MESSAGES | VIEW_CHANNEL" +)] +/// Various debug utilities +pub async fn cmd( + ctx: Context<'_>, + #[description = "Subcommand"] subcommand: Subcommands, + string_arg: Option, + u64_arg: Option +) -> Result<(), Error> +{ + let u64_arg_u: u64 = u64_arg.unwrap_or("0".to_string()).as_str().parse()?; + + match subcommand { + Subcommands::GuildInvite => guild_invite::cmd(ctx, u64_arg_u).await?, + Subcommands::LeaveGuild => leave_guild::cmd(ctx, u64_arg_u).await?, + Subcommands::Ping => ping::cmd(ctx).await?, + //Subcommands::ReloadCfg => reload_cfg::cmd(ctx).await?, + Subcommands::Save => save::cmd(ctx).await?, + Subcommands::Stop => stop::cmd(ctx, string_arg).await?, + Subcommands::ViewGuilds => view_guilds::cmd(ctx).await?, + Subcommands::WhoAmI => whoami::cmd(ctx).await?, + //_ => return Ok(()) + } + + return Ok(()); +} diff --git a/src/cmds/debug/ping.rs b/src/cmds/debug/ping.rs new file mode 100644 index 0000000..09e8f87 --- /dev/null +++ b/src/cmds/debug/ping.rs @@ -0,0 +1,8 @@ +use crate::{messages::send_msg, Context, Error}; + + +pub async fn cmd(ctx: Context<'_>) -> Result<(), Error> { + send_msg(ctx, "Pong".to_string(), true, true).await; + + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds/debug/reload_cfg.rs b/src/cmds/debug/reload_cfg.rs new file mode 100644 index 0000000..7650f7d --- /dev/null +++ b/src/cmds/debug/reload_cfg.rs @@ -0,0 +1,33 @@ +/*use crate::{Context, Error, data::{self, get_toml_mutex, read_cfg_data}, lang, messages::send_msg}; + + +pub async fn cmd(ctx: Context<'_>) -> Result<(), Error> { + let r = read_cfg_data(ctx.data(), false).await; + let d = get_toml_mutex(&ctx.data().cfg).await.unwrap(); + + if r.is_none() { return Ok(()); } + + update_cfg(ctx).await; + + if r.unwrap()["value"].as_bool().unwrap() { + send_msg( + ctx, + lang!("dc_msg_reload_cfg_success", toml::to_string_pretty(&d).unwrap()), + true, + true + ).await; + return Ok(()); + } + + send_msg(ctx, lang!("dc_msg_reload_cfg_python_fail"), true, true).await; + return Ok(()); +} + + +async fn update_cfg(ctx: Context<'_>) { + let data_binding = get_toml_mutex(&ctx.data().cfg).await.unwrap(); + let lang_cfg = data_binding["general"]["lang"].as_str().unwrap(); + data::load_lang_data(lang_cfg.to_string()); + + set_status(data_binding, ctx).await; +}*/ \ No newline at end of file diff --git a/src/cmds/debug/save.rs b/src/cmds/debug/save.rs new file mode 100644 index 0000000..b018c8e --- /dev/null +++ b/src/cmds/debug/save.rs @@ -0,0 +1,12 @@ +use crate::{Context, Error, db::{discord, reddit}, lang, messages::{edit_reply, send_msg}}; + +pub async fn cmd(ctx: Context<'_>) -> Result<(), Error> { + let msg = send_msg(ctx, lang!("dc_msg_owner_data_save"), true, true).await.unwrap(); + + discord::write_data(ctx.data()).await; + reddit ::write_data().await; + + edit_reply(ctx, msg, lang!("dc_msg_owner_data_save_complete")).await; + + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds/debug/stop.rs b/src/cmds/debug/stop.rs new file mode 100644 index 0000000..32a85e6 --- /dev/null +++ b/src/cmds/debug/stop.rs @@ -0,0 +1,35 @@ +use std::process; + +use poise::serenity_prelude::OnlineStatus; + +use crate::{db::{discord, reddit}, lang, messages::{edit_reply, send_msg}, websocket::send_cmd_json, Context, Error}; + +pub async fn cmd(ctx: Context<'_>, confirmation: Option) -> Result<(), Error> { + let stop_confirm = "i want to stop the bot now".replace(" ", ""); + let confirm_formatted = confirmation.unwrap_or_default().to_lowercase().replace(" ", ""); + let should_stop = ctx.data().args.dev || confirm_formatted == stop_confirm; + + if should_stop { + let msg = send_msg(ctx, lang!("dc_msg_owner_data_save"), true, true).await.unwrap(); + discord::write_data(ctx.data()).await; + reddit ::write_data().await; + send_cmd_json("stop_praw", None, true).await; + + let complete = format!( + "{}\n{}", + lang!("dc_msg_owner_data_save_complete"), + lang!("dc_msg_owner_shutdown") + ); + + edit_reply(ctx, msg, complete).await; + ctx.serenity_context().set_presence(None, OnlineStatus::Invisible); + ctx.framework().shard_manager.shutdown_all().await; + + process::exit(0); + } + else { + send_msg(ctx, lang!("dc_msg_owner_shutdown_failed_confirmation"), true, true).await; + } + + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds/debug/view_guilds.rs b/src/cmds/debug/view_guilds.rs new file mode 100644 index 0000000..0e44a6c --- /dev/null +++ b/src/cmds/debug/view_guilds.rs @@ -0,0 +1,29 @@ +use crate::{Context, Error, messages::send_msg}; + + +pub async fn cmd(ctx: Context<'_>) -> Result<(), Error> { + let guilds = ctx.cache().guilds(); + let mut msgs: Vec> = vec![vec![]]; + let mut char_count: usize = 0; + let mut msg_idx: usize = 0; + + for g in guilds { + let name = g.name(ctx.cache()).unwrap_or("[unnamed]".to_string()); + let id = g.get(); + + let msg = format!("**[{}]** {}", id, name); + char_count += msg.len(); + + if char_count >= 2000 { + msgs.push(Vec::new()); + msg_idx += 1; + } + + msgs[msg_idx].push(msg); + } + + for msg in msgs + { send_msg(ctx, msg.join("\n"), true, true).await; } + + return Ok(()); +} \ No newline at end of file diff --git a/src/cmds/debug/whoami.rs b/src/cmds/debug/whoami.rs new file mode 100644 index 0000000..b85c3b4 --- /dev/null +++ b/src/cmds/debug/whoami.rs @@ -0,0 +1,13 @@ +use crate::{lang, messages::send_msg, Context, Error}; + +pub async fn cmd(ctx: Context<'_>) -> Result<(), Error> { + let data = ctx.data(); + let uid: u64 = ctx.author().id.into(); + + let is_owner = data.owners .contains(&uid); + let is_bk_mod = data.bk_mods.contains(&uid); + + send_msg(ctx, lang!("dc_msg_whoami", is_owner, is_bk_mod), true, true).await; + + return Ok(()); +} \ No newline at end of file