commit
8e0cf4ce3c
9 changed files with 128 additions and 12 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,6 +1,11 @@
|
||||||
|
# Build files
|
||||||
/target
|
/target
|
||||||
|
|
||||||
|
# Tokens
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# Hidden files
|
||||||
/src/.*
|
/src/.*
|
||||||
|
|
||||||
|
# IDE config
|
||||||
.vscode
|
.vscode
|
|
@ -15,3 +15,6 @@ dotenv = "0.15.0"
|
||||||
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "http"] }
|
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "http"] }
|
||||||
serenity_utils = "0.7.0"
|
serenity_utils = "0.7.0"
|
||||||
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
|
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
1
src/commands/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod moove;
|
74
src/commands/moove.rs
Normal file
74
src/commands/moove.rs
Normal 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)
|
||||||
|
}
|
38
src/main.rs
38
src/main.rs
|
@ -1,3 +1,4 @@
|
||||||
|
use rand::random;
|
||||||
use serenity::async_trait;
|
use serenity::async_trait;
|
||||||
use serenity::model::channel::Message;
|
use serenity::model::channel::Message;
|
||||||
use serenity::model::gateway::Ready;
|
use serenity::model::gateway::Ready;
|
||||||
|
@ -5,6 +6,10 @@ use serenity::prelude::*;
|
||||||
use util::security::dotenv_var;
|
use util::security::dotenv_var;
|
||||||
use other::msg::hello;
|
use other::msg::hello;
|
||||||
|
|
||||||
|
mod message_handler;
|
||||||
|
use message_handler::handle;
|
||||||
|
|
||||||
|
mod commands;
|
||||||
mod util;
|
mod util;
|
||||||
mod other;
|
mod other;
|
||||||
|
|
||||||
|
@ -13,15 +18,34 @@ struct Handler;
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
async fn message(&self, ctx: Context, msg: Message) {
|
async fn message(&self, ctx: Context, msg: Message) {
|
||||||
if msg.content == "!ping" {
|
handle(ctx, msg).await;
|
||||||
if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await {
|
|
||||||
println!("Error sending message: {:?}", why);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||||
println!("{} is connected!", ready.user.name);
|
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" {
|
// if ready.user.name != "MOOver Debug" {
|
||||||
hello(ctx.http).await;
|
hello(ctx.http).await;
|
||||||
// }
|
// }
|
||||||
|
@ -32,9 +56,7 @@ impl EventHandler for Handler {
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
let token = dotenv_var("TOKEN").context("No TOKEN in env")?;
|
let token = dotenv_var("TOKEN").context("No TOKEN in env")?;
|
||||||
let intents = GatewayIntents::GUILD_MESSAGES
|
let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
|
||||||
| GatewayIntents::DIRECT_MESSAGES
|
|
||||||
| GatewayIntents::MESSAGE_CONTENT;
|
|
||||||
|
|
||||||
let mut client = Client::builder(&token, intents)
|
let mut client = Client::builder(&token, intents)
|
||||||
.event_handler(Handler)
|
.event_handler(Handler)
|
||||||
|
|
11
src/message_handler.rs
Normal file
11
src/message_handler.rs
Normal 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
0
src/util/embeds.rs
Normal file
|
@ -6,6 +6,6 @@ pub fn dotenv_var(key: &str) -> Option<String> {
|
||||||
|
|
||||||
match env::var(key) {
|
match env::var(key) {
|
||||||
Ok(val) => return Some(val),
|
Ok(val) => return Some(val),
|
||||||
Err(_) => None,
|
Err(_) => None
|
||||||
}
|
}
|
||||||
}
|
}
|
0
src/util/utilities.rs
Normal file
0
src/util/utilities.rs
Normal file
Loading…
Reference in a new issue