MOOving works

This commit is contained in:
Ladislav Hano 2023-07-15 11:18:08 +02:00
parent 573486537c
commit 90674f801d
6 changed files with 89 additions and 69 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
.env
.vscode

1
src/commands/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod moove;

67
src/commands/moove.rs Normal file
View file

@ -0,0 +1,67 @@
use std::sync::Arc;
use anyhow::{self, Context};
use poise::serenity_prelude::AttachmentType;
use serenity::builder::CreateEmbed;
use serenity::http::Http;
use serenity::model::channel::Message;
use url::Url;
use regex::Regex;
// 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()
pub enum MooveResult {
Mooved,
NotMooveRequest
}
pub async fn moove(http: Arc<Http>, msg: Message) -> anyhow::Result<MooveResult> {
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() {
return Ok(MooveResult::NotMooveRequest);
}
let msg_to_moove = msg.referenced_message.context("no message present")?;
let mentioned_channel = http.get_channel(
msg.content[2..msg.content.len() - 1].parse::<u64>()
.unwrap()).await?.id();
//steals all attachments, but sets all of them as Image urls, so rip actual docs etc
let attachments = msg_to_moove
.attachments
.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
.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| {
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?;
}
Ok(MooveResult::Mooved)
}

View file

@ -1,4 +1,3 @@
use moove::moove;
use rand::random;
use serenity::async_trait;
use serenity::model::channel::Message;
@ -6,7 +5,10 @@ use serenity::model::gateway::Ready;
use serenity::prelude::*;
use util::security::dotenv_var;
mod moove;
mod message_handler;
use message_handler::handle;
mod commands;
mod util;
struct Handler;
@ -14,15 +16,8 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
match moove(ctx.http.clone(), msg.clone()).await {
Ok(_) => (),
Err(e) => println!("ERROR MOVING!{e}"),
}
if msg.content == "!ping" {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
println!("Error sending message: {:?}", why);
}
}
println!("Got message");
handle(ctx, msg).await;
}
async fn ready(&self, ctx: Context, ready: Ready) {
@ -31,7 +26,7 @@ impl EventHandler for Handler {
Some(v) => v,
None => "OFF".to_string(),
};
if debug == "ON" {
if debug != "ON" {
let messages = [
"AAAAAAAAAAAAAAAAAAAA",
"Henlooo",

12
src/message_handler.rs Normal file
View file

@ -0,0 +1,12 @@
use serenity::client::Context;
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}")
};
}

View file

@ -1,56 +0,0 @@
use std::sync::Arc;
use anyhow::{self, Context};
use poise::serenity_prelude::AttachmentType;
use serenity::builder::{CreateEmbed, CreateMessage};
use serenity::http::Http;
use serenity::model::channel::Attachment;
use serenity::model::channel::Message;
use serenity::model::mention;
use url::Url;
// use regex::Regex;
// Checks if the message should be mooved, if not returns Ok(0)
// If the message should be mooved, try to move it and return Ok(1) if mooved succesfully
// else returns Err()
pub async fn moove(http: Arc<Http>, msg: Message) -> anyhow::Result<()> {
let channel_mentions = msg.mention_channels;
let words = msg.content.trim().split_whitespace().count();
// let re = Regex::new(r"<#[0-9]*>$").unwrap();
// if re.captures(content)
if channel_mentions.len() != 1 || words != 1 || msg.content.is_empty() {
anyhow::bail!("no message worth processing");
}
let msg_to_moove = msg.referenced_message.context("no message present")?;
let mentioned_channel = channel_mentions[0].id;
//steals all attachments, but sets all of them as Image urls, so rip actual docs etc
let attachments = msg_to_moove
.attachments
.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
.into_iter()
.map(|em| CreateEmbed::from(em))
.collect();
let mut new_content = format!("Sent by {}\n mooved {}\n", msg_to_moove.author, msg.author);
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?;
Ok(())
}