Manual code drops #341

This commit is contained in:
Axel Olausson Holtenäs 2022-05-29 20:24:24 +02:00
parent 75c9fdf438
commit 5623136fcd
No known key found for this signature in database
GPG key ID: 7BF6826B76382CBA
3 changed files with 132 additions and 2 deletions

View file

@ -13,7 +13,8 @@ export const builder = (group: SlashCommandSubcommandGroupBuilder) => {
.addSubcommand(modules.give.builder)
.addSubcommand(modules.set.builder)
.addSubcommand(modules.take.builder)
.addSubcommand(modules.transfer.builder);
.addSubcommand(modules.transfer.builder)
.addSubcommand(modules.drop.builder);
};
export const execute = async (interaction: CommandInteraction) => {
@ -26,5 +27,7 @@ export const execute = async (interaction: CommandInteraction) => {
return modules.take.execute(interaction);
case "transfer":
return modules.transfer.execute(interaction);
case "drop":
return modules.drop.execute(interaction);
}
};

View file

@ -0,0 +1,126 @@
// Dependencies
import { CommandInteraction, MessageEmbed, Permissions } from "discord.js";
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
import { v4 as uuidv4 } from "uuid";
import axios from "axios";
import apiSchema from "../../../../../../database/schemas/api";
import encryption from "../../../../../../handlers/encryption";
// Configurations
import getEmbedConfig from "../../../../../../helpers/getEmbedConfig";
// Handlers
import logger from "../../../../../../logger";
// Helpers
import pluralize from "../../../../../../helpers/pluralize";
// Models
import fetchUser from "../../../../../../helpers/fetchUser";
// Function
export default {
metadata: {
guildOnly: true,
ephemeral: true,
permissions: [Permissions.FLAGS.MANAGE_GUILD],
},
builder: (command: SlashCommandSubcommandBuilder) => {
return command
.setName("drop")
.setDescription("Drop some credits for specified amount of users.")
.addIntegerOption((option) =>
option
.setName("uses")
.setDescription("How many users should be able to use this.")
.setRequired(true)
)
.addIntegerOption((option) =>
option
.setName("credit")
.setDescription(`How much credits provided per use.`)
.setRequired(true)
)
.addChannelOption((option) =>
option
.setName("channel")
.setDescription("The channel to send the message to.")
.setRequired(true)
);
},
execute: async (interaction: CommandInteraction) => {
if (interaction.guild == null) return;
const { errorColor, successColor, footerText, footerIcon } =
await getEmbedConfig(interaction.guild); // Destructure
const { guild, options } = interaction;
const uses = options?.getInteger("uses");
const creditAmount = options?.getInteger("credit");
const channel = options?.getChannel("channel");
if (!uses) throw new Error("Amount of uses is required.");
if (!creditAmount) throw new Error("Amount of credits is required.");
if (!channel) throw new Error("Channel is required.");
const embed = new MessageEmbed()
.setTitle("[:toolbox:] Drop")
.setFooter({ text: footerText, iconURL: footerIcon });
const code = uuidv4();
const apiCredentials = await apiSchema?.findOne({
guildId: guild?.id,
});
if (!apiCredentials) return;
const api = axios?.create({
baseURL: apiCredentials.url,
headers: {
Authorization: `Bearer ${encryption.decrypt(apiCredentials.token)}`,
},
});
const shopUrl = apiCredentials?.url?.replace("/api", "/store");
await api
.post("vouchers", {
uses,
code,
credits: creditAmount,
memo: `${interaction?.createdTimestamp} - ${interaction?.user?.id}`,
})
.then(async () => {
await interaction.editReply({
embeds: [
embed
.setColor(successColor)
.setDescription(`Successfully crated code: ${code}`),
],
});
const discordChannel = guild.channels.cache.get(channel.id);
if (!discordChannel) return;
if (discordChannel.type !== "GUILD_TEXT") return;
discordChannel.send({
embeds: [
new MessageEmbed()
.setTitle("[:parachute:] Code Drop!")
.addFields([
{ name: "Code", value: `${code}`, inline: true },
{ name: "Amount", value: `${creditAmount}`, inline: true },
{ name: "Uses", value: `${uses}`, inline: true },
])
.setDescription(
`${interaction.user} dropped a voucher! You can use the code [here](${shopUrl})!`
)
.setColor(successColor),
],
});
});
},
};

View file

@ -2,5 +2,6 @@ import give from "./give";
import set from "./set";
import take from "./take";
import transfer from "./transfer";
import drop from "./drop";
export default { give, set, take, transfer };
export default { give, set, take, transfer, drop };