Skip to content

Commit

Permalink
Add list_premium and premium_deactivate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Dec 2, 2023
1 parent 6f40bcf commit 59eb291
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 149 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ arrayvec = "0.7.4"
bitflags = "2.4.1"
paste = "1.0.14"
typesize = "0.1.2"
futures-util = { version = "0.3.29", default-features = false }

[dependencies.symphonia]
features = ["mp3", "ogg", "wav", "pcm"]
Expand Down
105 changes: 4 additions & 101 deletions src/commands/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{borrow::Cow, num::NonZeroU16};

use songbird::error::JoinError;
use sqlx::Row;

use poise::{
serenity_prelude::{self as serenity, builder::*},
CreateReply,
};
use poise::serenity_prelude::{self as serenity, builder::*};

use crate::{
funcs::random_footer,
require, require_guild,
structs::{Command, CommandResult, Context, JoinVCToken, Result, TTSMode},
structs::{Command, CommandResult, Context, JoinVCToken, Result},
traits::{PoiseContextExt, SongbirdManagerExt},
};

Expand Down Expand Up @@ -252,97 +246,6 @@ pub async fn clear(ctx: Context<'_>) -> CommandResult {
Ok(())
}

/// Activates a server for TTS Bot Premium!
#[poise::command(
category = "Main Commands",
guild_only,
prefix_command,
slash_command,
aliases("activate"),
required_bot_permissions = "SEND_MESSAGES | EMBED_LINKS"
)]
pub async fn premium_activate(ctx: Context<'_>) -> CommandResult {
let guild_id = ctx.guild_id().unwrap();
let data = ctx.data();

if data.premium_check(Some(guild_id)).await?.is_none() {
ctx.say(ctx.gettext("Hey, this server is already premium!"))
.await?;
return Ok(());
}

let author = ctx.author();
let author_id = ctx.author().id.get() as i64;

let linked_guilds: i64 = sqlx::query("SELECT count(*) FROM guilds WHERE premium_user = $1")
.bind(author_id)
.fetch_one(&data.pool)
.await?
.get("count");

let error_msg = match data.fetch_patreon_info(author.id).await? {
Some(tier) => {
if linked_guilds as u8 >= tier.entitled_servers {
Some(Cow::Owned(ctx
.gettext("Hey, you already have {server_count} servers linked, you are only subscribed to the {entitled_servers} tier!")
.replace("{entitled_servers}", &tier.entitled_servers.to_string())
.replace("{server_count}", &linked_guilds.to_string())
))
} else {
None
}
}
None => Some(Cow::Borrowed(
ctx.gettext("Hey, I don't think you are subscribed on Patreon!"),
)),
};

if let Some(error_msg) = error_msg {
ctx.send(CreateReply::default().embed(CreateEmbed::default()
.title("TTS Bot Premium")
.description(error_msg)
.thumbnail(&data.premium_avatar_url)
.colour(crate::constants::PREMIUM_NEUTRAL_COLOUR)
.footer(CreateEmbedFooter::new({
let line1 = ctx.gettext("If you have just subscribed, please wait for up to an hour for the member list to update!\n");
let line2 = ctx.gettext("If this is incorrect, and you have waited an hour, please contact GnomedDev.");

let mut concat = String::with_capacity(line1.len() + line2.len());
concat.push_str(line1);
concat.push_str(line2);
concat
}))
)).await?;

return Ok(());
}

data.userinfo_db.create_row(author_id).await?;
data.guilds_db
.set_one(guild_id.into(), "premium_user", &author_id)
.await?;
data.guilds_db
.set_one(guild_id.into(), "voice_mode", &TTSMode::gCloud)
.await?;

ctx.say(ctx.gettext("Done! This server is now premium!"))
.await?;

let guild = ctx.cache().guild(guild_id);
let guild_name = guild.as_ref().map_or("<Unknown>", |g| g.name.as_str());

tracing::info!(
"{}#{} | {} linked premium to {} | {}, they had {} linked servers",
author.name,
author.discriminator.map_or(0, NonZeroU16::get),
author.id,
guild_name,
guild_id,
linked_guilds
);
Ok(())
}

pub fn commands() -> [Command; 4] {
[join(), leave(), clear(), premium_activate()]
pub fn commands() -> [Command; 3] {
[join(), leave(), clear()]
}
2 changes: 2 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ mod help;
mod main;
mod other;
mod owner;
mod premium;
mod settings;

pub fn commands() -> Vec<Command> {
main::commands()
.into_iter()
.chain(other::commands())
.chain(settings::commands())
.chain(premium::commands())
.chain(owner::commands())
.chain(help::commands())
.collect()
Expand Down
31 changes: 2 additions & 29 deletions src/commands/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
num::NonZeroU16,
sync::atomic::Ordering::SeqCst,
};

Expand Down Expand Up @@ -81,36 +80,11 @@ pub async fn close(ctx: Context<'_>) -> CommandResult {
Ok(())
}

#[poise::command(prefix_command, owners_only, hide_in_help)]
pub async fn add_premium(
ctx: Context<'_>,
guild: serenity::Guild,
user: serenity::User,
) -> CommandResult {
let data = ctx.data();
let user_id = user.id.into();

data.userinfo_db.create_row(user_id).await?;
data.guilds_db
.set_one(guild.id.into(), "premium_user", &user_id)
.await?;

ctx.say(format!(
"Linked <@{}> ({}#{} | {}) to {}",
user.id,
user.name,
user.discriminator.map_or(0, NonZeroU16::get),
user.id,
guild.name
))
.await?;
Ok(())
}

#[poise::command(
prefix_command,
owners_only,
hide_in_help,
aliases("invalidate_cache", "delete_cache"),
subcommands("guild", "user", "guild_voice", "user_voice")
)]
pub async fn remove_cache(ctx: Context<'_>) -> CommandResult {
Expand Down Expand Up @@ -429,13 +403,12 @@ pub async fn cache_info(ctx: Context<'_>, kind: Option<String>) -> CommandResult
Ok(())
}

pub fn commands() -> [Command; 9] {
pub fn commands() -> [Command; 8] {
[
dm(),
close(),
debug(),
register(),
add_premium(),
remove_cache(),
refresh_ofs(),
purge_guilds(),
Expand Down
Loading

0 comments on commit 59eb291

Please sign in to comment.