JaBa/commands/Economy/number.js

98 lines
3 KiB
JavaScript
Raw Normal View History

const Command = require("../../base/Command"),
2022-01-04 02:18:28 +05:00
Discord = require("discord.js");
const currentGames = {};
class Number extends Command {
constructor(client) {
super(client, {
name: "number",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: ["num"],
memberPermissions: [],
botPermissions: ["SEND_MESSAGES", "EMBED_LINKS"],
nsfw: false,
ownerOnly: false,
cooldown: 2000
});
}
async run(message, args, data) {
if (currentGames[message.guild.id]) return message.error("fun/number:GAME_RUNNING");
const participants = [],
number = this.client.functions.randomNum(1000, 10000);
2022-01-04 02:18:28 +05:00
await message.sendT("fun/number:GAME_START");
// Store the date wich the game has started
const gameCreatedAt = Date.now();
const filter = m => !m.author.bot;
const collector = new Discord.MessageCollector(message.channel, {
filter,
2022-01-04 02:18:28 +05:00
time: 480000 // 8 minutes
});
currentGames[message.guild.id] = true;
collector.on("collect", async msg => {
if (this.client.functions.getPrefix(msg, data)) return;
if (!participants.includes(msg.author.id)) participants.push(msg.author.id);
2022-01-05 22:04:46 +05:00
if (msg.content === "STOP") return collector.stop("force");
2022-01-04 02:18:28 +05:00
if (isNaN(msg.content)) return;
const parsedNumber = parseInt(msg.content, 10);
if (parsedNumber === number) {
const time = this.client.functions.convertTime(message.guild, Date.now() - gameCreatedAt);
message.sendT("fun/number:GAME_STATS", {
winner: msg.author.toString(),
number,
time,
participantCount: participants.length,
participants: participants.map(p => `<@${p}>`).join(", ")
});
if (participants.length > 1 && data.guild.disabledCategories && !data.guild.disabledCategories.includes("Economy")) {
const won = 100 * (participants.length * 0.5);
message.sendT("fun/number:WON", {
winner: msg.author.username,
credits: `**${won}** ${message.getNoun(won, message.translate("misc:NOUNS:CREDIT:1"), message.translate("misc:NOUNS:CREDIT:2"), message.translate("misc:NOUNS:CREDIT:5"))}`
});
const memberData = await this.client.findOrCreateMember({
2022-01-04 02:18:28 +05:00
id: msg.author.id,
guildID: message.guild.id
});
2022-01-05 22:04:46 +05:00
const info = {
user: message.translate("economy/transactions:NUMBERS"),
amount: won,
date: Date.now(),
type: "got"
};
data.memberData.transactions.push(info);
memberData.money += won;
memberData.save();
2022-01-13 00:26:23 +05:00
}
collector.stop();
2022-01-13 00:26:23 +05:00
}
2022-01-05 22:04:46 +05:00
if (parseInt(msg.content) < number) message.error("fun/number:BIG", { user: msg.author.toString(), number: parsedNumber });
if (parseInt(msg.content) > number) message.error("fun/number:SMALL", { user: msg.author.toString(), number: parsedNumber });
2022-01-04 02:18:28 +05:00
});
collector.on("end", (_collected, reason) => {
delete currentGames[message.guild.id];
2022-01-13 00:26:23 +05:00
if (reason === "time") return message.error("fun/number:DEFEAT", { number });
else if (reason === "force") return message.error("misc:FORCE_STOP", { user: message.author.toString() });
2022-01-04 02:18:28 +05:00
});
}
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
module.exports = Number;