From a5342cacadd07bc2b5a300eaaeaedce1854f9932 Mon Sep 17 00:00:00 2001 From: ByteDice Date: Sun, 2 Feb 2025 01:12:29 +0100 Subject: [PATCH] i added too much --- Cargo.toml | 1 + data/write_json.json | 4 +++ src/cmds.rs | 86 ++++++++++++++++++++++++++++++++++++++------ src/main.rs | 71 +++++++++++++++++++++++++----------- 4 files changed, 130 insertions(+), 32 deletions(-) create mode 100644 data/write_json.json diff --git a/Cargo.toml b/Cargo.toml index e5708a9..bfeb093 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] poise = "0.6.1" rand = "0.9.0" +serde_json = "1.0.138" tokio = { version = "1.43.0", features = ["rt-multi-thread"] } diff --git a/data/write_json.json b/data/write_json.json new file mode 100644 index 0000000..0f6039e --- /dev/null +++ b/data/write_json.json @@ -0,0 +1,4 @@ +[ + "my name", + "is not jeff." +] \ No newline at end of file diff --git a/src/cmds.rs b/src/cmds.rs index 9d4690a..116296d 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -1,16 +1,16 @@ -use crate::{send_embed, send_msg, Context, EmbedOptions, Error}; +use crate::{send_embed, send_msg, edit_msg, Context, EmbedOptions, Error}; -use poise::serenity_prelude::{OnlineStatus, Timestamp}; +use poise::serenity_prelude::{GetMessages, OnlineStatus, Timestamp}; use rand::{seq::IteratorRandom, Rng}; #[poise::command(slash_command, prefix_command)] pub async fn ping( ctx: Context<'_>, - #[description = "The text to echo back"] text: Option, + #[description = "The text to echo back."] text: Option, ) -> Result<(), Error> { - send_msg(ctx, text.unwrap_or_else(|| "Pong".to_string()), true).await?; + send_msg(ctx, text.unwrap_or_else(|| "Pong".to_string()), true, true).await; return Ok(()); } @@ -19,7 +19,7 @@ pub async fn ping( #[poise::command(slash_command, prefix_command, default_member_permissions = "ADMINISTRATOR")] pub async fn stop( ctx: Context<'_>, - #[description = "Type \"i want to stop the bot now\" to confirm"] confirmation: Option, + #[description = "Type \"i want to stop the bot now\" to confirm."] confirmation: Option, ) -> Result<(), Error> { let dev_enabled = ctx.data().dev; @@ -27,12 +27,12 @@ pub async fn stop( || confirmation.unwrap_or_else(|| "".to_string()).to_lowercase() == "i want to stop the bot now"; if should_stop { - send_msg(ctx, "Shutting down...".to_string(), true).await?; + send_msg(ctx, "Shutting down...".to_string(), true, true).await; ctx.serenity_context().set_presence(None, OnlineStatus::Invisible); ctx.framework().shard_manager.shutdown_all().await; } else { - send_msg(ctx, "Failed to shut down.".to_string(), true).await?; + send_msg(ctx, "Failed to shut down.".to_string(), true, true).await; } return Ok(()); @@ -51,9 +51,12 @@ pub async fn embed( #[description = "Color of side strip."] color: Option, #[description = "A URL the title is bound to."] url: Option, #[description = "Timestamp at bottom (best to leave empty)."] timestamp: Option, - #[description = "Empheral (only visible to you)"] empheral: Option + #[description = "Empheral (only visible to you)."] empheral: Option, + #[description = "Shows \"used {Command}\" reply text."] reply: Option ) -> Result<(), Error> { + let reply_unwrap = reply.unwrap_or_else(|| false); + send_embed( ctx, EmbedOptions { @@ -63,8 +66,13 @@ pub async fn embed( url, ts: timestamp, empheral: empheral.unwrap_or_else(|| false) - } - ).await?; + }, + reply_unwrap + ).await; + + if !reply_unwrap { + send_msg(ctx, "Mandatory success response.".to_string(), true, true).await; + } return Ok(()); } @@ -83,8 +91,64 @@ pub async fn eight_ball( send_msg( ctx, format!("Q: {}\nA: {}", question, rand_item.unwrap()), + true, true - ).await?; + ).await; + + return Ok(()); +} + + +#[poise::command( + slash_command, + prefix_command, + default_member_permissions = "ADMINISTRATOR" +)] +pub async fn write_json( + ctx: Context<'_>, + #[description = "Delete all messages sent by the bot in the selected channel."] remove_all: Option, + #[description = "Includes \"Use '/rule {rule}' to view rules individually\" preset message"] include_rule_command: Option, + #[description = "JSON (empty is preset file)"] json: Option +) -> Result<(), Error> +{ + let rm_all = remove_all.unwrap_or_else(|| false); + let include_cmd = include_rule_command.unwrap_or_else(|| false); + + if rm_all { + let progress = send_msg(ctx, "Deleting all messages in channel...".to_string(), true, true).await; + + let builder = GetMessages::new().limit(100); + let msgs = ctx.channel_id().messages(ctx.http(), builder).await?; + + for msg in msgs { + if msg.author.id == ctx.framework().bot_id { + msg.delete(ctx.http()).await?; + } + } + + edit_msg(ctx, progress.unwrap(), "Deleting all messages in channel... Done!".to_string()).await; + } + + let json_str = json.unwrap_or_else(|| + std::fs::read_to_string("./data/write_json.json") + .expect("No JSON preset file exists.") + ); + + let json_json: serde_json::Value = serde_json::from_str(&json_str).expect("JSON was improperly formatted"); + if !json_json.is_array() { + send_msg(ctx, "JSON is not an array of strings".to_string(), true, true).await; + return Ok(()); + } + + for i in json_json.as_array().unwrap() { + if !i.is_string() { continue; } + let i_str = i.to_string(); + send_msg(ctx, i_str[1..i_str.len() - 1].to_string(), false, false).await; + } + + if include_cmd { + send_msg(ctx, "Use /rules thank you".to_string(), false, false).await; + } return Ok(()); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2611a79..3320739 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod cmds; mod events; -use poise::CreateReply; +use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle}; use std::env; use poise::serenity_prelude::{self as serenity, Color, CreateEmbed, Timestamp}; @@ -66,7 +66,8 @@ async fn main() { cmds::ping(), cmds::embed(), cmds::stop(), - cmds::eight_ball() + cmds::eight_ball(), + cmds::write_json() ], event_handler: events::event_handler, ..Default::default() @@ -97,40 +98,68 @@ fn none_to_empty(string: Option) -> String { async fn send_msg( ctx: Context<'_>, t: String, - empheral: bool -) -> Result<(), Error> + empheral: bool, + reply: bool +) -> Option> { - let r = CreateReply { - content: Some(t), - ephemeral: Some(empheral), - ..Default::default() - }; + if reply { + let r = CreateReply { + content: Some(t), + ephemeral: Some(empheral), + ..Default::default() + }; - ctx.send(r).await?; - - return Ok(()); + let msg = ctx.send(r).await; + return Some(msg.unwrap()); + } + else { + let _ = ctx.channel_id().say(ctx.http(), t).await; + return None; + } } async fn send_embed( ctx: Context<'_>, options: EmbedOptions, -) -> Result<(), Error> + reply: bool +) -> Option> { - let embed = CreateEmbed::new() + let mut embed = CreateEmbed::new() .title (none_to_empty(options.title)) .description(options.desc) .colour (Color::new(options.col.unwrap_or_else(|| 5793266))) - .url (none_to_empty(options.url)) - .timestamp (options.ts.unwrap_or_else(|| Timestamp::now())); + .url (none_to_empty(options.url)); + + if options.ts.is_some() { embed = embed.timestamp(options.ts.unwrap()); } + if reply { + let r = CreateReply { + embeds: vec![embed], + ephemeral: Some(options.empheral), + ..Default::default() + }; + + let msg = ctx.send(r).await; + return Some(msg.unwrap()); + } + else { + let r = CreateMessage::new().embeds(vec![embed]); + let _ = ctx.channel_id().send_message(ctx.http(), r).await; + return None; + } +} + + +async fn edit_msg( + ctx: Context<'_>, + msg: ReplyHandle<'_>, + new_text: String +) { let r = CreateReply { - embeds: vec![embed], - ephemeral: Some(options.empheral), + content: Some(new_text), ..Default::default() }; - ctx.send(r).await?; - - return Ok(()); + let _ = msg.edit(ctx, r).await; } \ No newline at end of file