2023-07-15 09:18:08 +00:00
|
|
|
use std::sync::Arc;
|
2023-07-15 10:11:57 +00:00
|
|
|
use std::time::Duration;
|
2023-07-15 09:18:08 +00:00
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
use anyhow::Context;
|
|
|
|
use serenity::builder::{CreateAttachment, CreateEmbed, CreateMessage};
|
2023-07-15 09:18:08 +00:00
|
|
|
use serenity::http::Http;
|
|
|
|
use serenity::model::channel::Message;
|
2023-07-15 10:11:57 +00:00
|
|
|
use tokio::time::sleep;
|
2023-07-15 09:18:08 +00:00
|
|
|
use regex::Regex;
|
2024-02-14 12:22:29 +00:00
|
|
|
use serenity::model::id::ChannelId;
|
2023-07-15 09:18:08 +00:00
|
|
|
|
|
|
|
// Checks if the message should be mooved
|
|
|
|
// If the message should be mooved, try to move it and return Ok if mooved succesfully
|
|
|
|
// else returns Err()
|
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
pub async fn moove_check(msg: &Message) -> Option<u64> {
|
2023-07-15 09:18:08 +00:00
|
|
|
let word_count = msg.content.trim().split_whitespace().count();
|
|
|
|
let re = Regex::new(r"<#[0-9]*>$").unwrap();
|
|
|
|
|
|
|
|
if word_count != 1 || re.captures(&msg.content).is_none() {
|
2024-02-14 12:22:29 +00:00
|
|
|
return None
|
2023-07-15 09:18:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
let channel_id = match msg.content[2..msg.content.len() - 1].parse::<u64>() {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(_) => return None
|
|
|
|
};
|
|
|
|
|
|
|
|
return Some(channel_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn moove(http: Arc<Http>, msg: Message, m_channel_id: u64) -> anyhow::Result<()> {
|
|
|
|
// this should be in moove_check, but I need to find a good way to return in with channel_id
|
|
|
|
let msg_to_moove = msg.clone().referenced_message.context("Referenced message not found")?;
|
2023-07-15 09:18:08 +00:00
|
|
|
|
|
|
|
//steals all attachments, but sets all of them as Image urls, so rip actual docs etc
|
2024-02-14 12:22:29 +00:00
|
|
|
let mut attachments: Vec<CreateAttachment> = Vec::new();
|
|
|
|
for attachment in msg_to_moove.attachments.clone() {
|
|
|
|
let data = attachment.download().await?;
|
|
|
|
attachments.push(CreateAttachment::bytes(data, attachment.filename));
|
|
|
|
}
|
2023-07-15 09:18:08 +00:00
|
|
|
|
|
|
|
//steals all the embeds
|
|
|
|
let embeds: Vec<CreateEmbed> = msg_to_moove
|
2023-07-15 10:11:57 +00:00
|
|
|
.embeds.clone()
|
2023-07-15 09:18:08 +00:00
|
|
|
.into_iter()
|
2024-02-14 12:22:29 +00:00
|
|
|
.map(| embed | CreateEmbed::from(embed))
|
2023-07-15 09:18:08 +00:00
|
|
|
.collect();
|
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2023-07-15 09:18:08 +00:00
|
|
|
let mut new_content = format!("Sent by {}\n mooved {}\n", msg_to_moove.author, msg.author);
|
2024-02-14 12:22:29 +00:00
|
|
|
let mut new_msg = CreateMessage::new();
|
2023-07-15 09:18:08 +00:00
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
// Either copy all the attachments and embeds and create a new message that contains them all
|
2023-07-15 10:11:57 +00:00
|
|
|
if attachments.len() > 0 || embeds.len() > 0 {
|
2023-07-15 09:18:08 +00:00
|
|
|
new_content += format!("Message:\n{}", msg_to_moove.content).as_str();
|
2024-02-14 12:22:29 +00:00
|
|
|
|
|
|
|
new_msg = new_msg.content(new_content)
|
|
|
|
.add_embeds(embeds)
|
|
|
|
.add_files(attachments);
|
2023-07-15 09:18:08 +00:00
|
|
|
}
|
2024-02-14 12:22:29 +00:00
|
|
|
// or create a new embed with the content of the mooved message as one of the fields
|
|
|
|
else {
|
|
|
|
let embed = CreateEmbed::new()
|
|
|
|
.field("MOO", new_content, false)
|
|
|
|
.field("Message:\n", msg_to_moove.content.clone(), false);
|
|
|
|
|
|
|
|
new_msg = new_msg.add_embed(embed);
|
2023-07-15 10:11:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
ChannelId::new(m_channel_id).send_message(http.clone(), new_msg).await?;
|
|
|
|
|
2023-07-15 10:11:57 +00:00
|
|
|
sleep(Duration::from_secs(2)).await;
|
|
|
|
|
|
|
|
msg_to_moove.delete(http.clone()).await?;
|
|
|
|
msg.delete(http).await?;
|
2024-02-14 12:22:29 +00:00
|
|
|
Ok(())
|
2023-07-15 09:18:08 +00:00
|
|
|
}
|