Skip to content

Commit

Permalink
fix/command find (#427)
Browse files Browse the repository at this point in the history
* move: script

* feat: new script

* feat: logs on role update

* feat: script cli

* update: logChannel

* update: node packages

* fix: find with a specific player_aimed

* fix: error displayed when player not linked

* update: search current user option

* fix: roles attribution when user not found

* remove: extra console log
  • Loading branch information
JustDams authored Sep 7, 2024
1 parent d481c86 commit 38dbce2
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 272 deletions.
11 changes: 9 additions & 2 deletions commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,22 @@ module.exports = {
if (!playerParam) return errorCard('error.command.faceitDatasNotFound', interaction.locale)

const playerAimed = playerParam.playerDatas.player_id
const searchForCurrentUser = currentPlayer ? currentPlayer.faceitId !== playerAimed : false
let teamIncluded, faceitIncluded, steamIncluded

teamIncluded = await Promise.all((await getUsers(interaction, 5, null, null, true, false))
.map(async e => e.param))
faceitIncluded = (await getUsers(interaction, 5 - teamIncluded.length, null, 'faceit_parameters', false, false))
faceitIncluded = (await getUsers(interaction, 5 - teamIncluded.length, null, 'faceit_parameters', false, false).catch(() => []))
.map(e => e.param)
steamIncluded = (await getUsers(interaction, 5 - faceitIncluded.length, 'steam_parameters', null, false, false))
steamIncluded = (await getUsers(interaction, 5 - faceitIncluded.length, 'steam_parameters', null, false, false).catch(() => []))
.map(e => e.param)

// In case we don't have any parameters, we add the current user to the list if he's not the player aimed
if (!faceitIncluded.length && !steamIncluded.length && searchForCurrentUser) {
faceitIncluded = (await getUsers(interaction, 5, null, 'faceit_parameters', false, true)).map(e => e.param)
steamIncluded = (await getUsers(interaction, 5 - faceitIncluded.length, 'steam_parameters', null, true)).map(e => e.param)
}

if (!teamIncluded.length && !faceitIncluded.length && !steamIncluded.length)
throw getTranslation('error.command.atLeastOneParameter', interaction.locale, {
parameters: 'steam_parameters, faceit_parameters, team',
Expand Down
2 changes: 1 addition & 1 deletion commands/unlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { getInteractionOption } = require('../functions/utility')
const sendCardWithInfo = async (interaction) => {
const discordId = interaction.user.id

if (await User.getVerified(discordId)) {
if ((await User.getVerified(discordId)).length) {
return errorCard(getTranslation('error.user.unlink.verified', interaction.locale))
}

Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"oauth2": "https://faceitfinder.app/api/auth/discord/login",
"unlinkVerified": "https://faceitfinder.app/profile/settings",
"logChannel": "1120786737636048976",
"logGuild": "882210656328228895",
"itemByPage": 25,
"color": {
"primary": "#14578c",
Expand Down
1 change: 0 additions & 1 deletion functions/apiHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const getStats = async ({
return axios.get(`${process.env.API_URL}/api/stats/${matchNumber}?faceit=${faceit}&steam=${steam}&id=${id}&checkElo=${checkElo}&map=${map}&startDate=${startDate}&endDate=${endDate}&game=${game}`)
.then(res => res.data)
.catch(e => {
console.log(e)
console.error(e.response.status, e.response.statusText, e.response.config.url)
throw e.response.data.error
})
Expand Down
40 changes: 35 additions & 5 deletions functions/roles.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { defaultGame } = require('../config.json')
const { defaultGame, logChannel, logGuild } = require('../config.json')
const GuildCustomRole = require('../database/guildCustomRole')
const User = require('../database/user')
const { getStats } = require('./apiHandler')

const REMOVE = 'REMOVE', ADD = 'ADD'

const getRoleIds = (guildRoles) => Object.keys(Object.entries(guildRoles)[2][1])
.filter(e => e.startsWith('level')).map(e => guildRoles[e])

Expand Down Expand Up @@ -38,12 +40,21 @@ const setupRoles = async (client, user, guildId, remove) => {
if (user.nickname) await member.edit({ nick: playerDatas.nickname }).catch(() => null)

roles.forEach(async (role) => {
const removeRole = remove || playerElo < role.eloMin || playerElo > role.eloMax
const removeRole = playerElo < role.eloMin || playerElo > role.eloMax
const roleId = role.roleId
const rolesFit = member.roles.resolve(roleId)

if (removeRole || !rolesFit) await member.roles.remove(roleId).catch((err) => handleRoleErrors(err, role))
if (!removeRole && !rolesFit) await member.roles.add(roleId).catch((err) => handleRoleErrors(err, role))
// Remove role if it doesn't fit the criteria and the role is assigned or if the remove flag is set
if ((removeRole && rolesFit) || remove)
await member.roles.remove(roleId)
.then(e => logRoleUpdate(client, member, role, guildDatas, playerElo, REMOVE))
.catch((err) => handleRoleErrors(err, role))

// Add role if it fits the criteria and the role isn't already assigned
if (!removeRole && !rolesFit)
await member.roles.add(roleId)
.then(e => logRoleUpdate(client, member, role, guildDatas, playerElo, ADD))
.catch((err) => handleRoleErrors(err, role))
})
})
}
Expand All @@ -54,7 +65,7 @@ const updateRoles = async (client, discordId, guildId, remove = false) => {
if (discordId) {
user = [await User.get(discordId)].flat()
if (user.length > 1) user = user.filter(e => e.guildId === guildId)
guildId = user.at(0).guildId
guildId = user.at(0)?.guildId
}

if (guildId) guilds = [guildId].flat()
Expand All @@ -69,6 +80,25 @@ const handleRoleErrors = (err, role) => {
if (err.status === '404') GuildCustomRole.remove(role.guildId, role.roleId)
}

const logRoleUpdate = (client, member, role, guildDatas, playerElo, action) => {
console.log(role)
client.guilds.fetch(logGuild)
.then(guild => guild.channels.fetch(logChannel))
.then(channel => channel.send({
content: `
\`\`\`js
Guild: ${guildDatas.name} (${guildDatas.id})
User: ${member.user.tag} (${member.user.id})
Date: ${new Date().toLocaleString('fr-FR', { timeZone: 'Europe/Paris', timeStyle: 'short', dateStyle: 'long' })}
Role: ${role.roleId} - Elo: ${role.eloMin} - ${role.eloMax}
Elo: ${playerElo}
Action: ${action}
\`\`\`
`,
}))
.catch(console.error)
}

module.exports = {
updateRoles,
getRoleIds
Expand Down
Loading

0 comments on commit 38dbce2

Please sign in to comment.