Skip to content

Commit 0ab5b34

Browse files
committed
Menus now use the shared functions to clear the console, wait and show statuses
- Replaced the code that cleared the console and showed the logo in every menu with the shared clearConsole() function - Replaced the setTimeout waits with the shared wait() function - Replaced the enabled/disabled status texts with the shared getStatusText() function - Removed the local copies of these functions (in the general configuration utils, the greetings menu and the engagement, moderation, polls, scheduled messages and utilities menus) - The general configuration utils now re-export the shared functions
1 parent 9ff58b8 commit 0ab5b34

22 files changed

Lines changed: 68 additions & 148 deletions

‎lib/menus/baseGuildSelection.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Imports the necessary libraries
22
import { select } from '@inquirer/prompts';
3-
import { splashLogo } from 'helpbot/loaders';
4-
import { confirm } from './shared/utils.js';
3+
import { clearConsole, confirm } from './shared/utils.js';
54

65
// Stores the name of the menu
76
const menuName = new URL(import.meta.url).pathname.split('/').pop().replace('.js', '');
@@ -24,8 +23,7 @@ export default async (elegibleGuilds, beforeError) => {
2423
async function promptBaseGuildSelection() {
2524

2625
// Cleans the console and shows the logo
27-
process.stdout.write('\u001b[2J\u001b[0;0H');
28-
await splashLogo(client.locale.lib.loaders.splashLogo);
26+
await clearConsole();
2927

3028
// If an error ocurred on the previous run
3129
if (beforeError) {
@@ -59,8 +57,7 @@ export default async (elegibleGuilds, beforeError) => {
5957
newBaseGuildId = newBaseGuild.id;
6058

6159
// Cleans the console and shows the logo
62-
process.stdout.write('\u001b[2J\u001b[0;0H');
63-
await splashLogo(client.locale.lib.loaders.splashLogo);
60+
await clearConsole();
6461
};
6562

6663
// Runs the base guild selector

‎lib/menus/configure.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Imports the necessary libraries
22
import { select } from '@inquirer/prompts';
3-
import { splashLogo } from 'helpbot/loaders';
3+
import { clearConsole } from './shared/utils.js';
44

55
// Stores the name of the menu
66
const menuName = new URL(import.meta.url).pathname.split('/').pop().replace('.js', '');
@@ -14,8 +14,7 @@ export default async (beforeError) => {
1414
try {
1515

1616
// Cleans the console and shows the logo
17-
process.stdout.write('\u001b[2J\u001b[0;0H');
18-
await splashLogo(client.locale.lib.loaders.splashLogo);
17+
await clearConsole();
1918

2019
// Shows an error message if one occurred
2120
if (beforeError) console.log(`❌ ${locale.errorMessage}.\n`);

‎lib/menus/configure/engagement.js‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Imports the necessary libraries
22
import { select } from '@inquirer/prompts';
3-
import { splashLogo } from 'helpbot/loaders';
4-
import { setCommandStatus } from '../shared/utils.js';
3+
import { clearConsole, getStatusText, setCommandStatus } from '../shared/utils.js';
54
import { stopInterval, startInterval } from '../../loaders/intervals.js';
65
import experienceMenu from './engagement/experience.js';
76
import statisticsMenu from './engagement/statistics.js';
@@ -19,13 +18,6 @@ export default async (beforeError) => {
1918
// Stores the translations of the bot
2019
const locale = client.locale.lib.menus[parentMenuName].submenus[menuName];
2120

22-
// Function to clear the console
23-
async function clearConsole() {
24-
// Cleans the console and shows the logo
25-
process.stdout.write('\u001b[2J\u001b[0;0H');
26-
await splashLogo(client.locale.lib.loaders.splashLogo);
27-
}
28-
2921
// Function to draw the menu
3022
async function drawMenu() {
3123

@@ -41,9 +33,6 @@ export default async (beforeError) => {
4133
const experienceSystemEnabled = await client.functions.db.getConfig('leveling.experienceSystemEnabled');
4234
const collectedStatistics = [await client.functions.db.getConfig('leveling.collectMessagesCount'), await client.functions.db.getConfig('leveling.collectVoiceTime')];
4335

44-
// Function to get the text that shows a status
45-
const getStatusText = enabled => enabled ? `🟢 ${client.locale.status.enabled}` : `🔴 ${client.locale.status.disabled}`;
46-
4736
// Shows a text explaining the module
4837
console.log(`💬 ${locale.headerMessage}.\n`);
4938

@@ -247,7 +236,7 @@ export default async (beforeError) => {
247236
const isEnabled = commandsConfig.chatCommands[cmdName].enabled;
248237

249238
// Sets the status text according to the enabled status
250-
const statusText = isEnabled ? `🟢 ${client.locale.status.enabled}` : `🔴 ${client.locale.status.disabled}`;
239+
const statusText = getStatusText(isEnabled);
251240

252241
// Gets the localized name of the command, or defaults to the key name
253242
const locName = client.locale.handlers.commands.chatCommands[cmdName]?.appData?.name || cmdName;

‎lib/menus/configure/greetings.js‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Imports the necessary libraries
22
import { select, input } from '@inquirer/prompts';
3-
import { splashLogo } from 'helpbot/loaders';
3+
import { clearConsole, wait, getStatusText } from '../shared/utils.js';
44

55
// Stores the name of the menu and its parent menu
66
const parentMenuName = new URL(import.meta.url).pathname.split('/').slice(-2, -1)[0];
@@ -14,18 +14,6 @@ export default async (beforeError) => {
1414
// Stores the translations of the bot
1515
const locale = client.locale.lib.menus[parentMenuName].submenus[menuName];
1616

17-
// Function to clear the console
18-
async function clearConsole() {
19-
// Cleans the console and shows the logo
20-
process.stdout.write('\u001b[2J\u001b[0;0H');
21-
await splashLogo(client.locale.lib.loaders.splashLogo);
22-
};
23-
24-
// Function to wait some seconds, so the user can read a message
25-
async function wait(seconds) {
26-
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
27-
};
28-
2917
// Function to get the text that shows a role (its name, its ID if it could not be found, or none)
3018
async function getRoleText(roleId) {
3119

@@ -52,11 +40,6 @@ export default async (beforeError) => {
5240
return channel ? `#${channel.name}` : `ID: ${channelId}`;
5341
};
5442

55-
// Function to get the text that shows a status
56-
function getStatusText(enabled) {
57-
return enabled ? `🟢 ${client.locale.status.enabled}` : `🔴 ${client.locale.status.disabled}`;
58-
};
59-
6043
// Function to draw the menu
6144
async function drawMenu() {
6245

‎lib/menus/configure/mainConfiguration/botManagers.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Imports the necessary libraries
22
import { select, input } from '@inquirer/prompts';
3-
import { getLocale, clearConsole, drawMenu } from './utils.js';
3+
import { getLocale, clearConsole, wait, drawMenu } from './utils.js';
44

55
// Submenu to manage bot managers
66
async function botManagersMenu() {
@@ -156,7 +156,7 @@ async function botManagersMenu() {
156156
console.log(`\n❌ ${currentLocale.prompts.noMembersFound}.`);
157157

158158
// Waits 2 seconds
159-
await new Promise(resolve => setTimeout(resolve, 2000));
159+
await wait(2);
160160

161161
// Runs bot managers menu again
162162
await botManagersMenu();
@@ -230,7 +230,7 @@ async function botManagersMenu() {
230230
console.log(`\n❌ ${currentLocale.prompts.noRolesFound}.`);
231231

232232
// Waits 2 seconds
233-
await new Promise(resolve => setTimeout(resolve, 2000));
233+
await wait(2);
234234

235235
// Runs bot managers menu again
236236
await botManagersMenu();
@@ -311,7 +311,7 @@ async function botManagersMenu() {
311311
console.log(`\n❌ ${currentLocale.prompts.noUsersOrRolesFound}.`);
312312

313313
// Waits 2 seconds
314-
await new Promise(resolve => setTimeout(resolve, 2000));
314+
await wait(2);
315315

316316
// Runs bot managers menu again
317317
await botManagersMenu();
@@ -347,7 +347,7 @@ async function botManagersMenu() {
347347
};
348348

349349
// Waits 2 seconds
350-
await new Promise(resolve => setTimeout(resolve, 2000));
350+
await wait(2);
351351

352352
// Runs bot managers menu again
353353
await botManagersMenu();
@@ -365,7 +365,7 @@ async function botManagersMenu() {
365365
console.log(`\n❌ ${currentLocale.prompts.noManagersConfigured}.`);
366366

367367
// Waits 2 seconds
368-
await new Promise(resolve => setTimeout(resolve, 2000));
368+
await wait(2);
369369

370370
// Runs bot managers menu again
371371
await botManagersMenu();
@@ -446,7 +446,7 @@ async function botManagersMenu() {
446446
console.log(`\n🟢 ${currentLocale.prompts.managerRemoved}`);
447447

448448
// Waits 2 seconds
449-
await new Promise(resolve => setTimeout(resolve, 2000));
449+
await wait(2);
450450
};
451451

452452
// Runs bot managers menu again

‎lib/menus/configure/mainConfiguration/colorPalette.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Imports the necessary libraries
22
import { select, input } from '@inquirer/prompts';
3-
import { getLocale, clearConsole, drawMenu } from './utils.js';
3+
import { getLocale, clearConsole, wait, drawMenu } from './utils.js';
44

55
// Submenu to configure color palette
66
async function colorPaletteMenu() {
@@ -107,7 +107,7 @@ async function colorPaletteMenu() {
107107
console.log(`\n🟢 ${currentLocale.prompts.colorSuccess}`);
108108

109109
// Waits 2 seconds
110-
await new Promise(resolve => setTimeout(resolve, 2000));
110+
await wait(2);
111111

112112
// Runs color palette menu again
113113
await colorPaletteMenu();

‎lib/menus/configure/mainConfiguration/commandNamesLanguage.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Imports the necessary libraries
22
import { select } from '@inquirer/prompts';
33
import { loadCommands } from 'helpbot/loaders';
4-
import { getLocale, clearConsole, drawMenu, getLanguageChoices } from './utils.js';
4+
import { getLocale, clearConsole, wait, drawMenu, getLanguageChoices } from './utils.js';
55

66
// Submenu to select the language of the command names
77
async function commandNamesLanguageMenu() {
@@ -37,7 +37,7 @@ async function commandNamesLanguageMenu() {
3737
console.log(`\n🟢 ${currentLocale.prompts.commandNamesLanguageSuccess}`);
3838

3939
// Waits 3 seconds
40-
await new Promise(resolve => setTimeout(resolve, 3000));
40+
await wait(3);
4141

4242
// Returns to the main configuration menu
4343
await drawMenu();

‎lib/menus/configure/mainConfiguration/ignoredCommands.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { select } from '@inquirer/prompts';
33
import { loadCommands } from 'helpbot/loaders';
44
import fs from 'fs';
5-
import { getLocale, clearConsole, drawMenu } from './utils.js';
5+
import { getLocale, clearConsole, wait, drawMenu } from './utils.js';
66

77
// Submenu to configure ignored commands
88
async function ignoredCommandsMenu() {
@@ -100,7 +100,7 @@ async function ignoredCommandsMenu() {
100100
console.log(`\n🟢 ${currentLocale.prompts.ignoredSuccess}`);
101101

102102
// Waits 2 seconds
103-
await new Promise(resolve => setTimeout(resolve, 2000));
103+
await wait(2);
104104
};
105105

106106
// Runs ignored commands menu again

‎lib/menus/configure/mainConfiguration/language.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { select } from '@inquirer/prompts';
33
import { loadLocales, loadCommands } from 'helpbot/loaders';
44
import fs from 'fs';
5-
import { getLocale, clearConsole, drawMenu, getLanguageChoices } from './utils.js';
5+
import { getLocale, clearConsole, wait, drawMenu, getLanguageChoices } from './utils.js';
66

77
// Submenu to select the bot language
88
async function selectLanguageMenu() {
@@ -23,7 +23,7 @@ async function selectLanguageMenu() {
2323
console.log(`🔒 ${await client.functions.utils.parseLocale(currentLocale.prompts.languageSetByEnvironment, { locale: process.env.LOCALE })}.`);
2424

2525
// Waits 4 seconds
26-
await new Promise(resolve => setTimeout(resolve, 4000));
26+
await wait(4);
2727

2828
// Returns to the main configuration menu
2929
return await drawMenu();
@@ -56,7 +56,7 @@ async function selectLanguageMenu() {
5656
console.log(`\n🟢 ${getLocale().prompts.languageSuccess}.`);
5757

5858
// Waits 3 seconds
59-
await new Promise(resolve => setTimeout(resolve, 3000));
59+
await wait(3);
6060

6161
// Returns to the main configuration menu
6262
await drawMenu();

‎lib/menus/configure/mainConfiguration/pingThreshold.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Imports the necessary libraries
22
import { input } from '@inquirer/prompts';
3-
import { getLocale, clearConsole, drawMenu } from './utils.js';
3+
import { getLocale, clearConsole, wait, drawMenu } from './utils.js';
44

55
// Submenu to set the ping threshold
66
async function setPingThresholdMenu() {
@@ -55,7 +55,7 @@ async function setPingThresholdMenu() {
5555
console.log(`\n🟢 ${getLocale().prompts.pingSuccess.replace('{{threshold}}', parsedVal)}.`);
5656

5757
// Waits 2 seconds
58-
await new Promise(resolve => setTimeout(resolve, 2000));
58+
await wait(2);
5959

6060
// Returns to main configuration menu
6161
await drawMenu();

0 commit comments

Comments
 (0)