Skip to content

Commit bba94ae

Browse files
committed
Bug fixes for leagues broadcasts
1 parent 9135909 commit bba94ae

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

trackscape-discord-api/src/controllers/chat_controller.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{handler, ChatServerHandle};
33
use actix_web::web::Data;
44
use actix_web::{error, post, web, Error, HttpRequest, HttpResponse, Scope};
55
use celery::Celery;
6-
use log::error;
6+
use log::{error, info};
77
use serenity::all::{ChannelId, CreateEmbed, CreateEmbedAuthor};
88
use serenity::builder::CreateMessage;
99
use serenity::http::Http;
@@ -26,6 +26,8 @@ struct MyError {
2626
message: &'static str,
2727
}
2828

29+
const LEAGUES_ICON_TAG: &str = "<img=22> ";
30+
2931
#[post("/new-discord-message")]
3032
async fn new_discord_message(
3133
req: HttpRequest,
@@ -88,7 +90,7 @@ async fn new_clan_chats(
8890
let result = Err(MyError {
8991
message: "No verification code was set",
9092
});
91-
return result.map_err(|err| error::ErrorBadRequest(err.message));
93+
return result.map_err(|err: MyError| error::ErrorBadRequest(err.message));
9294
}
9395

9496
let verification_code = possible_verification_code.unwrap().to_str().unwrap();
@@ -113,7 +115,7 @@ async fn new_clan_chats(
113115
return result.map_err(|err| error::ErrorBadRequest(err.message));
114116
};
115117

116-
for chat in new_chat.clone() {
118+
for mut chat in new_chat.clone() {
117119
if chat.sender.clone() == "" && chat.clan_name.clone() == "" {
118120
continue;
119121
}
@@ -141,6 +143,12 @@ async fn new_clan_chats(
141143
}
142144
}
143145

146+
//HACK leagues. is_league_world is not getting set as expected so we need to check the icon_id
147+
if let Some(icon_id) = chat.icon_id {
148+
//League icon id
149+
chat.is_league_world = Some(icon_id == 22);
150+
}
151+
144152
//Sets the clan name to the guild. Bit of a hack but best way to get clan name
145153
if let None = registered_guild.clan_name {
146154
registered_guild.clan_name = Some(chat.clan_name.clone());
@@ -168,7 +176,8 @@ async fn new_clan_chats(
168176
CreateEmbed::new()
169177
.title("")
170178
.author(CreateEmbedAuthor::new(chat.sender.clone()).icon_url(author_image))
171-
.description(chat.message.clone())
179+
//HACK
180+
.description(chat.message.clone().replace(LEAGUES_ICON_TAG, ""))
172181
.color(0x0000FF)
173182
.timestamp(right_now),
174183
);
@@ -218,7 +227,13 @@ async fn new_clan_chats(
218227
continue;
219228
}
220229

221-
let league_world = chat.is_league_world.unwrap_or(false);
230+
// let league_world = chat.is_league_world.unwrap_or(false);
231+
//HACK leagues. is_league_world is not getting set as expected so we need to check the broadcast for the icon tag
232+
let league_world = chat.message.starts_with(LEAGUES_ICON_TAG);
233+
if league_world {
234+
chat.message = chat.message.replace(LEAGUES_ICON_TAG, "");
235+
chat.is_league_world = Some(true);
236+
}
222237

223238
let item_mapping_from_redis = get_item_mapping(&mut redis_connection).await;
224239

trackscape-discord-bot/src/commands/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ pub mod set_leagues_broadcast_channel;
1111
pub mod set_quest_min_command;
1212
pub mod set_threshold_command;
1313
pub mod set_wom_id_command;
14+
pub mod stop_leagues_notifications;
1415
pub mod toggle_broadcasts_command;
1516
pub mod trackscape_command_trait;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::on_boarding_message::send_on_boarding;
2+
use serenity::all::{CommandDataOption, CreateCommand};
3+
use serenity::client::Context;
4+
use serenity::model::id::ChannelId;
5+
use trackscape_discord_shared::database::BotMongoDb;
6+
7+
pub fn register() -> CreateCommand {
8+
CreateCommand::new("stop_leagues_notifications")
9+
.description("Stops leagues notifications, can reenable by setting the channel again.")
10+
}
11+
12+
pub async fn run(
13+
_options: &[CommandDataOption],
14+
ctx: &Context,
15+
db: &BotMongoDb,
16+
guild_id: u64,
17+
) -> Option<String> {
18+
let saved_guild_query = db.guilds.get_by_guild_id(guild_id).await;
19+
20+
return match saved_guild_query {
21+
Ok(possible_guild) => match possible_guild {
22+
Some(mut saved_guild) => {
23+
saved_guild.leagues_broadcast_channel = None;
24+
db.guilds.update_guild(saved_guild).await;
25+
Some("You have stopped receiving leagues notifications. You can reenable by setting the channel again.".to_string())
26+
}
27+
None => {
28+
Some("Error finding your server as registered. Try kicking and re adding the bot please.".to_string())
29+
}
30+
},
31+
Err(_) => {
32+
Some("There was a technical error. Please try again later.".to_string())
33+
}
34+
};
35+
None
36+
}

trackscape-discord-bot/src/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ impl EventHandler for Bot {
298298
)
299299
.await
300300
}
301+
"stop_leagues_notifications" => {
302+
commands::stop_leagues_notifications::run(
303+
&command.data.options,
304+
&ctx,
305+
&self.mongo_db,
306+
command.guild_id.unwrap().get(),
307+
)
308+
.await
309+
}
301310
_ => {
302311
info!("not implemented :(");
303312
None
@@ -339,6 +348,7 @@ fn get_commands() -> Vec<CreateCommand> {
339348
commands.push(commands::name_change_command::register());
340349
commands.push(commands::manually_run_wom_sync_command::register());
341350
commands.push(commands::set_leagues_broadcast_channel::register());
351+
commands.push(commands::stop_leagues_notifications::register());
342352
commands
343353
}
344354
pub async fn create_commands_for_guild(guild_id: &GuildId, ctx: Context) {

0 commit comments

Comments
 (0)