mirror of
https://github.com/revanced/revanced-discord-bot.git
synced 2025-05-02 15:34:24 +02:00
refactor: simplify Data
This commit is contained in:
parent
3f05f8cd92
commit
204e7e6194
@ -10,7 +10,7 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
|
||||
// Use the embed color from the updated configuration
|
||||
let embed_color = configuration.general.embed_color;
|
||||
// Also save the new configuration to the user data
|
||||
*ctx.data().write().await.configuration.write().await = configuration;
|
||||
ctx.data().write().await.configuration = configuration;
|
||||
|
||||
debug!("{} reloaded the configuration.", ctx.author().name);
|
||||
|
||||
@ -29,15 +29,7 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> {
|
||||
pub async fn stop(ctx: Context<'_>) -> Result<(), Error> {
|
||||
debug!("{} stopped the bot.", ctx.author().name);
|
||||
|
||||
let color = ctx
|
||||
.data()
|
||||
.read()
|
||||
.await
|
||||
.configuration
|
||||
.read()
|
||||
.await
|
||||
.general
|
||||
.embed_color;
|
||||
let color = ctx.data().read().await.configuration.general.embed_color;
|
||||
ctx.send(|f| {
|
||||
f.ephemeral(true)
|
||||
.embed(|f| f.description("Stopped the bot.").color(color))
|
||||
|
@ -16,9 +16,9 @@ pub async fn unmute(
|
||||
ctx.defer().await.expect("Failed to defer");
|
||||
|
||||
let data = &ctx.data().read().await;
|
||||
let configuration = data.configuration.read().await;
|
||||
let configuration = &data.configuration;
|
||||
|
||||
if let Some(pending_unmute) = data.pending_unmutes.read().await.get(&member.user.id.0) {
|
||||
if let Some(pending_unmute) = data.pending_unmutes.lock().await.get(&member.user.id.0) {
|
||||
trace!("Cancelling pending unmute for {}", member.user.id.0);
|
||||
pending_unmute.abort();
|
||||
}
|
||||
@ -83,7 +83,7 @@ pub async fn mute(
|
||||
let unmute_time = now + mute_duration;
|
||||
|
||||
let data = ctx.data().read().await;
|
||||
let configuration = data.configuration.read().await;
|
||||
let configuration = &data.configuration;
|
||||
let embed_color = configuration.general.embed_color;
|
||||
let mute = &configuration.general.mute;
|
||||
let mute_role_id = mute.role;
|
||||
@ -149,22 +149,22 @@ pub async fn mute(
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(pending_unmute) = data.pending_unmutes.read().await.get(&member.user.id.0) {
|
||||
let mut pending_unmute = data.pending_unmutes.lock().await;
|
||||
if let Some(pending_unmute) = pending_unmute.get(&member.user.id.0) {
|
||||
trace!("Cancelling pending unmute for {}", member.user.id.0);
|
||||
pending_unmute.abort();
|
||||
}
|
||||
|
||||
data.pending_unmutes.write().await.insert(
|
||||
member.user.id.0,
|
||||
queue_unmute_member(
|
||||
ctx.discord(),
|
||||
&data.database,
|
||||
&member,
|
||||
mute_role_id,
|
||||
mute_duration.num_seconds() as u64,
|
||||
),
|
||||
let r = queue_unmute_member(
|
||||
ctx.discord(),
|
||||
&data.database,
|
||||
&member,
|
||||
mute_role_id,
|
||||
mute_duration.num_seconds() as u64,
|
||||
);
|
||||
|
||||
pending_unmute.insert(member.user.id.0, r);
|
||||
|
||||
respond_mute_command(
|
||||
&ctx,
|
||||
ModerationKind::Mute(
|
||||
@ -197,7 +197,7 @@ pub async fn purge(
|
||||
const MAX_BULK_DELETE_AGO_SECS: i64 = 60 * 60 * 24 * 14;
|
||||
|
||||
let data = ctx.data().read().await;
|
||||
let configuration = data.configuration.read().await;
|
||||
let configuration = &data.configuration;
|
||||
let embed_color = configuration.general.embed_color;
|
||||
let channel = ctx.channel_id();
|
||||
let too_old_timestamp = Utc::now().timestamp() - MAX_BULK_DELETE_AGO_SECS;
|
||||
|
@ -20,8 +20,6 @@ pub async fn message_create(ctx: &serenity::Context, new_message: &serenity::Mes
|
||||
.read()
|
||||
.await
|
||||
.configuration
|
||||
.read()
|
||||
.await
|
||||
.message_responses
|
||||
.iter()
|
||||
.find(|&response| {
|
||||
|
@ -14,11 +14,7 @@ pub async fn thread_create(ctx: &serenity::Context, thread: &serenity::GuildChan
|
||||
let data_lock = get_data_lock(ctx).await;
|
||||
let configuration_lock = data_lock.read().await;
|
||||
|
||||
let thread_introductions = &configuration_lock
|
||||
.configuration
|
||||
.read()
|
||||
.await
|
||||
.thread_introductions;
|
||||
let thread_introductions = &configuration_lock.configuration.thread_introductions;
|
||||
|
||||
if let Some(introducer) = thread_introductions.iter().find(|introducer| {
|
||||
introducer
|
||||
|
28
src/main.rs
28
src/main.rs
@ -5,7 +5,7 @@ use std::sync::Arc;
|
||||
use commands::{configuration, moderation};
|
||||
use db::database::Database;
|
||||
use events::Handler;
|
||||
use poise::serenity_prelude::{self as serenity, RwLock, UserId};
|
||||
use poise::serenity_prelude::{self as serenity, Mutex, RwLock, UserId};
|
||||
use tokio::task::JoinHandle;
|
||||
use tracing::{error, trace};
|
||||
use utils::bot::load_configuration;
|
||||
@ -27,9 +27,9 @@ impl serenity::TypeMapKey for Data {
|
||||
}
|
||||
|
||||
pub struct Data {
|
||||
configuration: Arc<RwLock<Configuration>>,
|
||||
database: Arc<Database>,
|
||||
pending_unmutes: Arc<RwLock<HashMap<u64, JoinHandle<Option<Error>>>>>,
|
||||
configuration: Configuration,
|
||||
database: Database,
|
||||
pending_unmutes: Mutex<HashMap<u64, JoinHandle<Option<Error>>>>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@ -64,16 +64,14 @@ async fn main() {
|
||||
.collect();
|
||||
|
||||
let data = Arc::new(RwLock::new(Data {
|
||||
configuration: Arc::new(RwLock::new(configuration)),
|
||||
database: Arc::new(
|
||||
Database::new(
|
||||
&env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"),
|
||||
"revanced_discord_bot",
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
),
|
||||
pending_unmutes: Arc::new(RwLock::new(HashMap::new())),
|
||||
configuration,
|
||||
database: Database::new(
|
||||
&env::var("MONGODB_URI").expect("MONGODB_URI environment variable not set"),
|
||||
"revanced_discord_bot",
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
pending_unmutes: Mutex::new(HashMap::new()),
|
||||
}));
|
||||
|
||||
let handler = Arc::new(Handler::new(
|
||||
@ -91,7 +89,7 @@ async fn main() {
|
||||
Box::pin(async move {
|
||||
if let Some(member) = ctx.author_member().await {
|
||||
let data_lock = &ctx.data().read().await;
|
||||
let configuration = &data_lock.configuration.read().await;
|
||||
let configuration = &data_lock.configuration;
|
||||
let administrators = &configuration.administrators;
|
||||
|
||||
if !(administrators
|
||||
|
Loading…
x
Reference in New Issue
Block a user