feat: decancer user names

This commit is contained in:
oSumAtrIX 2022-08-09 18:02:52 +02:00
parent ba7b82a6de
commit 28a19c4120
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
6 changed files with 84 additions and 23 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
@ -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;
}
@ -59,12 +63,16 @@ 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 })
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 })
self.dispatch_poise_event(&ctx, &poise::Event::InteractionCreate {
interaction,
})
.await;
}
@ -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 {
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;
}
}

View File

@ -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;
@ -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

View File

@ -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")
}
@ -30,3 +34,24 @@ impl PoiseEmbed for crate::model::application::Embed {
.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();
}