Skip to content

Commit

Permalink
v1.2.5 - Add services functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitgor committed Aug 8, 2024
1 parent 27c6bda commit 5f09088
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ Table users {
user_xbox_uid varchar(256) [null]
user_timezone varchar(256) [null, default: `Europe/Tallinn`]
user_invite_id integer
services_vpn_us boolean [default: false]
services_vpn_ee boolean [default: false]
services_vpn_us varchar(64) [default: `0`]
services_vpn_ee varchar(64) [default: `0`]
user_date_created timestamp [default: `CURRENT_TIMESTAMP`]
user_date_updated timestamp [null]
user_date_deleted timestamp [null]
Expand Down
4 changes: 2 additions & 2 deletions ReinekeDb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CREATE TABLE `users` (
`user_xbox_uid` varchar(256),
`user_timezone` varchar(256) DEFAULT (Europe/Tallinn),
`user_invite_id` integer,
`services_vpn_us` boolean DEFAULT false,
`services_vpn_ee` boolean DEFAULT false,
`services_vpn_us` varchar(64) NOT NULL DEFAULT ('0'),
`services_vpn_ee` varchar(64) NOT NULL DEFAULT ('0'),
`user_date_created` timestamp DEFAULT (CURRENT_TIMESTAMP),
`user_date_updated` timestamp,
`user_date_deleted` timestamp
Expand Down
128 changes: 127 additions & 1 deletion commands/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ module.exports = {
.setDescription('Текст подсказки')
.setRequired(true)
.addChoices(...hints.queries)),
)
.addSubcommand(subcommand =>
subcommand
.setName('service')
.setDescription('Дообавить сервис участнику')
.addUserOption(option =>
option.setName('user')
.setDescription('Пользователь')
.setRequired(true))
.addStringOption(option =>
option.setName('service_code')
.setDescription('Наименование сервиса')
.setRequired(true)
.addChoices(...lists.services)),
),

async execute(interaction) {
Expand Down Expand Up @@ -237,13 +251,125 @@ module.exports = {
}
});
} else if (interaction.options.getSubcommand() === 'hint') {

const message = interaction.options.getString('message_id');
const hint = interaction.options.getString('hint_code');
const hintContent = hints.predefines[hint];

await interaction.channel.send({ content: hintContent, reply: { messageReference: message }, ephemeral: true });
interaction.reply({ content: "— Подсказка отправлена!", ephemeral: true });
} else if (interaction.options.getSubcommand() === 'service') {
const target_user = interaction.options.getUser('user');
const service_code = interaction.options.getString('service_code');

/* Step 1.
* Check if selected user is exists in the database
*/
let sql9 = "SELECT * FROM users WHERE user_discord_uid = ? LIMIT 1;";
database.query(sql9, [target_user.id], (error, user_data, fields) => {
if (user_data.length != 1 || error) {
interaction.reply({ content: "— Профиль этого пользователя отсутствует в БД или с ним возникли проблемы.", ephemeral: true });
} else {
/*
* Step 2.
* Check if selected service is already added to user profile
*/
var service_db_title = '';

switch (service_code) {
// Service: VPN US
case '101':
service_db_title = 'services_vpn_us';
break;
case '102':
service_db_title = 'services_vpn_ee';
break;
}

let sql7 = `SELECT * FROM users WHERE user_discord_uid = ? AND ${service_db_title} <> '0' LIMIT 1;`;
database.query(sql7, [target_user.id], (error, service_data, fields) => {
if (service_data.length != 1) {

/*
* Step 3.
* Show modal window to add the service to user profile
*
* VPN related services
*/
if (['101','102'].includes(service_code) ) {
var service_title = "";

switch (service_code) {
case '101':
service_title = "US.vpn.snfx.ee";
break;

case '102':
service_title = "EE.vpn.snfx.ee";
break;

default:
break;
}

/* Modal form for adding VPN service */
const modal_vpn_common = {
"title": `Добавить услугу: ${service_title}`,
"custom_id": "service_vpn_add",
"components": [
{
"type": 1,
"components": [{
"type": 4,
"custom_id": "service_uid",
"label": "ID пользователя:",
"style": 1,
"min_length": 1,
"max_length": 128,
"value": target_user.id,
"required": true
}]
},
{
"type": 1,
"components": [{
"type": 4,
"custom_id": "service_id",
"label": "ID услуги:",
"style": 1,
"min_length": 1,
"max_length": 64,
"value": service_code,
"required": true
}]
},
{
"type": 1,
"components": [{
"type": 4,
"custom_id": "service_password",
"label": "Пароль:",
"placeholder": "Пароль для доступа к сервису VPN",
"style": 1,
"min_length": 1,
"max_length": 64,
"required": true
}]
}
]
};

interaction.showModal(modal_vpn_common);
} else {
interaction.reply({ content: "— Не могу добавить эту услугу!", ephemeral: true });
}
} else {
interaction.reply({ content: "— Данная услуга уже добавлена для выбранного пользователя.", ephemeral: true });
}
});


}
});
}
}
};
Expand Down
70 changes: 70 additions & 0 deletions modals/service_vpn_add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { ButtonBuilder, ButtonStyle, ActionRowBuilder } = require('discord.js');
const config = require('../config.json');
const fs = require('node:fs');
const lists = require('../config.lists.json');
const hints = require('../config.hints.json');
const mysql = require('mysql');
const database = mysql.createConnection({
host: config.db_config.host,
user: config.db_config.dbuser,
password: config.db_config.dbpass,
database: config.db_config.dbname,
debug: false,
multipleStatements: true,
});

