JaBa/commands/Fun/number.js

166 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2024-04-29 15:57:02 +05:00
const { SlashCommandBuilder, MessageCollector, ButtonBuilder, ActionRowBuilder, ButtonStyle, ThreadAutoArchiveDuration } = require("discord.js");
2022-08-09 23:48:33 +05:00
const BaseCommand = require("../../base/BaseCommand"),
currentGames = {};
class Number extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
2022-08-09 23:48:33 +05:00
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("number")
2023-06-06 22:48:46 +05:00
.setDescription(client.translate("fun/number:DESCRIPTION"))
.setDescriptionLocalizations({
2023-07-05 00:58:06 +05:00
uk: client.translate("fun/number:DESCRIPTION", null, "uk-UA"),
ru: client.translate("fun/number:DESCRIPTION", null, "ru-RU"),
})
.setDMPermission(false),
2022-08-09 23:48:33 +05:00
dirname: __dirname,
ownerOnly: false,
2022-08-09 23:48:33 +05:00
});
}
2022-08-09 23:48:33 +05:00
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
2022-08-09 23:48:33 +05:00
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
async execute(client, interaction, data) {
2023-06-06 22:48:46 +05:00
if (currentGames[interaction.guildId]) return interaction.error("fun/number:GAME_RUNNING");
2022-08-09 23:48:33 +05:00
const participants = [],
number = client.functions.randomNum(1000, 5000);
2023-06-06 22:48:46 +05:00
await interaction.replyT("fun/number:GAME_START");
2022-08-09 23:48:33 +05:00
2024-04-29 15:57:02 +05:00
let channel;
if (interaction.channel.isThread()) channel = interaction.channel;
else
channel = await interaction.channel.threads.create({
name: `number-guessing-${client.functions.randomNum(10000, 20000)}`,
autoArchiveDuration: ThreadAutoArchiveDuration.OneHour,
});
2022-08-09 23:48:33 +05:00
2024-04-29 15:57:02 +05:00
const gameCreatedAt = Date.now();
2022-08-09 23:48:33 +05:00
const filter = m => !m.author.bot;
2024-04-29 15:57:02 +05:00
const collector = new MessageCollector(channel, {
2022-08-09 23:48:33 +05:00
filter,
2023-07-05 00:58:06 +05:00
time: 5 * 60 * 1000,
2022-08-09 23:48:33 +05:00
});
currentGames[interaction.guildId] = true;
collector.on("collect", async msg => {
2024-04-29 15:57:02 +05:00
if (!participants.includes(msg.author)) participants.push(msg.author);
2022-08-09 23:48:33 +05:00
if (msg.content === "STOP") return collector.stop("force");
if (isNaN(msg.content)) return;
const parsedNumber = parseInt(msg.content, 10);
if (parsedNumber === number) {
2024-04-29 15:57:02 +05:00
channel.send({
2023-06-06 22:48:46 +05:00
content: interaction.translate("fun/number:GAME_STATS", {
2022-08-09 23:48:33 +05:00
winner: msg.author.toString(),
number,
time: `<t:${Math.floor(gameCreatedAt / 1000)}:R>`,
2022-08-09 23:48:33 +05:00
participantCount: participants.length,
2024-04-29 15:57:02 +05:00
participants: participants.map(p => p.toString()).join(", "),
}),
2022-08-09 23:48:33 +05:00
});
if (participants.length > 1) {
const won = 100 * (participants.length * 0.5);
2024-04-29 15:57:02 +05:00
channel.send({
2023-06-06 22:48:46 +05:00
content: interaction.translate("fun/number:WON", {
2024-04-29 15:57:02 +05:00
winner: msg.author.toString(),
credits: `**${won}** ${client.functions.getNoun(won, interaction.translate("misc:NOUNS:CREDIT:1"), interaction.translate("misc:NOUNS:CREDIT:2"), interaction.translate("misc:NOUNS:CREDIT:5"))}`,
}),
2022-08-09 23:48:33 +05:00
});
const memberData = await client.getMemberData(msg.author.id, interaction.guildId);
2022-08-09 23:48:33 +05:00
2022-10-09 14:26:22 +05:00
memberData.money += won;
2022-08-09 23:48:33 +05:00
const info = {
user: interaction.translate("economy/transactions:NUMBERS"),
amount: won,
date: Date.now(),
type: "got",
2022-08-09 23:48:33 +05:00
};
data.memberData.transactions.push(info);
await memberData.save();
}
2023-10-19 22:48:38 +05:00
2024-04-29 15:57:02 +05:00
interaction.editReply({
content: interaction.translate("fun/number:GAME_STATS", {
winner: msg.author.toString(),
number,
time: `<t:${Math.floor(gameCreatedAt / 1000)}:R>`,
participantCount: participants.length,
participants: participants.map(p => p.toString()).join(", "),
}),
});
const deleteYes = new ButtonBuilder()
.setCustomId("number_delete_yes")
.setLabel(interaction.translate("common:YES"))
.setStyle(ButtonStyle.Danger);
const deleteNo = new ButtonBuilder()
.setCustomId("number_delete_no")
.setLabel(interaction.translate("common:NO"))
.setStyle(ButtonStyle.Secondary);
const row = new ActionRowBuilder().addComponents(deleteYes, deleteNo);
channel.send({
content: interaction.translate("fun/number:DELETE_CHANNEL"),
components: [row],
});
2022-08-09 23:48:33 +05:00
collector.stop();
}
2023-07-05 00:58:06 +05:00
if (parseInt(msg.content) < number)
msg.reply({
content: interaction.translate("fun/number:TOO_BIG", { user: msg.author.toString(), number: parsedNumber }),
});
if (parseInt(msg.content) > number)
msg.reply({
content: interaction.translate("fun/number:TOO_SMALL", { user: msg.author.toString(), number: parsedNumber }),
});
2022-08-09 23:48:33 +05:00
});
collector.on("end", (_, reason) => {
2022-08-09 23:48:33 +05:00
delete currentGames[interaction.guildId];
2024-04-29 15:57:02 +05:00
2023-06-06 22:48:46 +05:00
if (reason === "time") return interaction.editReply({ content: interaction.translate("fun/number:DEFEAT", { number }) });
else if (reason === "force") return interaction.editReply({ content: interaction.translate("misc:FORCE_STOP", { user: interaction.member.toString(), number }) });
2022-08-09 23:48:33 +05:00
});
}
2024-04-29 15:57:02 +05:00
/**
*
* @param {import("../../base/Client")} client
*/
async onLoad(client) {
client.on("interactionCreate", async interaction => {
if (!interaction.isButton()) return;
if (interaction.customId.startsWith("number_")) {
await interaction.deferUpdate();
if (interaction.customId === "number_delete_yes") interaction.channel.delete();
else if (interaction.customId === "number_delete_no") {
await interaction.message.delete();
interaction.channel.setArchived(true);
}
}
});
}
2022-08-09 23:48:33 +05:00
}
2023-07-05 00:58:06 +05:00
module.exports = Number;