Skip to content

Commit

Permalink
Merge pull request #6 from kszinhu/feat/debug-level
Browse files Browse the repository at this point in the history
feat: debug level
  • Loading branch information
kszinhu authored Aug 3, 2023
2 parents 7009c82 + e352a97 commit b78b029
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 125 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DISCORD_TOKEN={DISCORD_TOKEN}
# path to the database file (.yml)
DATABASE_PATH={DATABASE_PATH}
DEBUG=
# debug levels are "minimal", "info", "success", "error", "verbose"
DEBUG={DEBUG}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#----------------
FROM rust:1.71.0-alpine3.17 as builder

# System dependencies, update pkg-config and libssl-dev
# System dependencies
RUN apk add --no-cache \
build-base \
cmake \
Expand Down
11 changes: 2 additions & 9 deletions src/commands/radio/consumer.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
use crate::internal::debug::{log_message, STATUS_ERROR};

use super::Radio;

use rust_i18n::t;
use songbird::{input::Input, ytdl};

pub async fn consumer(radio: Radio) -> Result<Input, String> {
let url = radio.get_url();
let url = radio.get_url().unwrap();
let input = ytdl(&url).await;

match input {
Ok(input) => Ok(input),
Err(why) => {
log_message(&format!("Error starting source: {}", why), &STATUS_ERROR);

Err(t!("commands.radio.connection_error"))
}
Err(why) => Err(why.to_string()),
}
}
41 changes: 23 additions & 18 deletions src/commands/radio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod consumer;

use crate::{
events::voice::join,
internal::debug::{log_message, STATUS_INFO},
internal::debug::{log_message, MessageTypes},
};

use rust_i18n::t;
Expand All @@ -28,16 +28,18 @@ pub enum Radio {
}

