81 lines
2.6 KiB
Rust
81 lines
2.6 KiB
Rust
use std::env;
|
|
|
|
use dotenv::dotenv;
|
|
use once_cell::sync::Lazy;
|
|
use regex::Regex;
|
|
use serenity::all::MessageFlags;
|
|
use serenity::async_trait;
|
|
use serenity::builder::EditMessage;
|
|
use serenity::model::channel::Message;
|
|
use serenity::model::gateway::Ready;
|
|
use serenity::prelude::*;
|
|
|
|
struct Handler;
|
|
|
|
#[async_trait]
|
|
impl EventHandler for Handler {
|
|
async fn message(&self, ctx: Context, mut msg: Message) {
|
|
if msg.author.bot {
|
|
return;
|
|
}
|
|
static RE: Lazy<Regex> = Lazy::new(|| {
|
|
Regex::new(r"(https://(?:www\.)?((reddit\.com)|(twitter\.com|x\.com)|(instagram\.com))(?:/[a-zA-Z0-9_-]+)+)|(\|\|)")
|
|
.unwrap()
|
|
});
|
|
|
|
let content = msg.content.to_owned();
|
|
let mut new_msg = String::new();
|
|
let mut matched = false;
|
|
RE.captures_iter(&content).for_each(|capture| {
|
|
matched = true;
|
|
|
|
if let Some(reddit) = capture.get(3) {
|
|
new_msg.push_str(&capture[0].replace(reddit.as_str(), "rxddit.com"));
|
|
} else if let Some(twitter) = capture.get(4) {
|
|
new_msg.push_str(&capture[0].replace(twitter.as_str(), "fxtwitter.com"));
|
|
} else if let Some(instagram) = capture.get(5) {
|
|
new_msg.push_str(&capture[0].replace(instagram.as_str(), "ddinstagram.com"));
|
|
}else if capture.get(6).is_some() {
|
|
new_msg.push_str("||");
|
|
}
|
|
|
|
new_msg.push(' ');
|
|
});
|
|
if matched {
|
|
if let Some(mut flags) = msg.flags {
|
|
flags.toggle(MessageFlags::SUPPRESS_EMBEDS);
|
|
let builder = EditMessage::new().flags(flags);
|
|
if let Err(why) = msg.edit(&ctx.http, builder).await {
|
|
println!("Error sending message: {why:?}");
|
|
}
|
|
}
|
|
|
|
if let Err(why) = msg.channel_id.say(&ctx.http, new_msg).await {
|
|
println!("Error sending message: {why:?}");
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn ready(&self, _: Context, ready: Ready) {
|
|
println!("{} is connected!", ready.user.name);
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
dotenv().ok();
|
|
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
|
let intents = GatewayIntents::GUILD_MESSAGES
|
|
| GatewayIntents::DIRECT_MESSAGES
|
|
| GatewayIntents::MESSAGE_CONTENT;
|
|
|
|
let mut client = Client::builder(&token, intents)
|
|
.event_handler(Handler)
|
|
.await
|
|
.expect("Err creating client");
|
|
|
|
if let Err(why) = client.start().await {
|
|
println!("Client error: {why:?}");
|
|
}
|
|
}
|