From fdd1943f4217e77a67c580fc1bedd56da318a320 Mon Sep 17 00:00:00 2001 From: Damien <48047820+JustDams@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:37:54 +0100 Subject: [PATCH] refactor/help usage (#285) * fix: laststats steam_params * feat: faceit urls * refactor: help usage --- commands/compare.js | 3 ++- commands/dailystats.js | 1 + commands/find.js | 21 +++++++++++++-------- commands/help.js | 10 ++++++---- commands/last.js | 3 ++- commands/laststats.js | 3 ++- commands/link.js | 7 ++++--- commands/map.js | 3 ++- commands/team.js | 9 +++++++-- commands/weekstats.js | 1 + commands/yearstats.js | 1 + functions/commands.js | 13 ++++++++----- functions/faceit.js | 1 + templates/options.js | 10 ++++++---- 14 files changed, 56 insertions(+), 30 deletions(-) diff --git a/commands/compare.js b/commands/compare.js index b5aef784..7f4af9b5 100644 --- a/commands/compare.js +++ b/commands/compare.js @@ -292,7 +292,8 @@ module.exports = { choices: getMapChoice() },], description: 'Compare both user stats.', - usage: 'match_number: number, default 20 AND first_user_steam:steam parameter OR first_user_faceit:faceit nickname OR @user AND second_user_steam:steam parameter OR second_user_faceit:faceit nickname OR @user, map: map name', + usage: ' { } [ ] ', + example: 'match_number: 100 first_user_steam: justdams second_user_steam: sheraw map: Vertigo', type: 'stats', async execute(interaction) { const player1 = (await getUsers(interaction, 1, 'first_user_steam', 'first_user_faceit'))?.at(0)?.param diff --git a/commands/dailystats.js b/commands/dailystats.js index 512bcd3a..61e69e60 100644 --- a/commands/dailystats.js +++ b/commands/dailystats.js @@ -65,6 +65,7 @@ module.exports = { options: Options.stats, description: 'Displays the stats of the choosen day. With elo graph of the day.', usage: Options.usage, + example: 'steam_parameters: justdams', type: 'stats', async execute(interaction) { return getCardsConditions(interaction, sendCardWithInfo) diff --git a/commands/find.js b/commands/find.js index 000a2338..5e490f1c 100644 --- a/commands/find.js +++ b/commands/find.js @@ -1,6 +1,6 @@ const { ApplicationCommandOptionType } = require('discord.js') const Options = require('../templates/options') -const { getUsers } = require('../functions/commands') +const { getUsers, getInteractionOption } = require('../functions/commands') const { sendCardWithInfo } = require('./last') const { getMapChoice } = require('../functions/map') @@ -8,7 +8,7 @@ const getOptions = () => { const options = [...Options.stats] options.unshift({ name: 'player_aimed', - description: 'steam_params / faceit_params / @user / empty if linked.', + description: 'steam_params / faceit_params / @user / empty if linked. History of the player you want to search in.', required: false, type: ApplicationCommandOptionType.String, slash: true @@ -39,19 +39,24 @@ const getOptions = () => { module.exports = { name: 'find', options: getOptions(), - description: 'Find the games that includes the player requested (up to 5), last 1000 games.', - usage: `player_aimed:the history in which one you are searching AND ${Options.usage} AND map:the map you want to search AND excluded_steam_parameters:the steam parameters you want to exclude AND excluded_faceit_parameters:the faceit parameters you want to exclude`, + description: 'Find the games that includes the player requested (up to 5)', + usage: '{player_aimed} [ ] ', + example: 'player_aimed: justdams steam_parameters: weder77 faceit_parameters: sheraw excluded_faceit_parameters: KanzakiR3D map: Vertigo', type: 'stats', async execute(interaction) { - const playerAimed = (await getUsers(interaction, 1, 'player_aimed', 'player_aimed', false))[0].param + const playerAimed = (await getUsers(interaction, 2, 'player_aimed', 'player_aimed', false)) + .find(e => e.param.match(/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/i)) + ?.param + const users = (await getUsers(interaction, 5)).map(p => p.param).filter((e, i, a) => a.indexOf(e) === i) let excludedUsers = (await getUsers(interaction, 10, 'excluded_steam_parameters', 'excluded_faceit_parameters', false)) .map(p => p.param).filter((e, i, a) => a.indexOf(e) === i) - excludedUsers = excludedUsers.filter(e => e.normalize() !== playerAimed.normalize()) + const excludedSteam = getInteractionOption(interaction, 'excluded_steam_parameters') + const excludedFaceit = getInteractionOption(interaction, 'excluded_faceit_parameters') - if (excludedUsers.includes(playerAimed)) throw 'You can\'t exclude a player you are searching for.' - if (excludedUsers.some(e => users.includes(e))) throw 'You can\'t exclude a player you are searching for.' + if (!excludedSteam && !excludedFaceit) excludedUsers = excludedUsers.filter(e => e.normalize() !== playerAimed.normalize()) + if (excludedUsers.some(e => users.includes(e)) || excludedUsers.includes(playerAimed)) throw 'You can\'t exclude a player you are searching for.' return sendCardWithInfo(interaction, playerAimed, null, 0, users.filter(e => e.normalize() !== playerAimed.normalize()), null, excludedUsers) } diff --git a/commands/help.js b/commands/help.js index 2856c2cb..a837ac22 100644 --- a/commands/help.js +++ b/commands/help.js @@ -31,10 +31,12 @@ const getCommandsHelp = (commandName, card) => { command.options.forEach(o => { if (o.description) optionsDesc += `\`${o.name}\`: ${o.description}\n` }) - card.setDescription(`Information about the ${command.name} command`) + card.setDescription(`Information about the ${command.name} command \n \`<>\`: optional, \`[]\`: required, \`{}\`: required if not linked`) .addFields({ name: 'Description', value: command.description }, { name: 'Options', value: optionsDesc.length > 0 ? optionsDesc : 'This command does not require any options' }, - { name: 'Usage', value: `/${command.name} ${command.usage}` }) + { name: 'Usage', value: `\`/${command.name} ${command.usage}\`` }) + + if (command?.example) card.addFields({ name: 'Example', value: `\`/${command.name} ${command.example}\`` }) return { embeds: [card] } } @@ -56,7 +58,7 @@ module.exports = { } ], description: 'Display the command list.', - usage: 'command name', + usage: '', type: 'system', async execute(interaction) { const command = getInteractionOption(interaction, 'command')?.trim().split(' ')[0] @@ -64,7 +66,7 @@ module.exports = { const helpCard = new Discord.EmbedBuilder() .setColor(color.primary) .setTitle('Commands') - .setDescription('`/help {command}` for more info on a specific command') + .setDescription('`/help ` for more info on a specific command') .setFooter({ text: `${name} Help` }) if (command) return getCommandsHelp(command, helpCard) diff --git a/commands/last.js b/commands/last.js index aa37025f..8ef51364 100644 --- a/commands/last.js +++ b/commands/last.js @@ -274,7 +274,8 @@ module.exports = { name: 'last', options: getOptions(), description: 'Get the stats of last game.', - usage: `${Options.usage} AND map name`, + usage: `${Options.usage} `, + example: 'steam_parameters: justdams', type: 'stats', async execute(interaction) { return getCardsConditions(interaction, sendCardWithInfo) diff --git a/commands/laststats.js b/commands/laststats.js index c31b2b5f..2cc44daa 100644 --- a/commands/laststats.js +++ b/commands/laststats.js @@ -62,7 +62,8 @@ module.exports = { name: 'laststats', options: getOptions(), description: 'Displays the stats of the x last match. With elo graph of the x last match.', - usage: `match_number:number of matches to display AND ${Options.usage}`, + usage: `${Options.usage} ${Options.dateRangeUsage}`, + example: 'steam_parameters: justdams match_number: 1000 from_date: 01/01/2022 to_date: 01/01/2023', type: 'stats', async execute(interaction) { return getCardsConditions(interaction, sendCardWithInfo) diff --git a/commands/link.js b/commands/link.js index b0a0d38b..0ee8e9db 100644 --- a/commands/link.js +++ b/commands/link.js @@ -61,7 +61,7 @@ module.exports = { slash: true }, { - name: 'faceit_parameters', + name: 'faceit_parameter', description: 'faceit nickname / @user', required: false, type: Discord.ApplicationCommandOptionType.String, @@ -83,9 +83,10 @@ module.exports = { } ], description: 'Link a steam profile to the discord user, to get your stats directly (no parameters needed).', - usage: 'steam_parameter:steam param or @user or CSGO status (max 1 user) OR faceit_parameters:faceit nickname (max 1) AND discord_user: @user', + usage: '[ ] ', + example: 'steam_parameter: justdams', type: 'utility', async execute(interaction) { - return getCardsConditions(interaction, sendCardWithInfo, 1, 'steam_parameter') + return getCardsConditions(interaction, sendCardWithInfo, 1, 'steam_parameter', 'faceit_parameter') } } \ No newline at end of file diff --git a/commands/map.js b/commands/map.js index 26bdb765..609695a4 100644 --- a/commands/map.js +++ b/commands/map.js @@ -68,7 +68,8 @@ module.exports = { name: 'map', options: getOptions(), description: 'Displays the stats of the choosen map.', - usage: `map:choose a map name AND ${Options.usage}`, + usage: `${Options.usage} `, + example: 'steam_parameters: justdams map: Vertigo', type: 'stats', async execute(interaction) { return getCardsConditions(interaction, sendCardWithInfo) diff --git a/commands/team.js b/commands/team.js index 242b0717..4cce45ad 100644 --- a/commands/team.js +++ b/commands/team.js @@ -1,7 +1,6 @@ const Discord = require('discord.js') const Team = require('../database/team') const UserTeam = require('../database/userTeam') -const Steam = require('../functions/steam') const Player = require('../functions/player') const User = require('../database/user') const errorCard = require('../templates/errorCard') @@ -212,7 +211,13 @@ module.exports = { } ], description: 'Create a team and link up to 5 users to it (limited to 1 team by discord account).', - usage: `\n- \`${CREATE}\` [team name]\n- \`${DELETE}\`\n- \`${UPDATE}\` [access] {name}\n- \`${INFO}\`\n- \`${ADD_USER}\` [steamID / steam custom ID / url of one steam profile / @user / CSGO status OR faceit nicknames]\n- \`${REMOVE_USER}\` [steamID / steam custom ID / url of one steam profile / @user / CSGO status OR faceit nicknames]`, + usage: ` + - ${CREATE} [team name] + - ${DELETE} + - ${UPDATE} [access] + - ${INFO} + - ${ADD_USER} [ ] + - ${REMOVE_USER} [ ]`, type: 'utility', async execute(interaction) { const user = interaction.user.id diff --git a/commands/weekstats.js b/commands/weekstats.js index 6b79a6f8..b2f50aeb 100644 --- a/commands/weekstats.js +++ b/commands/weekstats.js @@ -67,6 +67,7 @@ module.exports = { options: Options.stats, description: 'Displays the stats of the choosen week. With elo graph of the week.', usage: Options.usage, + example: 'steam_parameters: justdams', type: 'stats', async execute(interaction) { return getCardsConditions(interaction, sendCardWithInfo) diff --git a/commands/yearstats.js b/commands/yearstats.js index efc2c8f9..2854bfeb 100644 --- a/commands/yearstats.js +++ b/commands/yearstats.js @@ -70,6 +70,7 @@ module.exports = { options: Options.stats, description: 'Displays the stats of the choosen year. With elo graph of the year.', usage: Options.usage, + example: 'steam_parameters: justdams', type: 'stats', async execute(interaction) { return getCardsConditions(interaction, sendCardWithInfo) diff --git a/functions/commands.js b/functions/commands.js index b0d5adb2..7c4a9592 100644 --- a/functions/commands.js +++ b/functions/commands.js @@ -83,11 +83,14 @@ const getUsers = async ( } if (faceitParameters) { await Promise.all(faceitParameters - .map(async nickname => await Player.getDatasFromNickname(nickname) - .catch(async () => { - const player = await Player.searchPlayer(nickname).catch(e => e) - return player.items?.at(0) || nickname - })) + .map(async nickname => { + nickname = nickname.split('/').filter(e => e).pop() + return await Player.getDatasFromNickname(nickname) + .catch(async () => { + const player = await Player.searchPlayer(nickname).catch(e => e) + return player.items?.at(0) || nickname + }) + }) ).then(params => { parameters.push(...params.map(e => { return { diff --git a/functions/faceit.js b/functions/faceit.js index 70898b52..f51d53c8 100644 --- a/functions/faceit.js +++ b/functions/faceit.js @@ -12,6 +12,7 @@ const fetchData = async (url, error) => axios.get(url, { .then(res => res.data) .catch(e => { console.error(e.response.status, e.response.statusText, url) + if (e.response.status === 500) throw 'Faceit: internal server error.' throw error }) diff --git a/templates/options.js b/templates/options.js index 9b65937d..c53f491e 100644 --- a/templates/options.js +++ b/templates/options.js @@ -3,7 +3,7 @@ const { ApplicationCommandOptionType } = require('discord.js') const stats = [ { name: 'steam_parameters', - description: 'steamIDs / steam custom IDs / url of one or more steam profiles / @users / CSGO status.', + description: 'steamIDs / steam custom IDs / steam profile urls / @users / CSGO status.', required: false, type: ApplicationCommandOptionType.String, slash: true @@ -17,7 +17,7 @@ const stats = [ }, { name: 'faceit_parameters', - description: 'faceit nicknames / @users', + description: 'faceit nicknames / faceit urls / @users', required: false, type: ApplicationCommandOptionType.String, slash: true @@ -41,10 +41,12 @@ const dateRange = [ } ] -const usage = 'steam_parameters:multiple steam params and @user OR CSGO status AND team:team slug (max 1) AND faceit_parameters:multiple faceit nicknames and @user' +const usage = '{ }' +const dateRangeUsage = ' ' module.exports = { stats, usage, - dateRange + dateRange, + dateRangeUsage } \ No newline at end of file