impl Radio {
pub fn get_url(&self) -> String {
pub fn get_url(&self) -> Option<String> {
match self {
Radio::CanoaGrandeFM => {
"https://servidor39-4.brlogic.com:8300/live?source=website".to_string()
Some("https://servidor39-4.brlogic.com:8300/live?source=website".to_string())
}
Radio::TupiFM => "https://ice.fabricahost.com.br/topfmbauru".to_string(),
Radio::EightyNineFM => "https://r13.ciclano.io:15223/stream".to_string(),
Radio::EightyEightFM => "http://cast.hoost.com.br:8803/live.m3u".to_string(),
Radio::NinetyFourFm => "https://cast2.hoost.com.br:28456/stream".to_string(),
Radio::PingoNosIFs => "unknown".to_string(),
Radio::TupiFM => Some("https://ice.fabricahost.com.br/topfmbauru".to_string()),
Radio::EightyNineFM => Some("https://r13.ciclano.io:15223/stream".to_string()),
Radio::EightyEightFM => Some("http://cast.hoost.com.br:8803/live.m3u".to_string()),
Radio::NinetyFourFm => {
Some("https://cast2.hoost.com.br:28456/stream?1691035067242".to_string())
}
Radio::PingoNosIFs => None,
}
}
pub fn to_string(&self) -> String {
Expand Down Expand Up @@ -90,23 +92,22 @@ pub async fn run(
}
};

if debug {
log_message(&format!("Radio: {}", radio.to_string()), &STATUS_INFO);
}

let manager = songbird::get(ctx)
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();

if debug {
log_message("Getting voice channel", &STATUS_INFO);
log_message(
format!("Radio: {}", radio.to_string()).as_str(),
MessageTypes::Debug,
);
}

let _ = join(&ctx, &guild, &user_id).await;
join(ctx, guild, user_id).await?;

if debug {
log_message("Joined voice channel", &STATUS_INFO);
log_message("Joined voice channel successfully", MessageTypes::Debug);
}

if let Some(handler_lock) = manager.get(guild.id) {
Expand All @@ -115,15 +116,19 @@ pub async fn run(
let source = match consumer::consumer(radio).await {
Ok(source) => source,
Err(why) => {
println!("Error starting source: {}", why);
return Ok(why);
log_message(
format!("Error while getting source: {}", why).as_str(),
MessageTypes::Error,
);

return Ok(t!("commands.radio.connection_error"));
}
};

handler.play_source(source);
} else {
if debug {
log_message("User not connected to a voice channel", &STATUS_INFO);
log_message("User not connected to a voice channel", MessageTypes::Debug);
}

return Ok(t!("commands.radio.user_not_connected"));
Expand Down
10 changes: 5 additions & 5 deletions src/database/locale.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::BorrowMut;

use super::{get_database, save_database};
use crate::internal::debug::{log_message, STATUS_ERROR, STATUS_INFO};
use crate::internal::debug::{log_message, MessageTypes};

use rust_i18n::{available_locales, set_locale};
use serenity::model::prelude::GuildId;
Expand Down Expand Up @@ -31,13 +31,13 @@ pub fn apply_locale(new_locale: &str, guild_id: &GuildId, is_preflight: bool) {
save_database(local_database.lock().unwrap().borrow_mut());

log_message(
&format!("Applied locale {} for guild {}", new_locale, guild_id),
&STATUS_INFO,
format!("Applied locale {} for guild {}", new_locale, guild_id).as_str(),
MessageTypes::Success,
);
} else {
log_message(
&format!("Locale {} not available for guild {}", new_locale, guild_id),
&STATUS_ERROR,
format!("Locale {} not available for guild {}", new_locale, guild_id).as_str(),
MessageTypes::Failed,
);
}
}
51 changes: 33 additions & 18 deletions src/events/voice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::internal::debug::{log_message, STATUS_ERROR, STATUS_INFO};
use crate::internal::debug::{log_message, MessageTypes};

use rust_i18n::t;

Expand All @@ -7,40 +7,55 @@ use serenity::model::prelude::{Guild, UserId};
use serenity::prelude::Context;

pub async fn join(ctx: &Context, guild: &Guild, user_id: &UserId) -> CommandResult<String> {
let debug = std::env::var("DEBUG").is_ok();
let channel_id = guild.voice_states.get(user_id).unwrap().channel_id;

let connect_to = match channel_id {
Some(channel) => channel,
None => {
log_message(&format!("User is not in a voice channel"), &STATUS_ERROR);
log_message(
format!("User is not in a voice channel").as_str(),
MessageTypes::Debug,
);

return Ok(t!("commands.voice.user_not_connected"));
}
};

println!("Connecting to {:?}", connect_to);
if debug {
log_message(
format!("Connecting to voice channel: {}", connect_to).as_str(),
MessageTypes::Debug,
);
}

let manager = songbird::get(ctx)
.await
.expect("Songbird Voice client placed in at initialisation.")
.clone();

println!("Manager: {:?}", manager);
if debug {
log_message(
format!("Manager: {:?}", manager).as_str(),
MessageTypes::Debug,
);
}

let handler = manager.join(guild.id, connect_to).await;

println!("Handler: {:?}", handler);

match handler.1 {
Ok(_) => {}
Err(why) => {
log_message(&format!("Failed: {:?}", why), &STATUS_ERROR);
log_message(format!("Failed: {:?}", why).as_str(), MessageTypes::Error);

return Ok(t!("commands.voice.join_failed"));
}
}

log_message(&format!("Joined voice channel"), &STATUS_INFO);
log_message(
format!("Joined voice channel").as_str(),
MessageTypes::Success,
);

Ok(t!("commands.voice.join"))
}
Expand All @@ -57,8 +72,8 @@ pub async fn mute(ctx: &Context, guild: &Guild, _user_id: &UserId) -> CommandRes
Some(handler) => handler,
None => {
log_message(
&format!("Bot not connected to a voice channel"),
&STATUS_ERROR,
format!("Bot not connected to a voice channel").as_str(),
MessageTypes::Failed,
);

return Ok(t!("commands.voice.bot_not_connected"));
Expand All @@ -69,11 +84,11 @@ pub async fn mute(ctx: &Context, guild: &Guild, _user_id: &UserId) -> CommandRes

if handler.is_mute() {
if debug {
log_message(&format!("User already muted"), &STATUS_ERROR);
log_message(format!("User already muted").as_str(), MessageTypes::Debug);
}
} else {
if let Err(why) = handler.mute(true).await {
log_message(&format!("Failed: {:?}", why), &STATUS_ERROR);
log_message(format!("Failed: {:?}", why).as_str(), MessageTypes::Error);
}
}

Expand All @@ -90,8 +105,8 @@ pub async fn unmute(ctx: &Context, guild: &Guild, _user_id: &UserId) -> CommandR
Some(handler) => handler,
None => {
log_message(
&format!("Bot not connected to a voice channel"),
&STATUS_ERROR,
format!("Bot not connected to a voice channel").as_str(),
MessageTypes::Failed,
);

return Ok(t!("commands.voice.bot_not_connected"));
Expand All @@ -102,7 +117,7 @@ pub async fn unmute(ctx: &Context, guild: &Guild, _user_id: &UserId) -> CommandR

if handler.is_mute() {
if let Err(why) = handler.mute(false).await {
log_message(&format!("Failed: {:?}", why), &STATUS_ERROR);
log_message(format!("Failed: {:?}", why).as_str(), MessageTypes::Error);
}
}

Expand All @@ -118,12 +133,12 @@ pub async fn leave(ctx: &Context, guild: &Guild, _user_id: &UserId) -> CommandRe

if has_handler {
if let Err(why) = manager.remove(guild.id).await {
log_message(&format!("Failed: {:?}", why), &STATUS_ERROR);
log_message(format!("Failed: {:?}", why).as_str(), MessageTypes::Error);
}
} else {
log_message(
&format!("Bot not connected to a voice channel"),
&STATUS_ERROR,
format!("Bot not connected to a voice channel").as_str(),
MessageTypes::Failed,
);

return Ok(t!("commands.voice.bot_not_connected"));
Expand Down
7 changes: 5 additions & 2 deletions src/integrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::internal::debug::{log_message, STATUS_INFO};
use crate::internal::debug::{log_message, MessageTypes};

use serenity::async_trait;
use serenity::model::channel::Message;
Expand All @@ -11,7 +11,10 @@ pub fn integration_callback(
name: &str,
callback: Box<dyn CallbackFn + Send + Sync>,
) -> Box<dyn CallbackFn + Send + Sync> {
log_message(&format!("Running integration {}", name), &STATUS_INFO);
log_message(
format!("Running integration {}", name).as_str(),
MessageTypes::Info,
);

callback
}
Expand Down
4 changes: 2 additions & 2 deletions src/interactions/chat/love.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::interactions::{CallbackFn, Interaction, InteractionType};
use crate::internal::debug::{log_message, STATUS_ERROR};
use crate::internal::debug::{log_message, MessageTypes};
use crate::internal::users::USERS;

use std::cell::RefCell;
Expand Down Expand Up @@ -50,7 +50,7 @@ impl CallbackFn for Love {

if let Some(message) = message {
if let Err(why) = channel.say(&ctx.http, message).await {
log_message(&format!("Error sending message: {:?}", why), &STATUS_ERROR);
log_message(format!("Error sending message: {:?}", why).as_str(), MessageTypes::Error);
}
}
},
Expand Down
7 changes: 5 additions & 2 deletions src/interactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serenity::async_trait;
use serenity::client::Context;
use serenity::model::prelude::{ChannelId, UserId};

use crate::internal::debug::{log_message, STATUS_INFO};
use crate::internal::debug::{log_message, MessageTypes};

pub mod chat;
pub mod voice_channel;
Expand All @@ -11,7 +11,10 @@ pub fn interaction_callback(
name: &str,
callback: Box<dyn CallbackFn + Send + Sync>,
) -> Box<dyn CallbackFn + Send + Sync> {
log_message(&format!("Running integration {}", name), &STATUS_INFO);
log_message(
format!("Running integration {}", name).as_str(),
MessageTypes::Info,
);

callback
}
Expand Down
15 changes: 9 additions & 6 deletions src/interactions/voice_channel/join_channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::internal::debug::{log_message, STATUS_ERROR, STATUS_INFO};
use crate::internal::debug::{log_message, MessageTypes};
use crate::internal::users::USERS;
use rust_i18n::t;

Expand All @@ -18,10 +18,10 @@ thread_local! {
}

pub async fn clear_cache() {
log_message("Starting clear cache task", &STATUS_INFO);
log_message("Starting clear cache task", MessageTypes::Server);
loop {
time::sleep(time::Duration::from_secs(86400)).await;
log_message("Clearing cache", &STATUS_INFO);
log_message("Clearing cache", MessageTypes::Server);

CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
Expand Down Expand Up @@ -78,11 +78,11 @@ pub async fn join_channel(channel: &ChannelId, ctx: &Context, user_id: &UserId)
return format!("O CAPETA CHEGOU {} vezes 😡", counter).into();
}

return t!(&format!("interactions.join_channel.{}", (*counter as u8).max(2)), user_id => user.id).into();
return t!(&format!("interactions.join_channel.{}", (*counter as u8).min(2)), user_id => user.id).into();
}
} else {
cache.insert(*user_id, (1, now, *user_id));
log_message(&format!("Added {} to cache", user.name), &STATUS_INFO);
log_message(format!("Added {} to cache", user.name).as_str(), MessageTypes::Success);

if user_id == USERS.get("scaliza").unwrap() {
return t!(&format!("interactions.join_channel.scaliza.0"), user_id => user.id).into();
Expand All @@ -94,7 +94,10 @@ pub async fn join_channel(channel: &ChannelId, ctx: &Context, user_id: &UserId)

if let Some(message) = message {
if let Err(why) = channel.say(&ctx.http, message).await {
log_message(&format!("Error sending message: {:?}", why), &STATUS_ERROR);
log_message(
format!("Error sending message: {:?}", why).as_str(),
MessageTypes::Error,
);
}
}
}
Loading

0 comments on commit b78b029

Please sign in to comment.