i added too much

This commit is contained in:
2025-02-02 01:12:29 +01:00
parent 94726df090
commit a5342cacad
4 changed files with 130 additions and 32 deletions
+1
View File
@@ -8,4 +8,5 @@ edition = "2021"
[dependencies] [dependencies]
poise = "0.6.1" poise = "0.6.1"
rand = "0.9.0" rand = "0.9.0"
serde_json = "1.0.138"
tokio = { version = "1.43.0", features = ["rt-multi-thread"] } tokio = { version = "1.43.0", features = ["rt-multi-thread"] }
+4
View File
@@ -0,0 +1,4 @@
[
"my name",
"is not jeff."
]
+75 -11
View File
@@ -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}; use rand::{seq::IteratorRandom, Rng};
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn ping( pub async fn ping(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "The text to echo back"] text: Option<String>, #[description = "The text to echo back."] text: Option<String>,
) -> Result<(), Error> ) -> 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(()); return Ok(());
} }
@@ -19,7 +19,7 @@ pub async fn ping(
#[poise::command(slash_command, prefix_command, default_member_permissions = "ADMINISTRATOR")] #[poise::command(slash_command, prefix_command, default_member_permissions = "ADMINISTRATOR")]
pub async fn stop( pub async fn stop(
ctx: Context<'_>, ctx: Context<'_>,
#[description = "Type \"i want to stop the bot now\" to confirm"] confirmation: Option<String>, #[description = "Type \"i want to stop the bot now\" to confirm."] confirmation: Option<String>,
) -> Result<(), Error> ) -> Result<(), Error>
{ {
let dev_enabled = ctx.data().dev; 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"; || confirmation.unwrap_or_else(|| "".to_string()).to_lowercase() == "i want to stop the bot now";
if should_stop { 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.serenity_context().set_presence(None, OnlineStatus::Invisible);
ctx.framework().shard_manager.shutdown_all().await; ctx.framework().shard_manager.shutdown_all().await;
} }
else { 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(()); return Ok(());
@@ -51,9 +51,12 @@ pub async fn embed(
#[description = "Color of side strip."] color: Option<u32>, #[description = "Color of side strip."] color: Option<u32>,
#[description = "A URL the title is bound to."] url: Option<String>, #[description = "A URL the title is bound to."] url: Option<String>,
#[description = "Timestamp at bottom (best to leave empty)."] timestamp: Option<Timestamp>, #[description = "Timestamp at bottom (best to leave empty)."] timestamp: Option<Timestamp>,
#[description = "Empheral (only visible to you)"] empheral: Option<bool> #[description = "Empheral (only visible to you)."] empheral: Option<bool>,
#[description = "Shows \"used {Command}\" reply text."] reply: Option<bool>
) -> Result<(), Error> ) -> Result<(), Error>
{ {
let reply_unwrap = reply.unwrap_or_else(|| false);
send_embed( send_embed(
ctx, ctx,
EmbedOptions { EmbedOptions {
@@ -63,8 +66,13 @@ pub async fn embed(
url, url,
ts: timestamp, ts: timestamp,
empheral: empheral.unwrap_or_else(|| false) 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(()); return Ok(());
} }
@@ -83,8 +91,64 @@ pub async fn eight_ball(
send_msg( send_msg(
ctx, ctx,
format!("Q: {}\nA: {}", question, rand_item.unwrap()), format!("Q: {}\nA: {}", question, rand_item.unwrap()),
true,
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<bool>,
#[description = "Includes \"Use '/rule {rule}' to view rules individually\" preset message"] include_rule_command: Option<bool>,
#[description = "JSON (empty is preset file)"] json: Option<String>
) -> 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(()); return Ok(());
} }
+50 -21
View File
@@ -1,7 +1,7 @@
mod cmds; mod cmds;
mod events; mod events;
use poise::CreateReply; use poise::{serenity_prelude::CreateMessage, CreateReply, ReplyHandle};
use std::env; use std::env;
use poise::serenity_prelude::{self as serenity, Color, CreateEmbed, Timestamp}; use poise::serenity_prelude::{self as serenity, Color, CreateEmbed, Timestamp};
@@ -66,7 +66,8 @@ async fn main() {
cmds::ping(), cmds::ping(),
cmds::embed(), cmds::embed(),
cmds::stop(), cmds::stop(),
cmds::eight_ball() cmds::eight_ball(),
cmds::write_json()
], ],
event_handler: events::event_handler, event_handler: events::event_handler,
..Default::default() ..Default::default()
@@ -97,40 +98,68 @@ fn none_to_empty(string: Option<String>) -> String {
async fn send_msg( async fn send_msg(
ctx: Context<'_>, ctx: Context<'_>,
t: String, t: String,
empheral: bool empheral: bool,
) -> Result<(), Error> reply: bool
) -> Option<ReplyHandle<'_>>
{ {
let r = CreateReply { if reply {
content: Some(t), let r = CreateReply {
ephemeral: Some(empheral), content: Some(t),
..Default::default() ephemeral: Some(empheral),
}; ..Default::default()
};
ctx.send(r).await?; let msg = ctx.send(r).await;
return Some(msg.unwrap());
return Ok(()); }
else {
let _ = ctx.channel_id().say(ctx.http(), t).await;
return None;
}
} }
async fn send_embed( async fn send_embed(
ctx: Context<'_>, ctx: Context<'_>,
options: EmbedOptions, options: EmbedOptions,
) -> Result<(), Error> reply: bool
) -> Option<ReplyHandle<'_>>
{ {
let embed = CreateEmbed::new() let mut embed = CreateEmbed::new()
.title (none_to_empty(options.title)) .title (none_to_empty(options.title))
.description(options.desc) .description(options.desc)
.colour (Color::new(options.col.unwrap_or_else(|| 5793266))) .colour (Color::new(options.col.unwrap_or_else(|| 5793266)))
.url (none_to_empty(options.url)) .url (none_to_empty(options.url));
.timestamp (options.ts.unwrap_or_else(|| Timestamp::now()));
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 { let r = CreateReply {
embeds: vec![embed], content: Some(new_text),
ephemeral: Some(options.empheral),
..Default::default() ..Default::default()
}; };
ctx.send(r).await?; let _ = msg.edit(ctx, r).await;
return Ok(());
} }