Skip to content

Commit

Permalink
refactor: rename RTP command
Browse files Browse the repository at this point in the history
  • Loading branch information
imatpot committed Aug 30, 2024
1 parent b7ece3e commit 7693dee
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CLIENT_ID=1234567890987654321
CLIENT_TOKEN=U1Tr4.53cR3t.T0k3N
HOME_GUILD_ID=1234567890987654321
READY_ROLE_ID=1234567890987654321
PLAYING_ROLE_ID=1234567890987654321
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ https://discord.com/api/oauth2/authorize?permissions=277293894656&scope=bot%20ap
while inserting your client ID and keeping the scopes. Remember that the bot is
designed to *only be active in 1 server at a time.*

Please also make sure the bot's personal role is ranked *above* the managed ready role!
Please also make sure the bot's personal role is ranked *above* the managed playing role!

---

Expand All @@ -58,22 +58,22 @@ to this GitHub section.
Check if Lunaro Manager is online and how long it took to receive the ping. This
value is calculated from the system time, and may thus be inaccurate.

### `🟢 /rtp join`
### `🟢 /play now`

Adds the configured ready role to your profile.
Adds the configured playing role to your profile.

### `⭕ /rtp leave`
### `⭕ /play later`

Removes the configured ready role from your profile.
Removes the configured playing role from your profile.

### `👀 /rtp check`
### `👀 /play info`

Lists the number of members with the ready role.
Lists the number of members with the playing role.

### `⛔ /tracking pause`

Disables activity tracking for your account. This is useful because the activity
tracker will otherwise override your manually set ready status.
tracker will otherwise override your manually set playing status.

### `💡 /about`

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ services:
CLIENT_ID: ${CLIENT_ID:-example}
CLIENT_TOKEN: ${CLIENT_TOKEN:-example}
HOME_GUILD_ID: ${HOME_GUILD_ID:-example}
READY_ROLE_ID: ${READY_ROLE_ID:-example}
PLAYING_ROLE_ID: ${PLAYING_ROLE_ID:-example}
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pub mod about;
pub mod contribute;
pub mod help;
pub mod ping;
pub mod rtp;
pub mod play;
pub mod tracking;
22 changes: 11 additions & 11 deletions src/commands/rtp.rs → src/commands/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use poise::{command, CreateReply};
use crate::{
env::Environment,
types::{error::Error, poise::PoiseContext},
util::rtp,
util::play,
};

/// 🥍 Manage your RTP status
#[command(slash_command, rename = "rtp", subcommands("join", "leave", "check"))]
/// 🥍 Manage your Lunaro status
#[command(slash_command, rename = "play", subcommands("join", "leave", "check"))]
pub async fn run(_context: PoiseContext<'_>) -> Result<(), Error> {
// Handled in subcommands
Ok(())
}

