mirror of
https://github.com/cassoule/flopobot_v2.git
synced 2026-01-18 16:37:40 +01:00
final touch
This commit is contained in:
@@ -6,7 +6,7 @@ import { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'disc
|
||||
|
||||
import { postAPOBuy } from '../../utils/index.js';
|
||||
import { DiscordRequest } from '../../api/discord.js';
|
||||
import { getAllAvailableSkins, updateSkin } from '../../database/index.js';
|
||||
import {getAllAvailableSkins, getUser, insertLog, updateSkin, updateUserCoins} from '../../database/index.js';
|
||||
import { skins } from '../../game/state.js';
|
||||
|
||||
/**
|
||||
@@ -22,28 +22,40 @@ export async function handleValorantCommand(req, res, client) {
|
||||
const valoPrice = parseInt(process.env.VALO_PRICE, 10) || 500;
|
||||
|
||||
try {
|
||||
// TODO acheter en FlopoCoins
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: "Les caisses Valorant sont temporairement désactivées.",
|
||||
flags: InteractionResponseFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
// --- 1. Verify and process payment ---
|
||||
const buyResponse = await postAPOBuy(userId, valoPrice);
|
||||
|
||||
if (!buyResponse.ok) {
|
||||
const errorData = await buyResponse.json();
|
||||
const errorMessage = errorData.message || `Tu n'as pas assez d'argent... Il te faut ${valoPrice}€.`;
|
||||
const commandUser = getUser.get(userId);
|
||||
if (!commandUser) {
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: errorMessage,
|
||||
content: "Erreur lors de la récupération de votre profil utilisateur.",
|
||||
flags: InteractionResponseFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (commandUser.coins < valoPrice) {
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: `Pas assez de FlopoCoins (${valoPrice} requis).`,
|
||||
flags: InteractionResponseFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
insertLog.run({
|
||||
id: `${userId}-${Date.now()}`,
|
||||
user_id: userId,
|
||||
action: 'VALO_CASE_OPEN',
|
||||
target_user_id: null,
|
||||
coins_amount: -valoPrice,
|
||||
user_new_amount: commandUser.coins - valoPrice,
|
||||
});
|
||||
updateUserCoins.run({
|
||||
userId: userId,
|
||||
coins: commandUser.coins - valoPrice,
|
||||
})
|
||||
|
||||
// --- 2. Send Initial "Opening" Response ---
|
||||
// Acknowledge the interaction immediately with a loading message.
|
||||
|
||||
@@ -9,7 +9,7 @@ import { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'disc
|
||||
import { DiscordRequest } from '../../api/discord.js';
|
||||
import { postAPOBuy } from '../../utils/index.js';
|
||||
import { activeInventories, skins } from '../../game/state.js';
|
||||
import { getSkin, updateSkin } from '../../database/index.js';
|
||||
import {getSkin, getUser, insertLog, updateSkin, updateUserCoins} from '../../database/index.js';
|
||||
|
||||
/**
|
||||
* Handles the click of the 'Upgrade' button on a skin in the inventory.
|
||||
@@ -20,16 +20,6 @@ export async function handleUpgradeSkin(req, res) {
|
||||
const { member, data } = req.body;
|
||||
const { custom_id } = data;
|
||||
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: "Les améliorations de skins sont temporairement désactivées.",
|
||||
flags: InteractionResponseFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
|
||||
//TODO paiement en FlopoCoins
|
||||
|
||||
const interactionId = custom_id.replace('upgrade_', '');
|
||||
const userId = member.user.id;
|
||||
|
||||
@@ -63,18 +53,40 @@ export async function handleUpgradeSkin(req, res) {
|
||||
|
||||
// --- 2. Handle Payment ---
|
||||
const upgradePrice = parseFloat(process.env.VALO_UPGRADE_PRICE) || parseFloat(skinToUpgrade.maxPrice) / 10;
|
||||
try {
|
||||
const buyResponse = await postAPOBuy(userId, upgradePrice);
|
||||
if (!buyResponse.ok) {
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: { content: `Il vous faut ${upgradePrice.toFixed(0)}€ pour tenter cette amélioration.`, flags: InteractionResponseFlags.EPHEMERAL },
|
||||
});
|
||||
}
|
||||
} catch (paymentError) {
|
||||
console.error("Payment API error:", paymentError);
|
||||
return res.status(500).json({ error: "Payment service unavailable."});
|
||||
|
||||
const commandUser = getUser.get(userId);
|
||||
|
||||
if (!commandUser) {
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: "Erreur lors de la récupération de votre profil utilisateur.",
|
||||
flags: InteractionResponseFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (commandUser.coins < upgradePrice) {
|
||||
return res.send({
|
||||
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: `Pas assez de FlopoCoins (${upgradePrice.toFixed(0)} requis).`,
|
||||
flags: InteractionResponseFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
insertLog.run({
|
||||
id: `${userId}-${Date.now()}`,
|
||||
user_id: userId,
|
||||
action: 'VALO_SKIN_UPGRADE',
|
||||
target_user_id: null,
|
||||
coins_amount: -upgradePrice.toFixed(0),
|
||||
user_new_amount: commandUser.coins - upgradePrice.toFixed(0),
|
||||
});
|
||||
updateUserCoins.run({
|
||||
userId: userId,
|
||||
coins: commandUser.coins - upgradePrice.toFixed(0),
|
||||
})
|
||||
|
||||
|
||||
// --- 3. Show Loading Animation ---
|
||||
|
||||
Reference in New Issue
Block a user