Skip to content

Commit

Permalink
Servers list command #69
Browse files Browse the repository at this point in the history
  • Loading branch information
niqore committed Aug 5, 2020
1 parent ee0a7f7 commit 8bf74d4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 31 deletions.
34 changes: 3 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,16 @@ const Draftbot = require('core/DraftBot');
* @return {string}
*/
const getJoinLeaveMessage = (guild, join, language) => {
let humans = guild.members.cache.filter(member => !member.user.bot).size;
let robots = guild.members.cache.filter(member => member.user.bot).size;
let ratio = Math.round((robots / humans) * 100);
let { validation, humans, bots, ratio } = getValidationInfos(guild);
return format(join ? JsonReader.bot.getTranslation(language).joinGuild : JsonReader.bot.getTranslation(language).leaveGuild, {
guild: guild,
humans: humans,
robots: robots,
robots: bots,
ratio: ratio,
validation: getGuildValidation(guild, humans, robots, ratio)
validation: validation
});
};

/**
* Get validation emoji of a guild
* @param {module:"discord.js".Guild} guild
* @param {Number} humans - will be calculated if not provided
* @param {Number} robots - will be calculated if not provided
* @param {Number} ratio - will be calculated if not provided
* @return {string}
*/
const getGuildValidation = (guild, humans = -1, robots = -1, ratio = -1) => {
if (humans === -1) {
humans = guild.members.cache.filter(member => !member.user.bot).size;
robots = guild.members.cache.filter(member => member.user.bot).size;
ratio = Math.round((robots / humans) * 100);
}
let validation = ":white_check_mark:";
if (ratio > 30 || humans < 30 || (humans < 100 && ratio > 20)) {
validation = ":x:";
}
else {
if (ratio > 20 || robots > 15 || humans < 100) {
validation = ":warning:";
}
}
return validation;
};

/**
* Will be executed each time the bot see a message
* @param {module:"discord.js".Message} message
Expand Down
4 changes: 4 additions & 0 deletions ressources/text/bot.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"resetIsNow": ":trophy: Le classement de la semaine est en cours de réinitialisation ! Le gagnant sera annoncé sous peu sur le serveur du bot ! Le jeu reprendra dans moins de 5 minutes !",
"joinGuild": "**:inbox_tray: Serveur discord rejoint :** `{guild}` | :bust_in_silhouette: : `{humans}` | :robot: : `{robots}` | Ratio bot/Humain : `{ratio}` % | Validation : {validation}",
"leaveGuild": "**:outbox_tray: Serveur discord quitté :** `{guild}` | :bust_in_silhouette: : `{humans}` | :robot: : `{robots}` | Ratio bot/Humain : `{ratio}` % | Validation : {validation}",
"serverList": "**:outbox_tray: Serveur {count} :** `{guild}` | :bust_in_silhouette: : `{humans}` | :robot: : `{robots}` | Ratio bot/Humain : `{ratio}` % | Validation : {validation}",
"totalUsersCount": "**Nombre total d'utilisateurs : `{count}`**",
"mentionHelp": ":wave: Bonjour !\n\n:arrow_right: Les commandes commencent par `{prefix}`. Exemple: `{prefix}help`\n\n:arrow_right: Vous n'aimez pas `{prefix}` ? Changez le avec `{prefix}prefix <nouveau prefix>`\n\n:flag_gb: You speak english? Change the language with `{prefix}language`\n\nSi vous avez besoin d'aide, n'hésitez pas à rejoindre le serveur support: https://discord.gg/USnCxg4"
},
"en": {
Expand All @@ -16,6 +18,8 @@
"resetIsNow": ":trophy: The weekly ranking is currently being reinitialized. The winner will soon be announced on the bot's server! The game will resume in less than 5 minutes!",
"joinGuild": "**:inbox_tray: Discord server joined :** `{guild}` | :bust_in_silhouette: : `{humans}` | :robot: : `{robots}` | Bot/human ratio : `{ratio}` % | Validation : {validation}",
"leaveGuild": "**:outbox_tray: Discord server leaved :** `{guild}` | :bust_in_silhouette: : `{humans}` | :robot: : `{robots}` | Bot/human ratio : `{ratio}` % | Validation : {validation}",
"serverList": "**:outbox_tray: Server {count} :** `{guild}` | :bust_in_silhouette: : `{humans}` | :robot: : `{robots}` | Bot/human ratio : `{ratio}` % | Validation : {validation}",
"totalUsersCount": "**Total users count : `{count}`**",
"mentionHelp": ":wave: Hello !\n\n:arrow_right: Commands start with `{prefix}`. Example: `{prefix}help`\n\n:arrow_right: You don't like `{prefix}`? Change it with `{prefix}prefix <new prefix>`\n\n:flag_fr: Vous parlez français ? Changez la langue avec `{prefix}language`\n\nIf you need help, don't hesitate to join the support server: https://discord.gg/USnCxg4"
}
},
Expand Down
47 changes: 47 additions & 0 deletions src/commands/admin/ServersCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Allows an admin to check the server list
* @param {("fr"|"en")} language - Language to use in the response
* @param {module:"discord.js".Message} message - Message from the discord server
* @param {String[]} args=[] - Additional arguments sent with the command
*/
const ServersCommand = async (language, message, args) => {
if ((await canPerformCommand(message, language,
PERMISSION.ROLE.BOTOWNER)) !== true) {
return;
}

let count = 0;
let total = 0;
let result = "";

function logMapElements(guild) {
count++;
let { validation, humans, bots, ratio } = getValidationInfos(guild);
total += humans;
result += format(JsonReader.bot.getTranslation(language).serverList, {
count: count,
guild: guild,
humans: humans,
robots: bots,
ratio: ratio,
validation: validation
}) + "\n";
if (result.length > 1800) {
message.channel.send(result);
result = "";
}
}

client.guilds.cache.forEach(logMapElements);
result += '\n' + format(JsonReader.bot.getTranslation(language).totalUsersCount, { count: total });
message.channel.send(result);
};

module.exports = {
commands: [
{
name: 'servs',
func: ServersCommand
}
]
};
20 changes: 20 additions & 0 deletions src/core/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,23 @@ global.parseTimeDifference = function (date1, date2, language) {
global.resetIsNow = function () {
return getNextSundayMidnight() - new Date() <= 1000 * 5 * 60;
};

/**
* Allow to get the validation information of a guild
* @param {module:"discord.js".Guild} guild - The guild that has to be checked
*/
global.getValidationInfos = function(guild) {
let humans = guild.members.cache.filter(member => !member.user.bot).size;
let bots = guild.members.cache.filter(member => member.user.bot).size;
let ratio = Math.round((bots / humans) * 100);
let validation = ":white_check_mark:";
if (ratio > 30 || humans < 30 || (humans < 100 && ratio > 20)) {
validation = ":x:";
}
else {
if (ratio > 20 || bots > 15 || humans < 100) {
validation = ":warning:";
}
}
return { validation: validation, humans: humans, bots: bots, ratio: ratio };
};

0 comments on commit 8bf74d4

Please sign in to comment.