Skip to content

Commit

Permalink
V2.10.1 started working on bot management command.
Browse files Browse the repository at this point in the history
  • Loading branch information
ValgulNecron committed Jul 21, 2024
1 parent 507d8f5 commit 150dcef
Show file tree
Hide file tree
Showing 32 changed files with 580 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kasuki"
version = "2.10.0"
version = "2.10.1"
edition = "2021"
authors = ["Valgul <[email protected]>"]
description = "A discord bot written in rust that get info from anilist API"
Expand Down
93 changes: 93 additions & 0 deletions json/guild_command/give_premium_sub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"guild_id": 1117152661620408531,
"name": "give_premium_sub",
"desc": "Give a user a premium subscription",
"nsfw": false,
"permissions": [
{
"permission": "Administrator"
}
],
"args": [
{
"name": "user",
"desc": "The user you want to give a premium subscription to.",
"required": true,
"autocomplete": false,
"arg_type": "User",
"localised_args": [
{
"code": "en-US",
"name": "user",
"desc": "The user you want to give a premium subscription to."
},
{
"code": "fr",
"name": "utilisateur",
"desc": "L'utilisateur dont vous voulez donner une souscription premium."
},
{
"code": "de",
"name": "Benutzer",
"desc": "Der Benutzer, dem Sie eine Premium-Abonnement geben können."
},
{
"code": "ja",
"name": "ユーザー",
"desc": "プレミアムサブスクリプションを与えるユーザー"
}
]
},
{
"name": "subscription",
"desc": "The subscription you want to give the user.",
"required": true,
"autocomplete": true,
"arg_type": "String",
"localised_args": [
{
"code": "en-US",
"name": "subscription",
"desc": "The subscription you want to give the user."
},
{
"code": "fr",
"name": "abonné",
"desc": "La souscription que vous souhaitez donner à l'utilisateur."
},
{
"code": "de",
"name": "Abonnement",
"desc": "Das Abonnement, das Sie dem Benutzer geben können."
},
{
"code": "ja",
"name": "サブスクリプション",
"desc": "ユーザーに与えるサブスクリプション"
}
]
}
],
"localised" : [
{
"code": "en-US",
"name": "give_premium_sub",
"desc": "Give a user a premium subscription"
},
{
"code": "fr",
"name": "give_premium_sub",
"desc": "Donner un utilisateur une souscription premium"
},
{
"code": "de",
"name": "give_premium_sub",
"desc": "Geben Sie einem Benutzer eine Premium-Abonnement"
},
{
"code": "ja",
"name": "give_premium_sub",
"desc": "ユーザーにプレミアムサブスクリプションを与える"
}
]
}
File renamed without changes.
14 changes: 14 additions & 0 deletions json/message/management/give_premium_sub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"en": {
"success": "Successfully gave user {user} the subscription {subscription} \n {res}"
},
"fr": {
"success": "Vous avez bien donne l'utilisateur {user} la souscription {subscription} \n {res}"
},
"de": {
"success": "Die Benutzer {user} hat die Abonnement {subscription} gegeben \n {res}"
},
"ja": {
"success": "{user} ユーザーに {subscription} によって与えられたサブスクリプション \n {res}"
}
}
18 changes: 18 additions & 0 deletions json/message/management/kill_switch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"en": {
"on": "The module has been activated for everyone.",
"off": "The module has been deactivated for everyone."
},
"fr": {
"on": "Le module a été activé pour tout le monde.",
"off": "Le module a été désactivé pour tout le monde."
},
"jp": {
"on": "モジュールが全員に有効化されました。",
"off": "モジュールが全員に無効化されました。"
},
"de": {
"on": "Das Modul wurde für alle aktiviert.",
"off": "Das Modul wurde für alle deaktiviert."
}
}
2 changes: 2 additions & 0 deletions src/command/autocomplete/autocomplete_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::command::autocomplete::anilist_user::{
anime, character, compare, ln, manga, search, staff, studio, user,
};
use crate::command::autocomplete::game::steam_game_info;
use crate::command::autocomplete::management::give_premium_sub::give_premium_sub_autocomplete;
use crate::command::autocomplete::vn;
use crate::command::autocomplete::vn::{game, producer};
use crate::helper::get_option::subcommand_group::get_subcommand;
Expand All @@ -27,6 +28,7 @@ pub async fn autocomplete_dispatching(
"anilist_user" => anilist_autocomplete(ctx, autocomplete_interaction, anilist_cache).await,
"steam" => steam_autocomplete(ctx, autocomplete_interaction, apps).await,
"vn" => vn_autocomplete(ctx, autocomplete_interaction, vndb_cache).await,
"give_premium_sub" => give_premium_sub_autocomplete(ctx, autocomplete_interaction).await,
_ => {}
}
}
Expand Down
77 changes: 77 additions & 0 deletions src/command/autocomplete/management/give_premium_sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::constant::DEFAULT_STRING;
use crate::helper::get_option::command::{get_option_map_string, get_option_map_user};
use crate::helper::get_option::subcommand::get_option_map_string_autocomplete_subcommand;
use serenity::all::{
AutocompleteChoice, CommandInteraction, Context, CreateAutocompleteResponse,
CreateInteractionResponse, SkuFlags, SkuKind,
};
use tracing::debug;

