|
2 | 2 | import inquirer from 'inquirer'; |
3 | 3 | import { splashLogo } from 'helpbot/loaders'; |
4 | 4 |
|
| 5 | +// Stores the name of the menu |
| 6 | +const menuName = new URL(import.meta.url).pathname.split('/').pop().replace('.js', ''); |
| 7 | + |
5 | 8 | // Stores the translations of the bot |
6 | | -const locale = client.locale.lib.menus.baseGuildSelection; |
| 9 | +const locale = client.locale.lib.menus[menuName]; |
7 | 10 |
|
8 | 11 | // Exports a default function |
9 | | -export default async (elegibleGuilds) => { |
10 | | - |
11 | | - // Load the Id of the base guild |
12 | | - const baseGuildId = await client.functions.db.getConfig('system.baseGuildId'); |
| 12 | +export default async (elegibleGuilds, beforeError) => { |
13 | 13 |
|
14 | | - // Stores the Id of the base guild |
15 | | - let newBaseGuildId = null; |
| 14 | + try { |
| 15 | + |
| 16 | + // Load the Id of the base guild |
| 17 | + const baseGuildId = await client.functions.db.getConfig('system.baseGuildId'); |
16 | 18 |
|
17 | | - // Function to ask for the new base guild |
18 | | - async function promptBaseGuildSelection() { |
| 19 | + // Stores the Id of the base guild |
| 20 | + let newBaseGuildId = null; |
19 | 21 |
|
20 | | - // Cleans the console and shows the logo |
21 | | - process.stdout.write('\u001b[2J\u001b[0;0H'); |
22 | | - await splashLogo(client.locale.lib.loaders.splashLogo); |
| 22 | + // Function to ask for the new base guild |
| 23 | + async function promptBaseGuildSelection() { |
23 | 24 |
|
24 | | - // Shows the help to choose a new base guild |
25 | | - console.log(`🧹 ${locale.promptBaseGuildSelectionHeader1}.`); |
26 | | - console.log(`👋 ${locale.promptBaseGuildSelectionHeader2}.\n`); |
| 25 | + // Cleans the console and shows the logo |
| 26 | + process.stdout.write('\u001b[2J\u001b[0;0H'); |
| 27 | + await splashLogo(client.locale.lib.loaders.splashLogo); |
| 28 | + |
| 29 | + // If an error ocurred on the previous run |
| 30 | + if (beforeError) { |
27 | 31 |
|
28 | | - // Generates the question of the base guild selector |
29 | | - const questions = [ |
30 | | - { |
31 | | - type: 'list', |
32 | | - name: 'baseGuildId', |
33 | | - message: `${locale.selectANewBaseGuild}:`, |
34 | | - choices: elegibleGuilds.map(guild => ({ name: `${guild.name} (${guild.id})${guild.id === baseGuildId ? ` - ${locale.alreadyConfiguredGuild}` : ''}`, value: guild })), |
35 | | - } |
36 | | - ]; |
| 32 | + // Shows the error message |
| 33 | + console.log(`❌ ${locale.errorMessage}.\n`); |
| 34 | + if (beforeError && process.env.NODE_ENV !== 'production') logger.error(`${beforeError.stack}\n`); |
37 | 35 |
|
38 | | - // Asks for the base guild |
39 | | - const { baseGuildId: newBaseGuild } = await inquirer.prompt(questions); |
| 36 | + // Resets the beforeError variable |
| 37 | + beforeError = false; |
| 38 | + }; |
40 | 39 |
|
41 | | - // If the selected base guild is different from the currently configured |
42 | | - if (baseGuildId && newBaseGuild.id !== baseGuildId) { |
| 40 | + // Shows the help to choose a new base guild |
| 41 | + console.log(`🧹 ${locale.promptBaseGuildSelectionHeader1}.`); |
| 42 | + console.log(`👋 ${locale.promptBaseGuildSelectionHeader2}.\n`); |
43 | 43 |
|
44 | | - // Asks the user if is sure to change the base guild |
45 | | - const confirmation = await inquirer.prompt([ |
| 44 | + // Generates the question of the base guild selector |
| 45 | + const questions = [ |
46 | 46 | { |
47 | | - type: 'input', |
48 | | - name: 'isSure', |
49 | | - message: `${locale.isSureToReplaceBaseGuild} (${locale.affirmativeAnswer}/${locale.negativeAnswer}):` |
| 47 | + type: 'list', |
| 48 | + name: 'baseGuildId', |
| 49 | + message: `${locale.selectANewBaseGuild}:`, |
| 50 | + choices: elegibleGuilds.map(guild => ({ name: `${guild.name} (${guild.id})${guild.id === baseGuildId ? ` - ${locale.alreadyConfiguredGuild}` : ''}`, value: guild })), |
50 | 51 | } |
51 | | - ]); |
| 52 | + ]; |
| 53 | + |
| 54 | + // Asks for the base guild |
| 55 | + const { baseGuildId: newBaseGuild } = await inquirer.prompt(questions); |
| 56 | + |
| 57 | + // If the selected base guild is different from the currently configured |
| 58 | + if (baseGuildId && newBaseGuild.id !== baseGuildId) { |
| 59 | + |
| 60 | + // Asks the user if is sure to change the base guild |
| 61 | + const confirmation = await inquirer.prompt([ |
| 62 | + { |
| 63 | + type: 'input', |
| 64 | + name: 'isSure', |
| 65 | + message: `${locale.isSureToReplaceBaseGuild} (${locale.affirmativeAnswer}/${locale.negativeAnswer}):` |
| 66 | + } |
| 67 | + ]); |
| 68 | + |
| 69 | + // Returns to the selector if the user is not sure |
| 70 | + if (locale.affirmativeAnswer !== confirmation.isSure.toLowerCase()) return promptBaseGuildSelection(); |
| 71 | + }; |
52 | 72 |
|
53 | | - // Returns to the selector if the user is not sure |
54 | | - if (locale.affirmativeAnswer !== confirmation.isSure.toLowerCase()) return promptBaseGuildSelection(); |
| 73 | + // Stores the new base guild Id |
| 74 | + newBaseGuildId = newBaseGuild.id; |
| 75 | + |
| 76 | + // Cleans the console and shows the logo |
| 77 | + process.stdout.write('\u001b[2J\u001b[0;0H'); |
| 78 | + await splashLogo(client.locale.lib.loaders.splashLogo); |
55 | 79 | }; |
56 | 80 |
|
57 | | - // Stores the new base guild Id |
58 | | - newBaseGuildId = newBaseGuild.id; |
| 81 | + // Runs the base guild selector |
| 82 | + await promptBaseGuildSelection(); |
59 | 83 |
|
60 | | - // Cleans the console and shows the logo |
61 | | - process.stdout.write('\u001b[2J\u001b[0;0H'); |
62 | | - await splashLogo(client.locale.lib.loaders.splashLogo); |
63 | | - }; |
| 84 | + // For each guild in cache |
| 85 | + for (let guild of elegibleGuilds.values()) { |
64 | 86 |
|
65 | | - // Runs the base guild selector |
66 | | - await promptBaseGuildSelection(); |
| 87 | + // If the guild is the new base guild, omits |
| 88 | + if (guild.id === newBaseGuildId) continue; |
67 | 89 |
|
68 | | - // For each guild in cache |
69 | | - for (let guild of elegibleGuilds.values()) { |
| 90 | + // Shows through the console the guild to leave |
| 91 | + logger.debug(`${await client.functions.utils.parseLocale(locale.leavingGuild, { guild: `"${guild.name}" (${guild.id})` })} ...`); |
70 | 92 |
|
71 | | - // If the guild is the new base guild, omits |
72 | | - if (guild.id === newBaseGuildId) continue; |
| 93 | + // Leaves the guild |
| 94 | + await guild.leave(); |
73 | 95 |
|
74 | | - // Shows through the console the guild to leave |
75 | | - logger.debug(`${await client.functions.utils.parseLocale(locale.leavingGuild, { guild: `"${guild.name}" (${guild.id})` })} ...`); |
| 96 | + // Deletes the guild from the cachedGuilds variable |
| 97 | + elegibleGuilds.delete(guild.id); |
| 98 | + }; |
76 | 99 |
|
77 | | - // Leaves the guild |
78 | | - await guild.leave(); |
| 100 | + // Returns the new eligible guilds object |
| 101 | + return elegibleGuilds; |
79 | 102 |
|
80 | | - // Deletes the guild from the cachedGuilds variable |
81 | | - elegibleGuilds.delete(guild.id); |
82 | | - }; |
| 103 | + } catch (error) { |
83 | 104 |
|
84 | | - // Returns the new eligible guilds object |
85 | | - return elegibleGuilds |
| 105 | + // Runs this menu again, passing the error |
| 106 | + await client.functions.menus[menuName](elegibleGuilds, error); |
| 107 | + }; |
86 | 108 | }; |
0 commit comments