🗃️ /credits take is now on mongodb

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

View file

@ -3,44 +3,40 @@ 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 = {
title: 'Give failed',
description: 'You need to have permission to manage this guild (MANAGE_GUILD)',
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const user = await interaction.options.getUser('user');
const amount = await interaction.options.getInteger('amount');
try {
if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) {
const embed = {
title: 'Give failed',
description: 'You need to have permission to manage this guild (MANAGE_GUILD)',
};
return await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const user = await interaction.options.getUser('user');
const amount = await interaction.options.getInteger('amount');
if (amount <= 0) {
const embed = {
title: 'Give',
description: "You can't give zero or below.",
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 {
await credits
.findOneAndUpdate(
{ userId: user.id },
{ $inc: { balance: amount } },
{ new: true, upsert: true }
)
.then(async () => {
const embed = {
title: 'Give',
description: `Gave ${amount <= 1 ? `${amount} credit` : `${amount} credits`} to ${user}.`,
color: 0x22bb33,
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);
});
if (amount <= 0) {
const embed = {
title: 'Give',
description: "You can't give zero or below.",
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 toUser = await credits.findOne({ userId: user.id });
toUser.balance += amount;
toUser.save();
const embed = {
title: 'Give',
description: `Gave ${amount <= 1 ? `${amount} credit` : `${amount} credits`} to ${user}.`,
color: 0x22bb33,
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);
}
};

View file

@ -1,8 +1,7 @@
const { Permissions } = require('discord.js');
const db = require('quick.db');
const credits = new db.table('credits');
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 = {
@ -12,7 +11,7 @@ module.exports = async (interaction) => {
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.reply({ embeds: [embed], ephemeral: true });
return await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const user = await interaction.options.getUser('user');
const amount = await interaction.options.getInteger('amount');
@ -25,9 +24,11 @@ module.exports = async (interaction) => {
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.reply({ embeds: [embed], ephemeral: true });
return await interaction.editReply({ embeds: [embed], ephemeral: true });
} else {
await credits.subtract(user.id, amount);
const toUser = await credits.findOne({ userId: user.id });
toUser.balance -= amount;
toUser.save();
const embed = {
title: 'Take',
@ -36,6 +37,6 @@ module.exports = async (interaction) => {
timestamp: new Date(),
footer: { iconURL: process.env.FOOTER_ICON, text: process.env.FOOTER_TEXT },
};
return await interaction.reply({ embeds: [embed], ephemeral: true });
return await interaction.editReply({ embeds: [embed], ephemeral: true });
}
};