module.exports = {
data: {
name: 'service_vpn_add'
},
async execute(interaction) {
const discord_user_uid = interaction.fields.getTextInputValue('service_uid');
const service_code = interaction.fields.getTextInputValue('service_id');
const service_password = interaction.fields.getTextInputValue('service_password');

const NotificationsChannel = interaction.client.channels.cache.get(config.log_channels.notifictions);
const BotLogChannel = interaction.client.channels.cache.get(config.log_channels.log);

var ProfileUri = config.url.commonUrl + "profile/";

var ProfileLinkBtn = new ButtonBuilder()
.setLabel('Посмотреть профиль')
.setURL(ProfileUri)
.setStyle(ButtonStyle.Link);

var ButtonsRow1 = new ActionRowBuilder()
.addComponents(ProfileLinkBtn);

switch (service_code) {
// Service: VPN US
case '101':
var sql1 = `UPDATE users SET services_vpn_us = ? WHERE user_discord_uid = ?;`;
database.query(sql1, [service_password, discord_user_uid], (error1, pingback) => {
if (error1){
BotLogChannel.send({ content: `[ADMIN] SERVICE: Can't add a service US.vpn.snfx.ee (ID: 101) to user <@` + discord_user_uid + `>\nCreated by <@` + interaction.user.id + `>` });
} else {
NotificationsChannel.send({content:`— <@` + discord_user_uid + `>, добавил для Тебя новый сервис! Подробная информация — на странице Твоего профиля.`, components: [ButtonsRow1]});
BotLogChannel.send({ content: `[ADMIN] SERVICE: Added to user <@` + discord_user_uid + `> - US.vpn.snfx.ee (ID: 101)\nCreated by <@` + interaction.user.id + `>` });
}
});
break;
// Service: VPN EE
case '102':
var sql2 = `UPDATE users SET services_vpn_ee = ? WHERE user_discord_uid = ?;`;
database.query(sql2, [service_password, discord_user_uid], (error2, pingback) => {
if (error2){
BotLogChannel.send({ content: `[ADMIN] SERVICE: Can't add a service EE.vpn.snfx.ee (ID: 102) to user <@` + discord_user_uid + `>\nCreated by <@` + interaction.user.id + `>` });
} else {
NotificationsChannel.send({content:`— <@` + discord_user_uid + `>, добавил для Тебя новый сервис! Подробная информация — на странице Твоего профиля.`, components: [ButtonsRow1]});
BotLogChannel.send({ content: `[ADMIN] SERVICE: Added to user <@` + discord_user_uid + `> - EE.vpn.snfx.ee (ID: 102)\nCreated by <@` + interaction.user.id + `>` });
}
});

break;

default:
break;
}
interaction.reply({ content: '— Добавил услугу указанному пользователю!', ephemeral: true });
}
};

0 comments on commit 5f09088

Please sign in to comment.