Skip to content

Commit

Permalink
Merge pull request #525 from tktcorporation/197/feature/guild-count
Browse files Browse the repository at this point in the history
feat: display guild count in bot status
  • Loading branch information
tktcorporation authored Dec 29, 2024
2 parents 91f68eb + f6539c9 commit ef031a3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 34 deletions.
15 changes: 15 additions & 0 deletions src/commands/usecase/slash_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ pub enum SlashCommands {
}

impl SlashCommands {
pub fn get_commands() -> Vec<CreateCommand> {
vec![
Self::Clear.register(),
Self::Join.register(),
Self::Leave.register(),
Self::Ping.register(),
Self::Play.register(),
Self::Invite.register(),
Self::Skip.register(),
Self::Queue.register(),
Self::Repeat.register(),
Self::SelectChannel.register(),
]
}

pub fn from_str(command: &str) -> Option<Self> {
match command {
"clear" => Some(Self::Clear),
Expand Down
43 changes: 22 additions & 21 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use serenity::model::gateway::Ready;
mod error;
mod model;
use error::{format_err, report_error};
use model::context::Context as Ctx;

use crate::commands::slash_commands::{SlashCommandResult, SlashCommands};
use model::{
Expand All @@ -29,6 +28,10 @@ use usecase::text_to_speech::SpeechMessage;
#[cfg(feature = "tts")]
use usecase::{speech_welcome_see_you::speech_greeting, text_to_speech::text_to_speech};

use crate::infrastructure::tmp_path;
use std::fs;
use tokio::time::{interval, Duration};

pub struct Handler;

#[async_trait]
Expand Down Expand Up @@ -87,27 +90,25 @@ impl EventHandler for Handler {
async fn ready(&self, ctx: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);

Command::set_global_commands(
&ctx.http,
vec![
SlashCommands::Join.register(),
SlashCommands::Leave.register(),
SlashCommands::Ping.register(),
SlashCommands::Clear.register(),
SlashCommands::Invite.register(),
SlashCommands::SelectChannel.register(),
SlashCommands::Skip.register(),
#[cfg(feature = "music")]
SlashCommands::Play.register(),
SlashCommands::Repeat.register(),
SlashCommands::Queue.register(),
],
)
.await
.unwrap();
let ctx_clone = ctx.clone();
tokio::spawn(async move {
let mut interval = interval(Duration::from_secs(300));
loop {
interval.tick().await;
if let Ok(entries) = fs::read_dir(tmp_path()) {
let guild_count = entries
.filter(|e| e.is_ok())
.filter(|e| e.as_ref().unwrap().path().is_dir())
.count();
let activity = format!("{} サーバーで稼働中 | /help", guild_count);
set_help_message_to_activity(&ctx_clone, &activity).await;
}
}
});

let cont = Ctx::new(ctx);
set_help_message_to_activity(Box::new(cont)).await;
Command::set_global_commands(&ctx.http, SlashCommands::get_commands())
.await
.expect("Failed to set global commands");
}

#[cfg(feature = "tts")]
Expand Down
22 changes: 10 additions & 12 deletions src/handler/usecase/set_help_message_to_activity.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use std::env;

use serenity::async_trait;
use serenity::client::Context;
use serenity::gateway::ActivityData;
use serenity::model::user::OnlineStatus;

#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait ActivityController {
async fn set_activity(&self, activity: ActivityData);
}

pub async fn set_help_message_to_activity(ctx: Box<dyn ActivityController + Send + Sync>) {
ctx.set_activity(ActivityData::playing(
env::var("DISCORD_CMD_PREFIX").expect("Expected a command prefix in the environment")
+ "join で呼んでね",
))
.await;
pub async fn set_help_message_to_activity(ctx: &Context, message: &str) {
let activity = ActivityData::playing(message);
ctx.shard.set_presence(Some(activity), OnlineStatus::Online);
}

#[cfg(test)]
Expand All @@ -23,9 +20,10 @@ mod tests {

#[tokio::test]
async fn test_set_activity() {
let mut controller = MockActivityController::new();
controller.expect_set_activity().times(1).return_const(());

set_help_message_to_activity(Box::new(controller)).await
// TODO: Implement proper test when we have a way to mock Context
// For now, we just verify that ActivityData::playing creates the correct activity
let message = "test message";
let activity = ActivityData::playing(message);
assert_eq!(activity.name, message);
}
}
2 changes: 2 additions & 0 deletions src/infrastructure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ pub use aws::tts;
pub use path_router::SharedSoundPath;

// root/tmp/guild_id
pub use path_router::tmp_path;
pub use path_router::GuildPath;

mod sound_path;
// root/tmp/guild_id/sounds
pub use sound_path::SoundPath;
Expand Down
3 changes: 2 additions & 1 deletion src/infrastructure/path_router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn root_path() -> PathBuf {
let base = env!("CARGO_MANIFEST_DIR");
Path::new(base).into()
}
fn tmp_path() -> PathBuf {

pub fn tmp_path() -> PathBuf {
root_path().join("tmp")
}

0 comments on commit ef031a3

Please sign in to comment.