Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: decancer user names
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 9, 2022
1 parent ba7b82a commit 28a19c4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/commands/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{utils::load_configuration, Context, Error};
use tracing::debug;

use crate::utils::load_configuration;
use crate::{Context, Error};

#[poise::command(slash_command, prefix_command)]
pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
// Update the configuration
Expand Down
5 changes: 5 additions & 0 deletions src/events/guild_member_addition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use super::*;

pub async fn guild_member_addition(ctx: &serenity::Context, new_member: &serenity::Member) {
crate::utils::cure(ctx, new_member).await;
}
9 changes: 9 additions & 0 deletions src/events/guild_member_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

pub async fn guild_member_update(
ctx: &serenity::Context,
_old_if_available: &Option<serenity::Member>,
new: &serenity::Member,
) {
crate::utils::cure(ctx, new).await;
}
50 changes: 33 additions & 17 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, ShardManager, UserId};
use std::sync::Arc;

use crate::{model::application::Configuration, Error};
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, ShardManager, UserId};

use crate::model::application::Configuration;
use crate::Error;

mod guild_member_addition;
mod guild_member_update;
mod message_create;
mod thread_create;

Expand Down Expand Up @@ -43,7 +47,7 @@ impl<T: Send + Sync> Handler<T> {
bot_id: self.bot_id.read().await.unwrap(),
options: &self.options,
user_data: &self.data,
shard_manager: &(*self.shard_manager.read().await).clone().unwrap(), // Shard manager can be read between all poise events without locks
shard_manager: &(*self.shard_manager.read().await).clone().unwrap(), /* Shard manager can be read between all poise events without locks */
};
poise::dispatch_event(framework_data, ctx, event).await;
}
Expand All @@ -59,13 +63,17 @@ impl serenity::EventHandler for Handler<Arc<RwLock<Configuration>>> {
async fn message(&self, ctx: serenity::Context, new_message: serenity::Message) {
message_create::message_create(&ctx, &new_message).await;

self.dispatch_poise_event(&ctx, &poise::Event::Message { new_message })
.await;
self.dispatch_poise_event(&ctx, &poise::Event::Message {
new_message,
})
.await;
}

async fn interaction_create(&self, ctx: serenity::Context, interaction: serenity::Interaction) {
self.dispatch_poise_event(&ctx, &poise::Event::InteractionCreate { interaction })
.await;
self.dispatch_poise_event(&ctx, &poise::Event::InteractionCreate {
interaction,
})
.await;
}

async fn message_update(
Expand All @@ -75,20 +83,28 @@ impl serenity::EventHandler for Handler<Arc<RwLock<Configuration>>> {
new: Option<serenity::Message>,
event: serenity::MessageUpdateEvent,
) {
self.dispatch_poise_event(
&ctx,
&poise::Event::MessageUpdate {
old_if_available,
new,
event,
},
)
self.dispatch_poise_event(&ctx, &poise::Event::MessageUpdate {
old_if_available,
new,
event,
})
.await;
}

async fn thread_create(&self, ctx: serenity::Context, thread: serenity::GuildChannel) {
thread_create::thread_create(&ctx, &thread).await;
self.dispatch_poise_event(&ctx, &poise::Event::ThreadCreate { thread })
.await;
}

async fn guild_member_addition(&self, ctx: serenity::Context, new_member: serenity::Member) {
guild_member_addition::guild_member_addition(&ctx, &new_member).await;
}

async fn guild_member_update(
&self,
ctx: serenity::Context,
old_if_available: Option<serenity::Member>,
new: serenity::Member,
) {
guild_member_update::guild_member_update(&ctx, &old_if_available, &new).await;
}
}
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::model::application::Configuration;
use std::{env, sync::Arc};
use std::env;
use std::sync::Arc;

use commands::configuration;
use events::Handler;
use poise::serenity_prelude::{self as serenity, RwLock};
use utils::load_configuration;

use crate::model::application::Configuration;

mod commands;
mod events;
mod logger;
Expand Down Expand Up @@ -87,7 +89,9 @@ async fn main() {
let mut client = serenity::Client::builder(
env::var("DISCORD_AUTHORIZATION_TOKEN")
.expect("Could not load Discord authorization token"),
serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT,
serenity::GatewayIntents::non_privileged()
| serenity::GatewayIntents::MESSAGE_CONTENT
| serenity::GatewayIntents::GUILD_MEMBERS,
)
.event_handler_arc(handler.clone())
.await
Expand All @@ -104,4 +108,4 @@ async fn main() {
.await;

client.start().await.unwrap();
}
}
27 changes: 26 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use poise::serenity_prelude::CreateEmbed;
use decancer::Decancer;
use poise::serenity_prelude::{self as serenity, CreateEmbed, RwLock};
use tracing::info;

use crate::model::application::Configuration;

const DECANCER: Decancer = Decancer::new();

pub(crate) fn load_configuration() -> Configuration {
Configuration::load().expect("Failed to load configuration")
}
Expand Down Expand Up @@ -29,4 +33,25 @@ impl PoiseEmbed for crate::model::application::Embed {
.image(self.image.url)
.author(|a| a.name(self.author.name).icon_url(self.author.icon_url))
}
}

pub async fn cure(ctx: &serenity::Context, member: &serenity::Member) {
let display_name = member.display_name();
let name = display_name.to_string();

let cured_user_name = DECANCER.cure(&name);

if name == cured_user_name {
return; // username is already cured
}

info!("Cured user {}", name);

member
.guild_id
.edit_member(&ctx.http, member.user.id, |edit_member| {
edit_member.nickname(cured_user_name)
})
.await
.unwrap();
}

0 comments on commit 28a19c4

Please sign in to comment.