feat(arabot): create payment command

This commit is contained in:
smyalygames 2023-03-02 16:48:57 +00:00
parent d98f714477
commit 6bfe57c135
2 changed files with 233 additions and 0 deletions

181
src/commands/economy/pay.ts Normal file
View File

@ -0,0 +1,181 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
Animal Rights Advocates Discord Bot
Copyright (C) 2023 Anthony Berg
This program 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.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Args, Command, RegisterBehavior } from '@sapphire/framework';
import type { User, Guild, Message } from 'discord.js';
import { updateUser } from '#utils/database/dbExistingUser';
import { getBalance, transfer } from '#utils/database/economy';
import { EmbedBuilder } from 'discord.js';
export class BalanceCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
name: 'pay',
description: 'Give a user an amount of money',
});
}
// Registers that this is a slash command
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(
(builder) => builder
.setName(this.name)
.setDescription(this.description)
.addUserOption((option) => option.setName('user')
.setDescription('The user to give the money to')
.setRequired(true))
.addIntegerOption((option) => option.setName('amount')
.setDescription('The amount to give to the user')
.setMinValue(1)
.setRequired(true))
.addStringOption((option) => option.setName('reason')
.setDescription('The reason/reference for the transaction')
.setRequired(true)),
{
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
},
);
}
// Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
const recipient = interaction.options.getUser('user', true);
const amount = interaction.options.getInteger('amount', true);
const reason = interaction.options.getString('reason', true);
const { user, guild } = interaction;
if (guild === null) {
await interaction.reply({
content: 'Could not find the guild!',
ephemeral: true,
});
return;
}
await interaction.deferReply();
const info = await this.pay(user, recipient, amount, reason, guild);
await interaction.editReply({
content: info.message,
embeds: info.embeds,
});
}
public async messageRun(message: Message, args: Args) {
let recipient: User;
try {
recipient = await args.pick('user');
} catch {
await message.react('❌');
await message.reply('User was not provided!');
return;
}
let amount: number;
try {
amount = await args.pick('integer');
} catch {
await message.react('❌');
await message.reply('Amount was not provided!');
return;
}
const reason = args.finished ? null : await args.rest('string');
if (reason === null) {
await message.react('❌');
await message.reply('Reason/reference was not provided!');
return;
}
const user = message.member?.user;
const { guild } = message;
if (user === undefined) {
await message.react('❌');
await message.reply('Could not find your user!');
return;
}
if (guild === null) {
await message.react('❌');
await message.reply('Could not find the guild!');
return;
}
const info = await this.pay(user, recipient, amount, reason, guild);
await message.reply({
content: info.message,
embeds: info.embeds,
});
if (!info.success) {
await message.react('❌');
}
}
private async pay(user: User, recipient: User, amount: number, reason: string, guild: Guild) {
const info = {
message: '',
embeds: [] as EmbedBuilder[],
success: false,
};
const member = guild.members.cache.get(user.id);
const recipientMember = guild.members.cache.get(recipient.id);
if (member === undefined) {
info.message = 'Could not find your guild member!';
return info;
}
if (recipientMember === undefined) {
info.message = 'Could not find the user!';
return info;
}
await updateUser(member);
await updateUser(recipientMember);
const balance = await getBalance(user.id);
if (balance.balance < amount) {
info.message = 'You don\'t have enough money to send!';
return info;
}
await transfer(user.id, recipient.id, amount, reason);
const embed = new EmbedBuilder()
.setColor('#00ff7d')
.setAuthor({ name: `Transfer to ${recipientMember.displayName}`, iconURL: `${recipientMember.displayAvatarURL()}` })
.addFields(
{ name: 'From', value: `${user}`, inline: true },
{ name: 'To', value: `${recipient}`, inline: true },
{ name: 'Amount', value: `${amount} ARA` },
{ name: 'Reason', value: reason },
);
info.success = true;
info.embeds.push(embed);
return info;
}
}

View File

@ -29,6 +29,58 @@ export async function getBalance(userId: Snowflake) {
return balance; return balance;
} }
// Pay
export async function transfer(
userId: Snowflake,
recipientId: Snowflake,
amount: number,
reason: string,
) {
const user = container.database.user.update({
where: {
id: userId,
},
data: {
Balance: {
update: {
balance: { decrement: amount },
},
},
SendPayment: {
create: {
recipient: {
connect: {
id: recipientId,
},
},
amount,
reason,
},
},
},
});
const recipient = container.database.balance.upsert({
where: {
userId: recipientId,
},
update: {
balance: { increment: amount },
},
create: {
user: {
connect: {
id: recipientId,
},
},
balance: amount,
},
});
await container.database.$transaction([user, recipient]);
}
// Daily // Daily
export async function daily(userId: Snowflake, amount: number) { export async function daily(userId: Snowflake, amount: number) {