2022-06-02 11:41:45 +02:00
#![ feature(option_result_contains) ]
2022-05-30 20:43:18 +02:00
use async_trait ::async_trait ;
2022-06-02 11:41:45 +02:00
use fancy_regex ::Regex ;
2022-05-30 18:37:21 +02:00
use lazy_static ::lazy_static ;
2022-05-30 21:49:08 +02:00
use serenity ::{
model ::{
channel ::ReactionType ,
id ::{ ChannelId , EmojiId , GuildId } ,
prelude ::* ,
} ,
prelude ::* ,
Client ,
} ;
2020-08-28 09:45:19 +02:00
struct Handler ;
lazy_static! {
static ref SERVER_ID : GuildId = GuildId ( std ::env ::args ( ) . nth ( 1 ) . unwrap ( ) . parse ( ) . unwrap ( ) ) ;
2020-09-08 18:29:15 +02:00
static ref MEME_CHANNEL : ChannelId =
ChannelId ( std ::env ::args ( ) . nth ( 2 ) . unwrap ( ) . parse ( ) . unwrap ( ) ) ;
2022-06-02 11:41:45 +02:00
static ref RETARD_REGEX : Regex =
Regex ::new ( " (?<!(. | j| d|op|in|us|ng|si|tw|dd))a( |$) " ) . unwrap ( ) ;
2020-08-28 09:45:19 +02:00
}
2022-05-30 20:43:18 +02:00
#[ async_trait ]
2020-08-28 09:45:19 +02:00
impl EventHandler for Handler {
2022-05-30 20:43:18 +02:00
async fn message ( & self , ctx : Context , message : Message ) {
2022-05-30 21:49:08 +02:00
if let Err ( e ) = handle_message ( ctx , message ) . await {
eprintln! ( " Error during command execution: {e} " ) ;
2020-09-08 18:29:15 +02:00
}
2022-05-30 21:49:08 +02:00
}
}
2022-06-02 11:41:45 +02:00
fn fix_spelling ( msg : & str ) -> String {
RETARD_REGEX . replace_all ( & msg , " **er** " ) . trim ( ) . to_owned ( )
}
2022-05-30 21:49:08 +02:00
async fn handle_message ( ctx : Context , message : Message ) -> Result < ( ) , serenity ::Error > {
if message . guild_id ! = Some ( * SERVER_ID ) {
return Ok ( ( ) ) ;
}
// That other idiot who ends words with “a” instead of “er”
2022-06-02 11:41:45 +02:00
if message . author . id = = 261246789942902794
& & RETARD_REGEX . is_match ( & message . content ) . contains ( & true )
& & ! message . content . starts_with ( " a " )
{
2022-05-30 21:49:08 +02:00
message . delete ( & ctx ) . await ? ;
2022-06-02 11:41:45 +02:00
let fixed = fix_spelling ( & message . content ) ;
2022-05-30 21:49:08 +02:00
message
. channel_id
2022-06-02 11:41:45 +02:00
. say ( & ctx , & format! ( " {} : {} " , message . author . mention ( ) , fixed ) )
2022-05-30 21:49:08 +02:00
. await ? ;
}
if message . channel_id = = * MEME_CHANNEL & & is_meme ( & message ) {
react ( & ctx , & message , 748564944449962017 , " based " ) . await ? ;
react ( & ctx , & message , 748564944819060856 , " cringe " ) . await ? ;
} ;
let content = message . content_safe ( & ctx ) . await . to_lowercase ( ) ;
if content . contains ( " everyone " ) & & content . contains ( " nitro " ) & & content . contains ( " http " ) {
message . delete ( & ctx ) . await ? ;
message . channel_id . say (
& ctx ,
& format! ( " {} : your message has been deleted because it triggered my spam filter. If you believe this to be in error, please contact the mods. " , message . author . mention ( ) )
) . await ? ;
2020-10-16 18:05:39 +02:00
}
2022-05-30 21:49:08 +02:00
Ok ( ( ) )
2020-08-28 09:45:19 +02:00
}
2022-05-30 21:49:08 +02:00
async fn react (
ctx : & Context ,
msg : & Message ,
emoji : u64 ,
name : & str ,
) -> Result < ( ) , serenity ::Error > {
2020-09-08 18:29:15 +02:00
let reaction = ReactionType ::Custom {
animated : false ,
id : EmojiId ( emoji ) ,
name : Some ( name . to_string ( ) ) ,
} ;
2022-05-30 21:49:08 +02:00
msg . react ( ctx , reaction ) . await ? ;
Ok ( ( ) )
2020-08-28 10:05:52 +02:00
}
2020-09-08 18:29:15 +02:00
fn is_meme ( msg : & Message ) -> bool {
2020-10-16 18:05:39 +02:00
! msg . attachments . is_empty ( ) | | msg . content . to_lowercase ( ) . contains ( " http " )
2020-09-08 18:29:15 +02:00
}
2022-05-30 20:43:18 +02:00
#[ tokio::main ]
async fn main ( ) {
let mut client =
Client ::builder ( std ::env ::var ( " DISCORD_TOKEN " ) . expect ( " no token in environment " ) )
. event_handler ( Handler )
. await
. expect ( " Could not create client " ) ;
client . start ( ) . await . expect ( " could not start " ) ;
2020-08-28 09:45:19 +02:00
}
2022-06-02 11:41:45 +02:00
#[ cfg(test) ]
mod tests {
use super ::* ;
#[ test ]
fn fix_spelling_test ( ) {
let msg = " aba ihr meint es ja bessa zu wissen ohne euch damit auseinanda gesetzt zu haben oda zu wollen " ;
let expected = " ab**er** ihr meint es ja bess**er** zu wissen ohne euch damit auseinand**er** gesetzt zu haben od**er** zu wollen " ;
assert_eq! ( fix_spelling ( msg ) , expected ) ;
let msg = " your a bad person " ;
assert_eq! ( fix_spelling ( msg ) , msg ) ;
let msg = " china usa europa da ja manga asia etwa unsa " ;
let expected = " china usa europa da ja manga asia etwa uns**er** " ;
assert_eq! ( fix_spelling ( msg ) , expected ) ;
let msg = " guta tip " ;
let expected = " gut**er** tip " ;
assert_eq! ( fix_spelling ( msg ) , expected ) ;
}
}