Merge pull request #2 from ZyLacx/ver-0.0

Ver 0.0
This commit is contained in:
ZyLacx 2024-01-22 19:54:26 +01:00 committed by GitHub
commit 8e0cf4ce3c
9 changed files with 128 additions and 12 deletions

7
.gitignore vendored
View file

@ -1,6 +1,11 @@
# Build files
/target
# Tokens
.env
# Hidden files
/src/.*
.vscode
# IDE config
.vscode

View file

@ -15,3 +15,6 @@ dotenv = "0.15.0"
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "http"] }
serenity_utils = "0.7.0"
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
rand = "0.8.4"
regex = "1.9.0"
url = "2.4.0"

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

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

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

@ -0,0 +1,74 @@
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;
// 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.clone().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.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.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 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?;
}
sleep(Duration::from_secs(2)).await;
msg_to_moove.delete(http.clone()).await?;
msg.delete(http).await?;
Ok(MooveResult::Mooved)
}

View file

@ -1,3 +1,4 @@
use rand::random;
use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
@ -5,6 +6,10 @@ use serenity::prelude::*;
use util::security::dotenv_var;
use other::msg::hello;
mod message_handler;
use message_handler::handle;
mod commands;
mod util;
mod other;
@ -13,15 +18,34 @@ struct Handler;
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!ping" {
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
println!("Error sending message: {:?}", why);
}
}
handle(ctx, msg).await;
}
async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
let debug = match dotenv_var("DEBUG") {
Some(v) => v,
None => "OFF".to_string(),
};
if debug != "ON" {
let messages = [
"AAAAAAAAAAAAAAAAAAAA",
"Henlooo",
"Good day y'all!",
"May have crashed...",
"MOOOooo",
"Heyyyyy!",
"I'm baaaaack!",
"Whom'st have summoned the ancient one?",
];
let rand_num = random::<usize>() % messages.len();
let channel = ctx.http.get_channel(780439236867653635).await.unwrap().id();
match channel.say(&ctx.http, messages[rand_num]).await {
Err(e) => println!("Something went wrong: {e}"),
Ok(_) => return,
};
}
// if ready.user.name != "MOOver Debug" {
hello(ctx.http).await;
// }
@ -32,15 +56,13 @@ impl EventHandler for Handler {
async fn main() -> anyhow::Result<()> {
use anyhow::Context;
let token = dotenv_var("TOKEN").context("No TOKEN in env")?;
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.context("Failed to build client")?;
client.start().await?;
Ok(())
}

11
src/message_handler.rs Normal file
View file

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

0
src/util/embeds.rs Normal file
View file

View file

@ -6,6 +6,6 @@ pub fn dotenv_var(key: &str) -> Option<String> {
match env::var(key) {
Ok(val) => return Some(val),
Err(_) => None,
Err(_) => None
}
}

0
src/util/utilities.rs Normal file
View file