Skip to content

Commit

Permalink
add analytics register commands
Browse files Browse the repository at this point in the history
  • Loading branch information
AsianIntel committed Nov 15, 2024
1 parent 0f9a688 commit 7edb3e6
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
4 changes: 4 additions & 0 deletions rowifi/src/commands/analytics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod register;

use image::{codecs::png::PngEncoder, ExtendedColorType, ImageEncoder};
use plotters::{
chart::{ChartBuilder, LabelAreaPosition},
Expand All @@ -20,6 +22,8 @@ use rowifi_models::{
};
use std::{io::Cursor, time::Duration};

pub use register::{analytics_register, analytics_unregister};

#[derive(Debug)]
pub struct ViewDuration(pub Duration);

Expand Down
128 changes: 128 additions & 0 deletions rowifi/src/commands/analytics/register.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use rowifi_framework::prelude::*;
use rowifi_models::{
discord::{
http::interaction::{InteractionResponse, InteractionResponseType},
util::Timestamp,
},
guild::GuildType,
roblox::id::GroupId,
};

#[derive(Arguments, Debug)]
pub struct RegisterArguments {
pub group_id: GroupId,
}

pub async fn analytics_register(
bot: Extension<BotContext>,
command: Command<RegisterArguments>,
) -> impl IntoResponse {
tokio::spawn(async move {
if let Err(err) = analytics_register_func(&bot, &command.ctx, command.args).await {
handle_error(bot.0, command.ctx, err).await;
}
});

Json(InteractionResponse {
kind: InteractionResponseType::DeferredChannelMessageWithSource,
data: None,
})
}

pub async fn analytics_register_func(
bot: &BotContext,
ctx: &CommandContext,
args: RegisterArguments,
) -> CommandResult {
let mut guild = bot
.get_guild(
"SELECT guild_id, kind, registered_groups FROM guilds WHERE guild_id = $1",
ctx.guild_id,
)
.await?;

if guild.kind.unwrap_or_default() != GuildType::Gamma {
let message = "Analytics is only available for Gamma Tier servers. You can upgrade the server on the dashboard.";
ctx.respond(&bot).content(message).unwrap().await?;
return Ok(());
}

if !guild.registered_groups.contains(&args.group_id) {
guild.registered_groups.push(args.group_id);
bot.database
.execute(
"UPDATE guilds SET registered_groups = $2 WHERE guild_id = $1",
&[&ctx.guild_id, &guild.registered_groups],
)
.await?;
}

let embed = EmbedBuilder::new()
.color(DARK_GREEN)
.footer(EmbedFooterBuilder::new("RoWifi").build())
.timestamp(Timestamp::from_secs(Utc::now().timestamp()).unwrap())
.title("Group Added For Analytics")
.build();
ctx.respond(&bot).embeds(&[embed]).unwrap().await?;

Ok(())
}

pub async fn analytics_unregister(
bot: Extension<BotContext>,
command: Command<RegisterArguments>,
) -> impl IntoResponse {
tokio::spawn(async move {
if let Err(err) = analytics_unregister_func(&bot, &command.ctx, command.args).await {
handle_error(bot.0, command.ctx, err).await;
}
});

Json(InteractionResponse {
kind: InteractionResponseType::DeferredChannelMessageWithSource,
data: None,
})
}

pub async fn analytics_unregister_func(
bot: &BotContext,
ctx: &CommandContext,
args: RegisterArguments,
) -> CommandResult {
let mut guild = bot
.get_guild(
"SELECT guild_id, kind, registered_groups FROM guilds WHERE guild_id = $1",
ctx.guild_id,
)
.await?;

if guild.kind.unwrap_or_default() != GuildType::Gamma {
let message = "Analytics is only available for Gamma Tier servers. You can upgrade the server on the dashboard.";
ctx.respond(&bot).content(message).unwrap().await?;
return Ok(());
}

let position = guild
.registered_groups
.iter()
.position(|g| *g == args.group_id);
if let Some(position) = position {
guild.registered_groups.remove(position);
bot.database
.execute(
"UPDATE guilds SET registered_groups = $2 WHERE guild_id = $1",
&[&ctx.guild_id, &guild.registered_groups],
)
.await?;
}

let embed = EmbedBuilder::new()
.color(DARK_GREEN)
.footer(EmbedFooterBuilder::new("RoWifi").build())
.timestamp(Timestamp::from_secs(Utc::now().timestamp()).unwrap())
.title("Group Added For Analytics")
.build();
ctx.respond(&bot).embeds(&[embed]).unwrap().await?;

Ok(())
}
4 changes: 3 additions & 1 deletion rowifi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use twilight_http::Client as TwilightClient;
use twilight_standby::Standby;

use crate::commands::{
analytics::analytics_view,
analytics::{analytics_register, analytics_unregister, analytics_view},
assetbinds::{delete_assetbind, new_assetbind, view_assetbinds},
backups::{backup_delete, backup_new, backup_restore, backup_view},
custombinds::{delete_custombind, new_custombind, view_custombinds},
Expand Down Expand Up @@ -139,6 +139,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
.route("/update-all", post(update_all))
.route("/update-role", post(update_role))
.route("/analytics/view", post(analytics_view))
.route("/analytics/register", post(analytics_register))
.route("/analytics/unregister", post(analytics_unregister))
.route("/serverinfo", post(serverinfo))
.route("/debug/update", post(debug_update))
.route("/standby", post(standby_route));
Expand Down

0 comments on commit 7edb3e6

Please sign in to comment.