pub async fn give_premium_sub_autocomplete(
ctx: Context,
autocomplete_interaction: CommandInteraction,
) {
let map = get_option_map_string(&autocomplete_interaction);
let subscription = map
.get(&String::from("subscription"))
.unwrap_or(DEFAULT_STRING);
let map = get_option_map_user(&autocomplete_interaction);
let user = match map.get(&String::from("user")) {
Some(user) => user,
None => return,
};

let sku_list = ctx.http.get_skus().await.unwrap();
let sku_list = sku_list
.iter()
.map(|sku| {
(
{
let kind = match sku.kind {
SkuKind::Subscription => String::from("Subscription"),
SkuKind::SubscriptionGroup => String::from("Subscription Group"),
SkuKind::Unknown(2) => String::from("DURABLE"),
SkuKind::Unknown(3) => String::from("CONSUMABLE"),
_ => String::from("Unknown"),
};
let available = 1 << 2;
let guild_subscription = 1 << 7;
let user_subscription = 1 << 8;
let flags_bits = sku.flags.bits();
let mut flags = String::new();
let mut flags2 = String::new();
if (available & flags_bits) != 0 {
flags2.push_str("is available ");
}

if (guild_subscription & flags_bits) != 0 {
flags.push_str("guild subscription ");
}

if (user_subscription & flags_bits) != 0 {
flags.push_str("user subscription ");
}
format!("{} {} for {} {}", sku.name, kind, flags, flags2)
.chars()
.take(100)
.collect::<String>()
},
sku.id.to_string(),
)
})
.filter(|sku| !sku.0.contains("Subscription Group"))
.map(|sku| AutocompleteChoice::new(sku.0, sku.1))
.take(25)
.collect::<Vec<AutocompleteChoice>>();
if sku_list.is_empty() {
return;
}
let data = CreateAutocompleteResponse::new().set_choices(sku_list);
let builder = CreateInteractionResponse::Autocomplete(data);
if let Err(why) = autocomplete_interaction
.create_response(ctx.http, builder)
.await
{
debug!("Error sending response: {:?}", why);
}
}
1 change: 1 addition & 0 deletions src/command/autocomplete/management/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod give_premium_sub;
1 change: 1 addition & 0 deletions src/command/autocomplete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod anilist_server;
pub mod anilist_user;
pub mod autocomplete_dispatch;
pub mod game;
pub mod management;
pub mod vn;
20 changes: 20 additions & 0 deletions src/command/run/command_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::command::run::anilist_user::{
use crate::command::run::anime::random_image;
use crate::command::run::anime_nsfw::random_nsfw_image;
use crate::command::run::bot::{credit, info, ping};
use crate::command::run::management::{give_premium_sub, kill_switch};
use crate::command::run::server::{
generate_image_pfp_server, generate_image_pfp_server_global, guild,
};
Expand Down Expand Up @@ -172,6 +173,24 @@ pub async fn command_dispatching(
)
.await?
}
// Management
"kill_switch" => {
kill_switch::run(
ctx,
command_interaction,
self_handler.bot_data.config.clone(),
)
.await?
}
"give_premium_sub" => {
give_premium_sub::run(
ctx,
command_interaction,
self_handler.bot_data.config.clone(),
)
.await?
}

// If the command name does not match any of the specified commands, return an error
_ => {
return Err(Box::new(ResponseError::Option(String::from(
Expand Down Expand Up @@ -225,6 +244,7 @@ pub async fn check_if_module_is_on(
async fn check_kill_switch_status(module: &str, db_type: String) -> Result<bool, Box<dyn Error>> {
let row: ActivationStatusModule =
get_data_module_activation_kill_switch_status(db_type).await?;
trace!(?row);
Ok(check_activation_status(module, row).await)
}

Expand Down
64 changes: 64 additions & 0 deletions src/command/run/management/give_premium_sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::config::Config;
use crate::helper::create_default_embed::get_default_embed;
use crate::helper::error_management::error_enum::ResponseError;
use crate::helper::get_option::command::{get_option_map_string, get_option_map_user};
use serenity::all::{
CommandInteraction, Context, CreateInteractionResponse, CreateInteractionResponseMessage,
EntitlementOwner,
};
use std::error::Error;
use std::sync::Arc;

pub async fn run(
ctx: &Context,
command_interaction: &CommandInteraction,
config: Arc<Config>,
) -> Result<(), Box<dyn Error>> {
let map = get_option_map_user(command_interaction);
let user = map
.get(&String::from("user"))
.ok_or(ResponseError::Option(String::from("No option for user")))?
.clone();
let map = get_option_map_string(command_interaction);
let subscription = map
.get(&String::from("subscription"))
.ok_or(ResponseError::Option(String::from(
"No option for subscription",
)))?
.clone();

let skus = ctx
.http
.get_skus()
.await
.map_err(|e| ResponseError::WebRequest(format!("{:#?}", e)))?;
let skus_id: Vec<String> = skus.iter().map(|sku| sku.id.to_string()).collect();
if !skus_id.contains(&subscription) {
Err(ResponseError::Option(String::from("Invalid sub id")))?
}
let mut sku_id = Default::default();
for sku in skus {
if sku.id.to_string() == subscription {
sku_id = sku.id;
}
}

let res = ctx
.http
.create_test_entitlement(sku_id, EntitlementOwner::User(user))
.await
.map_err(|e| ResponseError::WebRequest(format!("{:#?}", e)))?;

let embed = get_default_embed(None).description(format!(
"Successfully gave user {} the subscription {} \n {:#?}",
user, subscription, res
));
let builder_message = CreateInteractionResponseMessage::new().embed(embed);

let builder = CreateInteractionResponse::Message(builder_message);
command_interaction
.create_response(&ctx.http, builder)
.await
.map_err(|e| ResponseError::Sending(format!("{:#?}", e)))?;
Ok(())
}
Loading

0 comments on commit 150dcef

Please sign in to comment.