🗃️ /credits redeem is now on mongodb

This commit is contained in:
Axel Olausson Holtenäs 2022-02-27 22:36:08 +01:00
parent 8b8343a26f
commit 8929d3a4dc
No known key found for this signature in database
GPG key ID: E3AE7E194AE017ED
2 changed files with 86 additions and 82 deletions

View file

@ -2,7 +2,6 @@ const { Permissions } = require('discord.js');
const credits = require('../../../helpers/database/models/creditSchema');
const debug = require('../../../handlers/debug');
module.exports = async (interaction) => {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
const embed = {

View file

@ -1,94 +1,99 @@
const { disableRedeem } = require('../../../../config.json');
const db = require('quick.db');
const credits = new db.table('credits');
const credits = require('../../../helpers/database/models/creditSchema');
const debug = require('../../../handlers/debug');
const api = require('../../../handlers/api.js');
const { v4: uuidv4 } = require('uuid');
module.exports = async (interaction) => {
if (disableRedeem) {
const embed = {
title: 'Redeem failed',
description: `Redeem is disabled until further.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const amount = await interaction.options.getInteger('amount');
try {
if (disableRedeem) {
const embed = {
title: 'Redeem failed',
description: `Redeem is disabled until further.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const amount = await interaction.options.getInteger('amount');
const userCredits = await credits.get(interaction.user.id);
const user = await credits.findOne({ userId: interaction.user.id });
if ((amount || userCredits) < 100) {
const embed = {
title: 'Redeem',
description: `You can't redeem below 100. Your balance is ${userCredits}.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else if ((amount || userCredits) > 1000000) {
const embed = {
title: 'Redeem',
description: `You can't redeem over 1,000,000. Your balance is ${userCredits}.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else if (userCredits < amount) {
const embed = {
title: 'Redeem',
description: `You have insufficient credits. Your balance is ${userCredits}.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else {
const code = uuidv4();
if ((amount || user.balance) < 100) {
const embed = {
title: 'Redeem',
description: `You can't redeem below 100. Your balance is ${user.balance}.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else if ((amount || user.balance) > 1000000) {
const embed = {
title: 'Redeem',
description: `You can't redeem over 1,000,000. Your balance is ${user.balance}.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else if (user.balance < amount) {
const embed = {
title: 'Redeem',
description: `You have insufficient credits. Your balance is ${user.balance}.`,
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else {
const code = uuidv4();
api
.post('vouchers', {
uses: 1,
code,
credits: amount || userCredits,
memo: `${interaction.createdTimestamp} - ${interaction.user.id}`,
})
.then(async (res) => {
const embed = {
title: 'Redeem',
description: `Your new balance is ${userCredits - (amount || userCredits)}.`,
fields: [
{ name: 'Code', value: `${code}`, inline: true },
{
name: 'Credits',
value: `${amount || userCredits}`,
inline: true,
},
],
color: 0x22bb33,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
await credits.subtract(interaction.user.id, amount || userCredits);
api
.post('vouchers', {
uses: 1,
code,
credits: amount || user.balance,
memo: `${interaction.createdTimestamp} - ${interaction.user.id}`,
})
.then(async (res) => {
const embed = {
title: 'Redeem',
description: `Your new balance is ${user.balance - (amount || user.balance)}.`,
fields: [
{ name: 'Code', value: `${code}`, inline: true },
{
name: 'Credits',
value: `${amount || user.balance}`,
inline: true,
},
],
color: 0x22bb33,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
user.balance -= amount || user.balance;
await interaction.editReply({ embeds: [embed], ephemeral: true });
})
.catch(async (err) => {
console.log(err);
const embed = {
title: 'Redeem',
description: 'Something went wrong.',
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
});
await user.save();
await interaction.editReply({ embeds: [embed], ephemeral: true });
})
.catch(async (err) => {
console.log(err);
const embed = {
title: 'Redeem',
description: 'Something went wrong.',
color: 0xbb2124,
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
});
}
} catch {
async (err) => debug(err);
}
};