tried implemented debug message for send_message

master
Edward Shen 2020-05-02 16:54:12 -04:00
parent 144564f4f7
commit cc1ffcb065
Signed by: edward
GPG Key ID: 19182661E818369F
9 changed files with 91 additions and 4 deletions

1
Cargo.lock generated
View File

@ -314,6 +314,7 @@ dependencies = [
"sqlx", "sqlx",
"tokio", "tokio",
"unicode-segmentation", "unicode-segmentation",
"url",
] ]
[[package]] [[package]]

View File

@ -16,4 +16,5 @@ rand = "0.7"
unicode-segmentation = "1.6" unicode-segmentation = "1.6"
log = "0.4" log = "0.4"
env_logger = "0.7" env_logger = "0.7"
lazy_static = "1.4" lazy_static = "1.4"
url = "2.1"

View File

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 121 KiB

BIN
res/mocking-spongebob.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

View File

@ -42,6 +42,7 @@ async fn crosspost(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul
msg.author, msg.author,
args.rest() args.rest()
); );
for channel in channels { for channel in channels {
debug_channel_say(channel, msg.author.id, ctx, &rest).await?; debug_channel_say(channel, msg.author.id, ctx, &rest).await?;
} }

50
src/commands/mock.rs Normal file
View File

@ -0,0 +1,50 @@
use crate::util::{debug_say, is_bot_owner};
use rand::thread_rng;
use rand::Rng;
use serenity::framework::standard::{macros::command, CommandResult};
use serenity::model::channel::Message;
use serenity::prelude::Context;
use url::Url;
#[command]
async fn mock(ctx: &Context, msg: &Message) -> CommandResult {
if is_bot_owner(msg.author.id) {
let prev_msg = msg
.channel_id
.messages(ctx, |retriever| retriever.before(msg.id).limit(1))
.await?
.pop();
let mut content: String = prev_msg.unwrap().content_safe(ctx).await;
for word in content.clone().split_whitespace() {
if let Err(_) = Url::parse(word) {
content = (&content).replacen(word, &random_uppercase(word), 1);
}
}
// debug_say(msg, ctx, content).await?;
msg.channel_id
.send_message(ctx, |m| {
m.content(content);
m.add_file(serenity::http::AttachmentType::Bytes {
data: std::borrow::Cow::from(
&include_bytes!("../../res/mocking-spongebob.jpg")[..],
),
filename: "spongemock.jpg".to_string(),
})
})
.await?;
}
Ok(())
}
fn random_uppercase(word: &str) -> String {
let mut rng = thread_rng();
word.chars()
.map(|c| {
if rng.gen::<bool>() {
c.to_uppercase().collect::<String>()
} else {
c.to_lowercase().collect()
}
})
.collect()
}

View File

@ -1,6 +1,6 @@
use crate::commands::{ use crate::commands::{
clap::CLAP_COMMAND, crosspost::CROSSPOST_COMMAND, cube::CUBE_COMMAND, heck::HECK_COMMAND, clap::CLAP_COMMAND, crosspost::CROSSPOST_COMMAND, cube::CUBE_COMMAND, heck::HECK_COMMAND,
source::SOURCE_COMMAND, mock::MOCK_COMMAND, source::SOURCE_COMMAND,
}; };
use serenity::framework::standard::macros::group; use serenity::framework::standard::macros::group;
@ -8,8 +8,9 @@ mod clap;
mod crosspost; mod crosspost;
mod cube; mod cube;
mod heck; mod heck;
mod mock;
mod source; mod source;
#[group] #[group]
#[commands(heck, clap, cube, source, crosspost)] #[commands(heck, clap, cube, source, crosspost, mock)]
pub(crate) struct General; pub(crate) struct General;

View File

@ -72,7 +72,7 @@ const DESU_ACTIONS: &[DesuAction<BoxFuture<Result<Message, serenity::Error>>>] =
&|ctx, msg| { &|ctx, msg| {
Box::pin(msg.channel_id.send_message(ctx, |m| { Box::pin(msg.channel_id.send_message(ctx, |m| {
m.add_file(serenity::http::AttachmentType::Bytes { m.add_file(serenity::http::AttachmentType::Bytes {
data: std::borrow::Cow::from(&include_bytes!("../desu.jpg")[..]), data: std::borrow::Cow::from(&include_bytes!("../../res/desu.jpg")[..]),
filename: "desu.jpg".to_string(), filename: "desu.jpg".to_string(),
}) })
})) }))

View File

@ -1,5 +1,6 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serenity::{ use serenity::{
builder::CreateMessage,
http::Http, http::Http,
model::{channel::Message, id::ChannelId}, model::{channel::Message, id::ChannelId},
}; };
@ -45,3 +46,35 @@ pub async fn debug_channel_say(
Ok(None) Ok(None)
} }
} }
// pub async fn debug_send_message<'a, F>(
// channel: impl Into<ChannelId>,
// author_id: impl Into<u64>,
// http: impl AsRef<Http>,
// f: F,
// ) -> Result<Option<Message>, serenity::Error>
// where
// for<'b> F: FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>,
// {
// let channel: ChannelId = channel.into();
// if cfg!(debug_assertions) && author_id.into() == *BOT_OWNER_ID {
// Ok(Some(
// channel
// .send_message(http, |mut m| {
// let resp = f(m);
// // let cur_content = resp.0.get("content");
// f(m)
// })
// .await?,
// ))
// } else if !cfg!(debug_assertions) {
// Ok(Some(channel.send_message(http, f).await?))
// } else {
// Ok(None)
// }
// }
pub fn is_bot_owner(id: impl Into<u64>) -> bool {
id.into() == *BOT_OWNER_ID
}