From c620e492c9929fef722da2b462386e206ea21fd6 Mon Sep 17 00:00:00 2001 From: "Jonny_Bro (Nikita)" Date: Tue, 10 Oct 2023 20:44:42 +0500 Subject: [PATCH] some cleanup in helpers functions --- helpers/autoUpdateDocs.js | 8 +-- helpers/birthdays.js | 70 +++++++++++++-------------- helpers/checkReminds.js | 13 ++--- helpers/extenders.js | 10 ++-- helpers/functions.js | 10 ++-- languages/en-US/general/remindme.json | 2 +- 6 files changed, 58 insertions(+), 55 deletions(-) diff --git a/helpers/autoUpdateDocs.js b/helpers/autoUpdateDocs.js index b9c77c5a..0bcf6172 100644 --- a/helpers/autoUpdateDocs.js +++ b/helpers/autoUpdateDocs.js @@ -14,7 +14,7 @@ module.exports.update = function (client) { if (!categories.includes(cmd.category)) categories.push(cmd.category); }); - let text = `# JaBa имеет **${commands.length} ${client.functions.getNoun(commands.length, "команда", "команды", "команд")}** в **${categories.length} ${client.functions.getNoun(categories.length, "категории", "категориях", "категориях")}**! \n\n#### Содержимое таблицы \n**Название**: Название команды \n**Описание**: Описание команды \n**Использование**: Использование команды ( [] - обязательно, () - необязательно ) \n**Разрешено использование**: Где можно использовать команду \n\n`; + let text = `# JaBa has **${commands.length} ${client.functions.getNoun(commands.length, "command", "commands", "commands")}** in **${categories.length} ${client.functions.getNoun(categories.length, "category", "categories", "categories")}**! \n\n#### Table content \n**Name**: Command name \n**Description**: Command description \n**Usage**: How to use the command (*[]* - required, *()* - optional) \n**Accessible in**: Where you can use the command \n\n`; // categories.sort(function(a, b) { // const aCmdsSize = commands.filter(cmd => cmd.category === a).size; @@ -24,10 +24,10 @@ module.exports.update = function (client) { // }) categories.sort().forEach(cat => { - const categoriesArray = [["Название", "Описание", "Использование", "Разрешено использование"]]; + const categoriesArray = [["Name", "Description", "Usage", "Accessible in"]]; const cmds = [...new Map(commands.filter(cmd => cmd.category === cat).map(v => [v.constructor.name, v])).values()]; - text += `### ${cat} (${cmds.length} ${client.functions.getNoun(cmds.length, "команда", "команды", "команд")})\n\n`; + text += `### ${cat} (${cmds.length} ${client.functions.getNoun(cmds.length, "command", "commands", "commands")})\n\n`; cmds.sort(function (a, b) { if (a.command.name < b.command.name) return -1; else return 1; @@ -36,7 +36,7 @@ module.exports.update = function (client) { `**${cmd.command.name}** ${cmd.aliases.length ? `**(${cmd.aliases.join(", ")})**` : ""}`, client.translate(`${cmd.category.toLowerCase()}/${cmd.command.name}:DESCRIPTION`), `${cmd.command.name} ${client.translate(`${cmd.category.toLowerCase()}/${cmd.command.name}:USAGE`).replace(/\n/, " \\| ")}`, - cmd.command.dm_permission ? "На сервере и в ЛС бота" : "Только на сервере", + cmd.command.dm_permission ? "Servers/DMs" : "Only on servers", ]); }); text += `${table(categoriesArray)}\n\n`; diff --git a/helpers/birthdays.js b/helpers/birthdays.js index 274f5d2c..f9cb85f4 100644 --- a/helpers/birthdays.js +++ b/helpers/birthdays.js @@ -7,50 +7,50 @@ const { CronJob } = require("cron"), */ module.exports.init = async function (client) { new CronJob("0 5 * * *", async function () { - const date = new Date(), - currentDay = date.getDate(), - currentMonth = date.getMonth(), - currentYear = date.getFullYear(); - client.guilds.cache.forEach(async guild => { const guildData = await client.findOrCreateGuild({ id: guild.id }); if (guildData.plugins.birthdays) { - const channel = client.channels.cache.get(guildData.plugins.birthdays); + const channel = client.channels.cache.get(guildData.plugins.birthdays), + date = new Date(), + currentDay = date.getDate(), + currentMonth = date.getMonth(), + currentYear = date.getFullYear(); + if (channel) { client.usersData.find({ birthdate: { $gt: 1 } }).then(async users => { for (const user of users) { - if (guild.members.cache.find(m => m.id === user.id)) { - const userDate = new Date(user.birthdate); - const day = userDate.getDate(); - const month = userDate.getMonth(); - const year = userDate.getFullYear(); - const age = currentYear - year; + if (!guild.members.cache.find(m => m.id === user.id)) return; - if (currentMonth === month && currentDay === day) { - const embed = new EmbedBuilder() - .setAuthor({ - name: client.user.getUsername(), - iconURL: client.user.displayAvatarURL(), - }) - .setColor(client.config.embed.color) - .setFooter(client.config.embed.footer) - .addFields([ - { - name: client.translate("economy/birthdate:HAPPY_BIRTHDAY", null, guildData.language), - value: client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", { - name: user.username, - user: user.id, - age: `**${age}** ${client.functions.getNoun(age, client.translate("misc:NOUNS:AGE:1", null, guildData.language), client.translate("misc:NOUNS:AGE:2", null, guildData.language), client.translate("misc:NOUNS:AGE:5", null, guildData.language))}`, - }, guildData.language), - }, - ]); + const userDate = new Date(user.birthdate), + day = userDate.getDate(), + month = userDate.getMonth(), + year = userDate.getFullYear(), + age = currentYear - year; - const msg = await channel.send({ - embeds: [embed], - }); - await msg.react("🎉"); - } + if (currentMonth === month && currentDay === day) { + const embed = new EmbedBuilder() + .setAuthor({ + name: client.user.getUsername(), + iconURL: client.user.displayAvatarURL(), + }) + .setColor(client.config.embed.color) + .setFooter(client.config.embed.footer) + .addFields([ + { + name: client.translate("economy/birthdate:HAPPY_BIRTHDAY", null, guildData.language), + value: client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", { + name: user.username, + user: user.id, + age: `**${age}** ${client.functions.getNoun(age, client.translate("misc:NOUNS:AGE:1", null, guildData.language), client.translate("misc:NOUNS:AGE:2", null, guildData.language), client.translate("misc:NOUNS:AGE:5", null, guildData.language))}`, + }, guildData.language), + }, + ]); + + const msg = await channel.send({ + embeds: [embed], + }); + await msg.react("🎉"); } } }); diff --git a/helpers/checkReminds.js b/helpers/checkReminds.js index 9c816a43..9c039d80 100644 --- a/helpers/checkReminds.js +++ b/helpers/checkReminds.js @@ -9,18 +9,19 @@ module.exports.init = function (client) { client.usersData.find({ reminds: { $gt: [] } }).then(users => { for (const user of users) { if (!client.users.cache.has(user.id)) client.users.fetch(user.id); + client.databaseCache.usersReminds.set(user.id, user); } }); setInterval(async function () { - const dateNow = Date.now(); client.databaseCache.usersReminds.forEach(async user => { - const dUser = client.users.cache.get(user.id); + const cachedUser = client.users.cache.get(user.id); - if (dUser) { - const reminds = user.reminds; - const mustSent = reminds.filter(r => r.sendAt < dateNow); + if (cachedUser) { + const dateNow = Date.now(), + reminds = user.reminds, + mustSent = reminds.filter(r => r.sendAt < dateNow); if (mustSent.length > 0) { mustSent.forEach(r => { @@ -42,7 +43,7 @@ module.exports.init = function (client) { .setColor(client.config.embed.color) .setFooter(client.config.embed.footer); - dUser.send({ + cachedUser.send({ embeds: [embed], }); }); diff --git a/helpers/extenders.js b/helpers/extenders.js index 9081db4f..6639da79 100644 --- a/helpers/extenders.js +++ b/helpers/extenders.js @@ -5,15 +5,14 @@ User.prototype.getUsername = function () { }; BaseInteraction.prototype.translate = function (key, args) { - const language = this.client.translations.get(this.guild ? (this.guild.data ? this.guild.data.language : "ru-RU") : "ru-RU"); + const language = this.client.translations.get(this.guild ? this.guild.data.language : "en-US"); if (!language) throw "Interaction: Invalid language set in data."; return language(key, args); }; BaseInteraction.prototype.replyT = function (key, args, options = {}) { - let string = this.translate(key, args); - if (options.prefixEmoji) string = `${this.client.customEmojis[options.prefixEmoji]} | ${string}`; + const string = options.prefixEmoji ? `${this.client.customEmojis[options.prefixEmoji]} | ${string}` : this.translate(key, args, this.guild ? this.guild.data.language : "en-US"); if (options.edit) return this.editReply({ content: string, ephemeral: options.ephemeral || false }); else return this.reply({ content: string, ephemeral: options.ephemeral || false }); @@ -32,15 +31,14 @@ BaseInteraction.prototype.error = function (key, args, options = {}) { }; Message.prototype.translate = function (key, args) { - const language = this.client.translations.get(this.guild ? this.guild.data.language : "ru-RU"); + const language = this.client.translations.get(this.guild ? this.guild.data.language : "en-US"); if (!language) throw "Message: Invalid language set in data."; return language(key, args); }; Message.prototype.replyT = function (key, args, options = {}) { - let string = this.translate(key, args, this.guild ? this.guild.data.language : "ru-RU"); - if (options.prefixEmoji) string = `${this.client.customEmojis[options.prefixEmoji]} | ${string}`; + const string = options.prefixEmoji ? `${this.client.customEmojis[options.prefixEmoji]} | ${string}` : this.translate(key, args, this.guild ? this.guild.data.language : "en-US"); if (options.edit) return this.edit({ content: string, allowedMentions: { repliedUser: options.mention ? true : false } }); else return this.reply({ content: string, allowedMentions: { repliedUser: options.mention ? true : false } }); diff --git a/helpers/functions.js b/helpers/functions.js index 43d9ce92..48cdf8e9 100644 --- a/helpers/functions.js +++ b/helpers/functions.js @@ -16,9 +16,10 @@ module.exports = { * @returns {String} Invite Link */ async createInvite(client, guildId) { - const guild = client.guilds.cache.get(guildId); - const member = guild.members.me; - const channel = guild.channels.cache.find(ch => (ch.permissionsFor(member.id).has(PermissionsBitField.Flags.CreateInstantInvite) && ch.type === ChannelType.GuildText) || ch.type === "GUILD_VOICE"); + const guild = client.guilds.cache.get(guildId), + member = guild.members.me, + channel = guild.channels.cache.find(ch => (ch.permissionsFor(member.id).has(PermissionsBitField.Flags.CreateInstantInvite) && ch.type === ChannelType.GuildText) || ch.type === "GUILD_VOICE"); + if (channel) return (await channel.createInvite()).url || "No channels found or missing permissions"; }, @@ -83,6 +84,7 @@ module.exports = { randomNum(min, max) { min = Math.ceil(min); max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1) + min); }, @@ -97,6 +99,7 @@ module.exports = { printDate(client, date, format = null, locale = client.defaultLanguage) { const languageData = client.languages.find(language => language.name === locale); if (format === "" || format === null) format = languageData.defaultMomentFormat; + return moment(new Date(date)).locale(languageData.moment).format(format); }, @@ -112,6 +115,7 @@ module.exports = { convertTime(client, time, type = false, noPrefix = false, locale = client.defaultLanguage) { const languageData = client.languages.find(language => language.name === locale); const m = moment(time).locale(languageData.moment); + return type ? m.toNow(noPrefix) : m.fromNow(noPrefix); }, diff --git a/languages/en-US/general/remindme.json b/languages/en-US/general/remindme.json index 86959afb..9e2e7d95 100644 --- a/languages/en-US/general/remindme.json +++ b/languages/en-US/general/remindme.json @@ -5,5 +5,5 @@ "TIME": "Time (e.g., 10s, 5m, 2h, 1d, 3w)", "SAVED": "Reminder `{{message}}` saved for **{{time}}**", "TITLE": "Reminder from JaBa", - "CREATED": "Reminder created for: **{{time}}**" + "CREATED": "Reminder created: **{{time}}**" } \ No newline at end of file