/// 🟢 Equip your Arcata
#[command(slash_command)]
async fn join(context: PoiseContext<'_>) -> Result<(), Error> {
async fn now(context: PoiseContext<'_>) -> Result<(), Error> {
let env = Environment::instance();

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

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

let display_name = match &member.nick {
Some(nick) => nick,
Expand All @@ -43,7 +43,7 @@ async fn join(context: PoiseContext<'_>) -> Result<(), Error> {

/// ⭕ Unequip your Arcata
#[command(slash_command)]
async fn leave(context: PoiseContext<'_>) -> Result<(), Error> {
async fn later(context: PoiseContext<'_>) -> Result<(), Error> {
let env = Environment::instance();

let member = &mut context
Expand All @@ -63,24 +63,24 @@ async fn leave(context: PoiseContext<'_>) -> Result<(), Error> {
)))
.await?;

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

Ok(())
}

/// 👀 Check who's equipped their Arcata
#[command(slash_command)]
async fn check(context: PoiseContext<'_>) -> Result<(), Error> {
let ready_member_count = rtp::count(context.serenity_context()).await?;
async fn info(context: PoiseContext<'_>) -> Result<(), Error> {
let playing_member_count = play::count(context.serenity_context()).await?;

let verb = match ready_member_count {
let verb = match playing_member_count {
1 => "is",
_ => "are",
};

context
.send(CreateReply::default().content(format!(
"👀 There {verb} {ready_member_count} Tenno available for Lunaro"
"👀 There {verb} {playing_member_count} Tenno playing Lunaro"
)))
.await?;

Expand Down
14 changes: 7 additions & 7 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Environment {
pub home_guild_id: GuildId,

/// The role ID of the role to be given to users playing Lunaro.
pub ready_role_id: RoleId,
pub playing_role_id: RoleId,

/// The bot's Cargo package information.
pub cargo: Cargo,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Environment {
client_id: get_client_id()?.into(),
client_token: get_client_token()?,
home_guild_id: get_home_guild_id()?.into(),
ready_role_id: get_ready_role_id()?.into(),
playing_role_id: get_playing_role_id()?.into(),
cargo: get_cargo()?,
})
}
Expand Down Expand Up @@ -132,12 +132,12 @@ fn get_home_guild_id() -> Result<u64, Error> {
parse_id(&home_guild_id_string, env_name)
}

/// Fetches and validates the ready role ID environment variable.
fn get_ready_role_id() -> Result<u64, Error> {
let env_name = "READY_ROLE_ID";
let ready_role_id_string = read_variable(env_name)?;
/// Fetches and validates the playing role ID environment variable.
fn get_playing_role_id() -> Result<u64, Error> {
let env_name = "PLAYING_ROLE_ID";
let playing_role_id_string = read_variable(env_name)?;

parse_id(&ready_role_id_string, env_name)
parse_id(&playing_role_id_string, env_name)
}

/// Fetches and validates the Cargo package information.
Expand Down
14 changes: 7 additions & 7 deletions src/events/presence_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
env::Environment,
traits::config_file::ConfigFile,
types::error::Error,
util::{activity_tracking, rtp},
util::{activity_tracking, play},
};

/// Handles the presence update event.
Expand All @@ -21,17 +21,17 @@ pub async fn handle(context: Context, presence: &Presence) -> Result<(), Error>
.get_member(presence.guild_id.unwrap(), presence.user.id)
.await?;

let is_ready = member
let is_playing = member
.user
.has_role(&context, env.home_guild_id, env.ready_role_id)
.has_role(&context, env.home_guild_id, env.playing_role_id)
.await?;

let is_playing_lunaro = activity_tracking::is_playing_lunaro(presence).is_ok_and(|value| value);

if is_playing_lunaro && !is_ready {
rtp::add(member, &context).await?;
} else if !is_playing_lunaro && is_ready {
rtp::remove(member, &context).await?;
if is_playing_lunaro && !is_playing {
play::add(member, &context).await?;
} else if !is_playing_lunaro && is_playing {
play::remove(member, &context).await?;
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() {
commands::contribute::run(),
commands::help::run(),
commands::ping::run(),
commands::rtp::run(),
commands::play::run(),
commands::tracking::run(),
],

Expand Down
2 changes: 1 addition & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod activity_tracking;
pub mod data;
pub mod rtp;
pub mod play;
20 changes: 10 additions & 10 deletions src/util/rtp.rs → src/util/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use poise::serenity_prelude::{Context, Member};

use crate::{env::Environment, types::error::Error};

/// Count how many members have the ready role.
/// Count how many members have the playing role.
pub async fn count(context: &Context) -> Result<usize, Error> {
let env = Environment::instance();

Expand All @@ -11,37 +11,37 @@ pub async fn count(context: &Context) -> Result<usize, Error> {
.get_guild_members(env.home_guild_id, None, None)
.await?;

let ready_member_count = members
let playing_member_count = members
.iter()
.filter(|member| {
member
.roles
.iter()
.any(|role_id| role_id == &env.ready_role_id)
.any(|role_id| role_id == &env.playing_role_id)
})
.count();

Ok(ready_member_count)
Ok(playing_member_count)
}

/// Add the ready role to a member.
/// Add the playing role to a member.
pub async fn add(member: &mut Member, context: &Context) -> Result<(), Error> {
member
.add_role(context, Environment::instance().ready_role_id)
.add_role(context, Environment::instance().playing_role_id)
.await?;

log::debug!("Added {} to ready role", member.user.tag());
log::debug!("Added {} to playing role", member.user.tag());

Ok(())
}

/// Remove the ready role from a member.
/// Remove the playing role from a member.
pub async fn remove(member: &mut Member, context: &Context) -> Result<(), Error> {
member
.remove_role(context, Environment::instance().ready_role_id)
.remove_role(context, Environment::instance().playing_role_id)
.await?;

log::debug!("Removed {} from ready role", member.user.tag());
log::debug!("Removed {} from playing role", member.user.tag());

Ok(())
}

0 comments on commit 7693dee

Please sign in to comment.