JaBa/commands/Economy/findwords.js

157 lines
4.8 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 FindWords extends Command {
constructor(client) {
super(client, {
name: "findwords",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: ["findw"],
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("economy/number:GAME_RUNNING");
2022-01-04 02:18:28 +05:00
// Reads words file
let lang = null;
if (message.guild.data.language === "uk-UA") return lang = "ru-RU";
else lang = message.guild.data.language;
const wordList = require(`../../assets/json/words/${lang}.json`);
// Init some utils variables
const participants = [],
winners = [],
words = [],
nbGames = this.client.functions.randomNum(3, 10);
2022-01-04 02:18:28 +05:00
// Store the date wich the game has started
const createdAt = Date.now(); // 20929038303
for (let i = 0; i < nbGames; i++) {
const result = Math.floor((Math.random() * wordList.length));
words.push(wordList[result].substring(0, 3).toLowerCase());
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
let i = 0; // Inits i variable to count games
currentGames[message.guild.id] = true; // Update current game variable
generateGame.call(this, words[i]); // Generate a new round
function generateGame(word) {
word = word.toLowerCase();
// Launch timer
const delay = (i === 0) ? 10000 : 0;
if (i === 0) message.sendT("economy/findwords:GAME_STARTING");
2022-01-04 02:18:28 +05:00
setTimeout(() => {
// Send announcment message
message.sendT("economy/findwords:FIND_WORD", {
2022-01-04 02:18:28 +05:00
word: word.toUpperCase()
}, false, false, "warn");
// init a collector to receive the answers
const filter = m => !m.author.bot;
const collector = new Discord.MessageCollector(message.channel, {
filter,
2022-01-04 02:18:28 +05:00
time: 20000
});
collector.on("collect", (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 (msg.content.toLowerCase().indexOf(word) >= 0 && wordList.map((word) => word.toLowerCase()).indexOf(msg.content.toLowerCase()) >= 0) {
collector.stop(msg.author.id); // Stop the collector
} else msg.error("economy/findwords:INVALID_WORD", { member: msg.author.toString() });
2022-01-04 02:18:28 +05:00
});
collector.on("end", async (collected, reason) => {
if (reason === "time") message.error("economy/findwords:NO_WINNER");
2022-01-13 00:26:23 +05:00
else if (reason === "force") return message.error("misc:FORCE_STOP", { user: message.author.toString() });
else {
message.success("economy/findwords:WORD_FOUND", {
2022-01-04 02:18:28 +05:00
winner: `<@${reason}>`
});
winners.push(reason);
2022-01-13 00:26:23 +05:00
}
2022-01-05 22:04:46 +05:00
2022-01-04 02:18:28 +05:00
if (i < nbGames - 1) {
i++;
generateGame.call(this, words[i]);
} else {
currentGames[message.guild.id] = false;
if (winners.length < 1) return message.error("economy/findwords:NO_WINNER_ALL");
2022-01-04 02:18:28 +05:00
const winnerID = await getWinner(winners);
const time = message.convertTime(createdAt, "from", true);
const user = await this.client.users.fetch(winnerID);
message.sendT("economy/findwords:GAME_STATS", {
2022-01-04 02:18:28 +05:00
winner: user.username,
duration: time,
participantCount: participants.length,
participants: participants.map((p) => `<@${p}>`).join(", ")
});
if (participants.length > 1 && data.guild.disabledCategories && !data.guild.disabledCategories.includes("Economy")) {
const won = 150 * (participants.length * 0.5);
message.sendT("economy/findwords:CREDITS", {
2022-01-04 02:18:28 +05:00
winner: user.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: user.id,
guildID: message.guild.id
});
2022-01-05 22:04:46 +05:00
const info = {
user: message.translate("economy/transactions:WORDS"),
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
}
}
2022-01-04 02:18:28 +05:00
});
}, delay);
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
async function getWinner(array) {
return new Promise(function (resolve) {
const counts = {};
let compare = 0,
mostFrequent;
for (let i = 0, len = array.length; i < len; i++) {
const winner = array[i];
if (!counts[winner]) counts[winner] = 1;
else counts[winner] = counts[winner] + 1;
if (counts[winner] > compare) {
compare = counts[winner];
mostFrequent = array[i];
2022-01-13 00:26:23 +05:00
}
}
2022-01-04 02:18:28 +05:00
resolve(mostFrequent);
});
2022-01-13 00:26:23 +05:00
}
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 = FindWords;