Skip to content

Commit

Permalink
feat: allow tracking config when setting status manually
Browse files Browse the repository at this point in the history
  • Loading branch information
imatpot committed Aug 30, 2024
1 parent 0ac5d28 commit 97a189b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ value is calculated from the system time, and may thus be inaccurate.
### `🟢 /play now`

Adds the configured playing role to your profile.
You can optionally disable Lunaro tracking for your account at the same time.

### `⭕ /play later`

Removes the configured playing role from your profile.
You can optionally re-enable Lunaro tracking for your account at the same time.

### `👀 /play info`

Expand Down
38 changes: 26 additions & 12 deletions src/commands/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use poise::{command, CreateReply};
use crate::{
env::Environment,
types::{error::Error, poise::PoiseContext},
util::play,
util::{lunaro_tracking, play},
};

/// 🥍 Manage your Lunaro status
Expand All @@ -15,7 +15,11 @@ pub async fn run(_context: PoiseContext<'_>) -> Result<(), Error> {

/// 🟢 Equip your Arcata
#[command(slash_command)]
async fn now(context: PoiseContext<'_>) -> Result<(), Error> {
async fn now(
context: PoiseContext<'_>,
#[description = "Whether you also want to disable Lunaro tracking for the time being"]
disable_lunaro_tracking: bool,
) -> Result<(), Error> {
let env = Environment::instance();

let member = &mut context
Expand All @@ -24,6 +28,10 @@ async fn now(context: PoiseContext<'_>) -> Result<(), Error> {
.get_member(env.home_guild_id.into(), context.author().id)
.await?;

if disable_lunaro_tracking {
lunaro_tracking::deny_for(&member.user).await?;
}

play::add(member, context.serenity_context()).await?;

let display_name = match &member.nick {
Expand All @@ -32,18 +40,19 @@ async fn now(context: PoiseContext<'_>) -> Result<(), Error> {
};

context
.send(
CreateReply::default()
.content(format!("🟢 {display_name} is now available for Lunaro")),
)
.send(CreateReply::default().content(format!("🟢 {display_name} is now playing Lunaro")))
.await?;

Ok(())
}

/// ⭕ Unequip your Arcata
#[command(slash_command)]
async fn later(context: PoiseContext<'_>) -> Result<(), Error> {
async fn later(
context: PoiseContext<'_>,
#[description = "Whether you also want to enable Lunaro tracking again"]
reenable_lunaro_tracking: bool,
) -> Result<(), Error> {
let env = Environment::instance();

let member = &mut context
Expand All @@ -52,19 +61,24 @@ async fn later(context: PoiseContext<'_>) -> Result<(), Error> {
.get_member(env.home_guild_id, context.author().id)
.await?;

if reenable_lunaro_tracking {
lunaro_tracking::allow_for(&member.user).await?;
}

play::remove(member, context.serenity_context()).await?;

let display_name = match &member.nick {
Some(nick) => nick,
None => &member.user.name,
};

context
.send(CreateReply::default().content(format!(
"⭕ {display_name} is no longer available for Lunaro"
)))
.send(
CreateReply::default()
.content(format!("⭕ {display_name} is no longer playing Lunaro")),
)
.await?;

play::remove(member, context.serenity_context()).await?;

Ok(())
}

Expand Down
11 changes: 2 additions & 9 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{env, sync::OnceLock};

use dotenv::dotenv;
use poise::serenity_prelude::{GuildId, RoleId, UserId};
use poise::serenity_prelude::{ApplicationId, GuildId, RoleId};
use regex::Regex;
use serde::Deserialize;

Expand All @@ -13,10 +13,8 @@ static CARGO_LOCK_PATH: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"),
/// Contains read & validated environment variables.
#[derive(Default, Debug)]
pub struct Environment {
#[allow(unused)]

/// The bot's Discord user ID.
pub client_id: UserId,
pub client_id: ApplicationId,

/// Token to connect to the Discord API.
pub client_token: String,
Expand Down Expand Up @@ -116,11 +114,6 @@ fn get_client_token() -> Result<String, Error> {
let env_name = "CLIENT_TOKEN";
let client_token = read_variable(env_name)?;

// FIXME Discord changed their tokens again, so this check no longer works
// if validate_token(&client_token).is_err() {
// return Err(EnvironmentError::InvalidEnvironmentVariable(env_name.to_string()).into());
// }

Ok(client_token)
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ async fn main() {

let intents = GatewayIntents::GUILD_MESSAGES | GatewayIntents::GUILD_MESSAGE_REACTIONS;

let client_builder = Client::builder(&env.client_token, intents)
let client_builder = Client::builder(&env.client_token.trim(), intents)
.application_id(env.client_id)
.framework(framework)
.await;

Expand Down

0 comments on commit 97a189b

Please sign in to comment.