Skip to content

Commit

Permalink
feat: add defer replying
Browse files Browse the repository at this point in the history
  • Loading branch information
kszinhu committed Aug 3, 2023
1 parent 6f10e6b commit e352a97
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
14 changes: 2 additions & 12 deletions src/commands/radio/consumer.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
use crate::internal::debug::{log_message, MessageTypes};

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).as_str(),
MessageTypes::Error,
);

Err(t!("commands.radio.connection_error"))
}
Err(why) => Err(why.to_string()),
}
}
25 changes: 12 additions & 13 deletions src/commands/radio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -102,13 +104,10 @@ pub async fn run(
);
}

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

if debug {
log_message(
format!("Join result: {}", join_result).as_str(),
MessageTypes::Debug,
);
log_message("Joined voice channel successfully", MessageTypes::Debug);
}

if let Some(handler_lock) = manager.get(guild.id) {
Expand All @@ -122,7 +121,7 @@ pub async fn run(
MessageTypes::Error,
);

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

Expand Down
17 changes: 13 additions & 4 deletions src/events/voice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 {
Expand All @@ -21,19 +22,27 @@ pub async fn join(ctx: &Context, guild: &Guild, user_id: &UserId) -> CommandResu
}
};

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) => {
Expand Down
15 changes: 8 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use serenity::async_trait;
use serenity::client::bridge::gateway::ShardManager;
use serenity::framework::StandardFramework;
use serenity::model::application::interaction::{Interaction, InteractionResponseType};
use serenity::model::application::interaction::Interaction;
use serenity::model::gateway::Ready;
use serenity::model::id::GuildId;
use serenity::model::prelude::command::Command;
Expand Down Expand Up @@ -125,7 +125,7 @@ impl EventHandler for Handler {
);
}

command.defer(ctx.http.clone()).await.unwrap();
let _ = command.defer(&ctx.http.clone()).await;

let content = match command.data.name.as_str() {
"ping" => commands::ping::run(&command.data.options).await,
Expand Down Expand Up @@ -185,12 +185,13 @@ impl EventHandler for Handler {
_ => "Not implemented".to_string(),
};

log_message(
format!("Responding with: {}", content).as_str(),
MessageTypes::Debug,
);

if let Err(why) = command
.create_interaction_response(&ctx.http, |response| {
response
.kind(InteractionResponseType::ChannelMessageWithSource)
.interaction_response_data(|message| message.content(content))
})
.edit_original_interaction_response(ctx.http, |response| response.content(content))
.await
{
log_message(
Expand Down

0 comments on commit e352a97

Please sign in to comment.