JaBa/commands/Fun/findwords.js

133 lines
4 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const Command = require("../../base/Command.js"),
Discord = require("discord.js");
const currentGames = {};
class FindWords extends Command {
constructor (client) {
super(client, {
name: "findwords",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: [],
memberPermissions: [],
botPermissions: [ "SEND_MESSAGES", "EMBED_LINKS" ],
nsfw: false,
ownerOnly: false,
cooldown: 2000
2021-12-10 21:39:54 +05:00
});
}
async run (message, args, data) {
if (currentGames[message.guild.id]) return message.error("fun/number:GAME_RUNNING");
// 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`);
2021-12-10 21:39:54 +05:00
// Init some utils variables
const participants = [],
winners = [],
words = [],
nbGames = 4;
// 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());
};
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
2021-12-11 21:08:37 +05:00
function generateGame(word) {
2021-12-10 21:39:54 +05:00
word = word.toLowerCase();
// Launch timer
const delay = (i === 0) ? 10000 : 0;
if (i === 0) message.sendT("fun/findwords:GAME_STARTING");
setTimeout(() => {
// Send announcment message
message.sendT("fun/findwords:FIND_WORD", { word }, false, false, "warn");
// init a collector to receive the answers
const collector = new Discord.MessageCollector(message.channel, (m) => !m.author.bot, {
time: 20000
});
collector.on("collect", (msg) => {
if (!participants.includes(msg.author.id)) {
participants.push(msg.author.id);
};
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("fun/findwords:INVALID_WORD", { member: msg.author.toString() });
};
});
collector.on("end", async (collected, reason) => {
if (reason === "time") {
message.error("fun/findwords:NO_WINNER");
} else {
message.success("fun/findwords:WORD_FOUND", { winner: `<@${reason}>` });
winners.push(reason);
}
2021-12-11 01:11:50 +05:00
if (i < nbGames - 1) {
2021-12-10 21:39:54 +05:00
i++;
generateGame.call(this, words[i]);
} else {
currentGames[message.guild.id] = false;
if (winners.length < 1) return message.error("fun/findwords:NO_WINNER_ALL");
const winnerID = await getWinner(winners);
const time = message.convertTime(createdAt, "from", true);
const user = await this.client.users.fetch(winnerID);
message.sendT("fun/findwords:GAME_STATS", {
winner: user.username,
duration: time,
participantCount: participants.length,
2021-12-24 20:52:27 +05:00
participantList: participants.map((p) => `<@${p}>`).join("\n")
2021-12-10 21:39:54 +05:00
});
if (participants.length > 1 && data.guild.disabledCategories && !data.guild.disabledCategories.includes("Economy")) {
message.sendT("fun/findwords:CREDITS", { winner: user.username });
const userdata = await this.client.findOrCreateMember({ id: user.id, guildID: message.guild.id });
userdata.money = userdata.money + 15;
userdata.save();
};
};
});
}, delay);
};
2021-12-11 21:08:37 +05:00
async function getWinner(array) {
return new Promise(function (resolve) {
2021-12-10 21:39:54 +05:00
const counts = {};
let compare = 0;
let 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;
};
2021-12-11 21:08:37 +05:00
if (counts[winner] > compare) {
2021-12-10 21:39:54 +05:00
compare = counts[winner];
mostFrequent = array[i];
};
};
resolve(mostFrequent);
});
};
}
};
module.exports = FindWords;