message deletion works

This commit is contained in:
Ladislav Hano 2023-07-15 12:11:57 +02:00
parent 487ceae110
commit 2452c0abc1
3 changed files with 20 additions and 15 deletions

View file

@ -1,10 +1,12 @@
use std::sync::Arc;
use std::time::Duration;
use anyhow::{self, Context};
use poise::serenity_prelude::AttachmentType;
use serenity::builder::CreateEmbed;
use serenity::http::Http;
use serenity::model::channel::Message;
use tokio::time::sleep;
use url::Url;
use regex::Regex;
@ -26,7 +28,7 @@ pub async fn moove(http: Arc<Http>, msg: Message) -> anyhow::Result<MooveResult>
return Ok(MooveResult::NotMooveRequest);
}
let msg_to_moove = msg.referenced_message.context("no message present")?;
let msg_to_moove = msg.clone().referenced_message.context("no message present")?;
let mentioned_channel = http.get_channel(
msg.content[2..msg.content.len() - 1].parse::<u64>()
@ -34,34 +36,39 @@ pub async fn moove(http: Arc<Http>, msg: Message) -> anyhow::Result<MooveResult>
//steals all attachments, but sets all of them as Image urls, so rip actual docs etc
let attachments = msg_to_moove
.attachments
.attachments.clone()
.into_iter()
.map(|att| AttachmentType::Image(Url::parse(att.url.as_str()).unwrap()));
//steals all the embeds
let embeds: Vec<CreateEmbed> = msg_to_moove
.embeds
.embeds.clone()
.into_iter()
.map(|em| CreateEmbed::from(em))
.collect();
let mut new_content = format!("Sent by {}\n mooved {}\n", msg_to_moove.author, msg.author);
if !msg_to_moove.content.is_empty() {
mentioned_channel.send_message(http, |m| {
if attachments.len() > 0 || embeds.len() > 0 {
new_content += format!("Message:\n{}", msg_to_moove.content).as_str();
mentioned_channel.send_message(http.clone(), |m| {
m.content(new_content)
.add_embeds(embeds)
.add_files(attachments)
}).await?;
}
else if !msg_to_moove.content.is_empty() {
mentioned_channel.send_message(http.clone(), |m| {
m.add_embed(|e| {
e.field("MOO", new_content, false)
.field("Message:\n", msg_to_moove.content.clone(), false)
})
}).await?;
}
else if attachments.len() > 0 || embeds.len() > 0 {
new_content += format!("Message:\n{}", msg_to_moove.content).as_str();
mentioned_channel.send_message(http, |m| {
m.content(new_content)
.add_embeds(embeds)
.add_files(attachments)
}).await?;
}
sleep(Duration::from_secs(2)).await;
msg_to_moove.delete(http.clone()).await?;
msg.delete(http).await?;
Ok(MooveResult::Mooved)
}

View file

@ -16,7 +16,6 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
println!("Got message");
handle(ctx, msg).await;
}

View file

@ -4,7 +4,6 @@ use serenity::model::channel::Message;
use crate::commands::moove::moove;
pub async fn handle(ctx: Context, msg: Message) {
println!("In handler");
match moove(ctx.http, msg).await {
Ok(_) => return,
Err(e) => println!("ERROR: {e}")