Skip to content
Snippets Groups Projects
Unverified Commit 98ccf001 authored by Maxime FRIESS's avatar Maxime FRIESS :blue_heart:
Browse files

[Commands] Added /account

parent eb25aff5
Branches
Tags
No related merge requests found
......@@ -111,6 +111,14 @@ class SebAPI {
return await this.__request("GET", `/api/people?filter[discord_id]=${discord_id}`);
}
async personal_account_bypersonid(person_id) {
return await this.__request("GET", `/api/personal_accounts?filter[person_id]=${person_id}`);
}
async personal_transactions_bypersonalaccountid(personal_account_id, page=1) {
return await this.__request("GET", `/api/personal_transactions?filter[personal_account_id]=${personal_account_id}&per_page=10&page=${page}&order_by=created_at&order_sort=DESC`);
}
async members() {
return await this.__request("GET", "/api/members");
}
......
/**
* Copyright © 2021 Maxime Friess <M4x1me@pm.me>
*
* This file is part of Seb-BOT.
*
* Seb-BOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Seb-BOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Seb-BOT. If not, see <https://www.gnu.org/licenses/>.
*/
import { ApplicationCommandOptionType } from 'discord-api-types/v9';
import { MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';
import SebAPI from '../api/SebApi.js';
import Bot from '../Bot.js';
import Command from '../Command.js';
import SoftConfig from '../config/SoftConfig.js';
class AccountCommand extends Command {
constructor() {
super();
this.sub = {
"balance": this.exec_balance.bind(this),
"history": this.exec_history.bind(this)
}
Bot.registerButton("acc_history_next", this.buttonNext.bind(this));
Bot.registerButton("acc_history_previous", this.buttonPrevious.bind(this));
}
getName() {
return "account";
}
getOptions() {
return [{
type: ApplicationCommandOptionType.Subcommand,
name: "balance",
description: "Récupère le montant sur le compte personnel"
}, {
type: ApplicationCommandOptionType.Subcommand,
name: "history",
description: "Récupère l'historique des transactions du compte personnel"
}];
}
getDescription() {
return "Gestion du compte personnel";
}
async __get_account(interaction) {
let d = await SebAPI.person_bydiscordid(interaction.user.id);
if (!d.good) {
await interaction.editReply({ content: "Une erreur s'est produite." });
return;
}
if (d?.data?.data?.length <= 0) {
await interaction.editReply({ content: "Vous n'êtes pas enregistré dans Seb." });
return;
}
d = d.data.data[0];
let a_d = await SebAPI.personal_account_bypersonid(d.id);
if (!a_d.good) {
await interaction.editReply({ content: "Une erreur s'est produite." });
return;
}
if (a_d?.data?.data?.length <= 0) {
await interaction.editReply({ content: "Vous n'avez pas de compte personnel." });
return;
}
a_d = a_d.data.data[0];
return { d, a_d };
}
__getCurrentPage(interaction) {
const res = /^Page ([0-9]+)\/[0-9]+$/gm.exec(interaction.message.content);
const [, curr_page] = res;
return parseInt(curr_page);
}
async buttonNext(interaction) {
await interaction.deferUpdate();
await interaction.editReply(await this.history_message(interaction, this.__getCurrentPage(interaction) + 1));
}
async buttonPrevious(interaction) {
await interaction.deferUpdate();
await interaction.editReply(await this.history_message(interaction, this.__getCurrentPage(interaction) - 1));
}
async history_message(interaction, page = 1) {
const { d, a_d } = await this.__get_account(interaction);
let t_d = await SebAPI.personal_transactions_bypersonalaccountid(a_d.id, page);
if (!t_d.good) {
await interaction.editReply({ content: "Une erreur s'est produite." });
return;
}
if (t_d?.data?.data?.length <= 0) {
await interaction.editReply({ content: "Aucune transaction n'est enregistrée sur votre compte personnel." });
return;
}
t_d = t_d?.data;
const embed = new MessageEmbed()
.setColor(SoftConfig.get("bot.color", "#fb963a"))
.addField('ID', t_d.data.map((e) => "#" + e.id).join("\n"), true)
.addField('Date', t_d.data.map((e) => new Date(e.created_at).toLocaleString('fr-FR')).join("\n"), true)
.addField('Montant', t_d.data.map((e) => Number(e.amount).toLocaleString('fr-FR', { currency: 'EUR', currencyDisplay: 'symbol', style: 'currency' })).join('\n'), true);
const row = new MessageActionRow().addComponents(
new MessageButton()
.setCustomId('acc_history_previous')
.setLabel("<")
.setDisabled(t_d.current_page == 1)
.setStyle("PRIMARY"),
new MessageButton()
.setCustomId('acc_history_next')
.setLabel(">")
.setDisabled(t_d.current_page == t_d.last_page)
.setStyle("PRIMARY")
);
return { embeds: [embed], components: [row], content: `Historique du compte de ${d.fullname}\nPage ${t_d.current_page}/${t_d.last_page}` };
}
async exec_history(interaction) {
await interaction.deferReply({ ephemeral: true });
await interaction.editReply(await this.history_message(interaction));
}
async exec_balance(interaction) {
await interaction.deferReply({ ephemeral: true });
const { d, a_d } = await this.__get_account(interaction);
interaction.editReply({ content: `Solde du compte de ${d.fullname} : ${Number(a_d.balance).toLocaleString('fr-FR', { currency: 'EUR', currencyDisplay: 'symbol', style: 'currency' })}` });
}
async execute(interaction) {
await this.sub[interaction.options.getSubcommand()](interaction);
}
}
export default AccountCommand;
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment