Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirabellier committed Mar 2, 2024
1 parent e7b5796 commit 7053e94
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 56 deletions.
3 changes: 2 additions & 1 deletion src/commands/rpg/equip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const schemas = require("../../database/schemas");
const Discord = require("discord.js");
const constants = require("../../utils/constants");
const { camelCaseToNormal } = require("../../utils/utils");
const characters = require("../../database/json/characters.json");

module.exports = {
name: "equip",
Expand All @@ -27,7 +28,7 @@ module.exports = {
name: message.author.username,
iconURL: message.author.displayAvatarURL(),
})
.setThumbnail(character.get("img"))
.setThumbnail(characters[character.get("img")].image)
.setColor("#0099ff")
.setTitle("Equipped Equipments")
.setDescription(
Expand Down
22 changes: 7 additions & 15 deletions src/commands/rpg/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,12 @@ module.exports = {
const name = args.join(" ");
const images = JSON.parse(userCharacter.get("images"));

function searchCharacterByName(name) {
const searchResults = [];
for (const key in characters) {
const character = characters[key];
if (character.name.toLowerCase() === name.toLowerCase()) {
searchResults.push(key);
}
}
return searchResults;
function searchCharacterByName(searchname) {
return Object.entries(characters)
.filter(([_, { name }]) =>
name.toLowerCase().includes(searchname.toLowerCase()),
)
.map(([id]) => id);
}

const results = searchCharacterByName(name);
Expand All @@ -70,12 +67,7 @@ module.exports = {
}

function findFirstCommonElement(array1, array2) {
for (let i = 0; i < array1.length; i++) {
if (array2.includes(array1[i])) {
return array1[i];
}
}
return null;
return array1.find((id) => array2.includes(id));
}

const result = findFirstCommonElement(results, images);
Expand Down
50 changes: 10 additions & 40 deletions src/commands/rpg/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,31 @@ module.exports = {
equipments.weapons.equipped
? equipments.weapons.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.shield.emoji} Shield:** ${
equipments.shields.equipped
? equipments.shields.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.helmet.emoji} Helmet:** ${
equipments.helmet.equipped
? equipments.helmet.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.armor.emoji} Armor:** ${
equipments.armor.equipped
? equipments.armor.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.gloves.emoji} Gloves:** ${
equipments.gloves.equipped
? equipments.gloves.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}`,
inline: true,
Expand Down Expand Up @@ -201,46 +186,31 @@ module.exports = {
equipments.weapons.equipped
? equipments.weapons.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.shield.emoji} Shield:** ${
equipments.shields.equipped
? equipments.shields.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.helmet.emoji} Helmet:** ${
equipments.helmet.equipped
? equipments.helmet.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.armor.emoji} Armor:** ${
equipments.armor.equipped
? equipments.armor.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}\n**• ${constants.assets.gloves.emoji} Gloves:** ${
equipments.gloves.equipped
? equipments.gloves.equipped
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ")
.replace(/\b\w/g, (e) => e.toUpperCase())
: "None"
}`,
inline: true,
Expand Down

0 comments on commit 7053e94

Please sign in to comment.