diff options
author | Minteck <contact@minteck.org> | 2022-03-14 21:43:52 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-03-14 21:43:52 +0100 |
commit | 2d0b698b3b4901ed7105882a2adc5439566c4c20 (patch) | |
tree | 08c34d5fddd95b7ff206d0eff9d46e537f4801aa | |
parent | 80baef5480d168210b3be920ec921d3d6d0b8234 (diff) | |
download | ponyfind-2d0b698b3b4901ed7105882a2adc5439566c4c20.tar.gz ponyfind-2d0b698b3b4901ed7105882a2adc5439566c4c20.tar.bz2 ponyfind-2d0b698b3b4901ed7105882a2adc5439566c4c20.zip |
Implements #18
-rw-r--r-- | .DS_Store | bin | 0 -> 8196 bytes | |||
-rwxr-xr-x | .idea/dictionaries/Minteck.xml | 2 | ||||
-rwxr-xr-x | commands/color.js | 170 | ||||
-rwxr-xr-x | config/version.txt | 2 | ||||
-rwxr-xr-x | handler/command.js | 5 | ||||
-rw-r--r-- | modules/colorfind.js | 441 | ||||
-rwxr-xr-x | modules/registers.js | 8 |
7 files changed, 626 insertions, 2 deletions
diff --git a/.DS_Store b/.DS_Store Binary files differnew file mode 100644 index 0000000..a68536f --- /dev/null +++ b/.DS_Store diff --git a/.idea/dictionaries/Minteck.xml b/.idea/dictionaries/Minteck.xml index 41f8aa8..c3fcce8 100755 --- a/.idea/dictionaries/Minteck.xml +++ b/.idea/dictionaries/Minteck.xml @@ -1,3 +1,3 @@ <component name="ProjectDictionaryState">
- <dictionary name="Minteck" />
+ <dictionary name="minteck" />
</component>
\ No newline at end of file diff --git a/commands/color.js b/commands/color.js new file mode 100755 index 0000000..cf9df1d --- /dev/null +++ b/commands/color.js @@ -0,0 +1,170 @@ +/*
+ * MIT License
+ *
+ * Copyright (c) 2022- Minteck
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ */
+
+const fs = require('fs');
+const {MessageActionRow, MessageButton, MessageSelectMenu, MessageEmbed} = require('discord.js');
+
+const admin = fs.readFileSync("./config/admin.txt").toString().trim();
+const list = Object.keys(JSON.parse(fs.readFileSync("./data/data.json").toString()));
+
+const getResult = require('../modules/colorfind');
+const getEmbed = require('../modules/embed');
+const getPixel = require("../modules/pixel");
+const getPublic = require('../modules/public');
+
+const official = fs.readFileSync("./config/official.txt").toString().replace(/\r\n/g, "\n").split("\n");
+const fpserver = fs.readFileSync("./config/fpserver.txt").toString().trim();
+
+module.exports = async (interaction) => {
+ query = interaction.options.getString('query').toLowerCase();
+ result = getResult(query, interaction.user.id, interaction.guild ? interaction.guild.id : 0);
+
+ if (result.results.length > 0 && getEmbed(result.results[0], interaction.user.id, interaction.guild ? interaction.guild.id : 0) !== false) {
+ if (statsQueries[result.results[0]] !== undefined) {
+ if (!statsQueries[result.results[0]].includes(query)) {
+ statsQueries[result.results[0]].push(query);
+ }
+ } else {
+ statsQueries[result.results[0]] = [ query ];
+ }
+ fs.writeFile("./stats/queries.json", JSON.stringify(statsQueries), () => {});
+ if (result.results[0].toLowerCase().trim() === query.toLowerCase().trim()) {
+ keys = [null, result.results[0]];
+ if (statsPonies[keys[1]] !== undefined) {
+ statsPonies[keys[1]]++;
+ } else {
+ statsPonies[keys[1]] = 1;
+ }
+ fs.writeFile("./stats/ponies.json", JSON.stringify(statsPonies), () => {});
+ if (interaction.guild) {
+ row = new MessageActionRow()
+ .addComponents(
+ getPublic(interaction, keys),
+ new MessageButton()
+ .setLabel(l("Read More", "Lire plus", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .setStyle("LINK")
+ .setURL("https://mlp.fandom.com/wiki/" + encodeURI(keys[1])),
+ new MessageButton()
+ .setCustomId("pony.pixel|" + keys[1])
+ .setLabel("Pixel Art")
+ .setDisabled(!fs.existsSync("./pixel/" + keys[1].toLowerCase()))
+ .setStyle("SECONDARY"),
+ new MessageButton()
+ .setCustomId("result.report|" + keys[1])
+ .setLabel(l("Report an issue", "Signaler un problème", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .setStyle("DANGER")
+ )
+ } else {
+ row = new MessageActionRow()
+ .addComponents(
+ new MessageButton()
+ .setLabel(l("Read More", "Lire plus", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .setStyle("LINK")
+ .setURL("https://mlp.fandom.com/wiki/" + encodeURI(keys[1])),
+ new MessageButton()
+ .setCustomId("pony.pixel|" + keys[1])
+ .setLabel("Pixel Art")
+ .setDisabled(!fs.existsSync("./pixel/" + keys[1].toLowerCase()))
+ .setStyle("SECONDARY"),
+ new MessageButton()
+ .setCustomId("result.report|" + keys[1])
+ .setLabel(l("Report an issue", "Signaler un problème", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .setStyle("DANGER")
+ )
+ }
+ console.log("Replying (embed)");
+ await interaction.reply({
+ ephemeral: interaction.guild !== null,
+ embeds: [
+ getEmbed(keys[1], interaction.user.id, interaction.guild ? interaction.guild.id : 0, false, interaction.user, interaction.member, interaction.guild)
+ ],
+ components: [
+ row
+ ]
+ });
+ } else {
+ console.log("Replying (buttons)");
+ await interaction.reply({
+ ephemeral: interaction.guild !== null,
+ embeds: [
+ new MessageEmbed()
+ .setColor('#d6dc28')
+ .setTitle(l("Results for", "Résultats pour", interaction.user.id, interaction.guild ? interaction.guild.id : 0) + " " + result.query)
+ .setDescription(l("Here are the 5 first results corresponding to your criteria.", "Voici les 5 premiers résultats correspondants à vos critères.", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .addField(l("Your conditions:", "Vos conditions :", interaction.user.id, interaction.guild ? interaction.guild.id : 0), result.full)
+ ],
+ components: [
+ new MessageActionRow()
+ .addComponents(
+ new MessageButton()
+ .setCustomId("pony.display|" + result.first[0])
+ .setLabel(result.first[0])
+ .setStyle(result.exact.includes(result.first[0]) ? "PRIMARY" : "SECONDARY"),
+ new MessageButton()
+ .setCustomId("pony.display|" + result.first[1])
+ .setLabel(result.first[1])
+ .setStyle(result.exact.includes(result.first[1]) ? "PRIMARY" : "SECONDARY"),
+ new MessageButton()
+ .setCustomId("pony.display|" + result.first[2])
+ .setLabel(result.first[2])
+ .setStyle(result.exact.includes(result.first[2]) ? "PRIMARY" : "SECONDARY"),
+ new MessageButton()
+ .setCustomId("pony.display|" + result.first[3])
+ .setLabel(result.first[3])
+ .setStyle(result.exact.includes(result.first[3]) ? "PRIMARY" : "SECONDARY"),
+ new MessageButton()
+ .setCustomId("pony.display|" + result.first[4])
+ .setLabel(result.first[4])
+ .setStyle(result.exact.includes(result.first[4]) ? "PRIMARY" : "SECONDARY"),
+ ),
+ ]
+ });
+ }
+ } else {
+ console.log("Replying (no results)");
+ await interaction.reply({
+ ephemeral: interaction.guild !== null,
+ embeds: [
+ new MessageEmbed()
+ .setColor('#dc2828')
+ .setTitle(l("Results for", "Résultats pour", interaction.user.id, interaction.guild ? interaction.guild.id : 0) + " " + result.query)
+ .setDescription(l("No results found. Please try with other criteria.", "Aucun résultat trouvé. Essayez avec d'autres critères.", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ ],
+ components: [
+ new MessageActionRow()
+ .addComponents(
+ new MessageButton()
+ .setCustomId("result.suggest|" + query)
+ .setLabel(l("Suggest a missing pony", "Proposer un poney manquant", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .setStyle("SECONDARY"),
+ new MessageButton()
+ .setCustomId("result.report|" + query)
+ .setLabel(l("Report an issue", "Signaler un problème", interaction.user.id, interaction.guild ? interaction.guild.id : 0))
+ .setStyle("DANGER")
+ )
+ ]
+ });
+ }
+}
\ No newline at end of file diff --git a/config/version.txt b/config/version.txt index 0bc39ca..2309de0 100755 --- a/config/version.txt +++ b/config/version.txt @@ -1 +1 @@ -2.2.63
\ No newline at end of file +2.3.65
\ No newline at end of file diff --git a/handler/command.js b/handler/command.js index 2be1c92..1179701 100755 --- a/handler/command.js +++ b/handler/command.js @@ -41,6 +41,7 @@ const commandInfo = require('../commands/info'); const commandEval = require('../commands/eval');
const commandConfig = require('../commands/config');
const commandPony = require('../commands/pony');
+const commandColor = require('../commands/color');
const commandEpisode = require('../commands/episode');
const commandHelp = require('../commands/help');
@@ -112,6 +113,10 @@ module.exports = async (interaction) => { await commandPony(interaction);
}
+ if (interaction.commandName === 'color') {
+ await commandColor(interaction);
+ }
+
if (interaction.commandName === 'episode') {
await commandEpisode(interaction);
}
diff --git a/modules/colorfind.js b/modules/colorfind.js new file mode 100644 index 0000000..8ffe808 --- /dev/null +++ b/modules/colorfind.js @@ -0,0 +1,441 @@ +/* + * MIT License + * + * Copyright (c) 2022- Minteck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +const Database = require('../data/data.json'); +let db = {}; + +for (let entry of Object.keys(Database)) { + search = {}; + + for (let e of Object.keys(Database[entry].search)) { + search[e] = Database[entry].search[e].filter(i => i !== null); + } + + db[entry] = search; +} + +module.exports = (query, uid, gid) => { + console.log("Running color search"); + matches = {}; + + keys_l1 = query.toLowerCase().split(" "); + keys_l2 = []; + keys_l3 = { + "coat": [], + "mane": [], + "eyes": [] + }; + + keys = { + "coat": [], + "mane": [], + "eyes": [] + }; + + for (let key of keys_l1) { + if (key.split(":").length > 1) keys_l2.push({ + key: key.split(":")[0].trim(), + value: key.split(":")[1].trim() + }); + } + + for (let item of keys_l2) { + let key = item.key; + let value = item.value; + + switch (key) { + case "coat": + case "co": + case "coa": + case "skin": + case "sk": + case "ski": + case "s": + case "peau": + case "pea": + case "pe": + case "manteau": + case "mante": + case "mantea": + case "mant": + case "fur": + case "fu": + case "fourrure": + case "fou": + case "four": + case "fourr": + case "fourru": + case "fourrur": + case "fo": + case "f": + keys_l3["coat"].push(value); + break; + + case "eyes": + case "ey": + case "eye": + case "e": + case "yeux": + case "ye": + case "yeu": + case "y": + case "oeil": + case "oe": + case "oei": + case "o": + case "pupil": + case "pupi": + case "pup": + case "pu": + case "pupille": + case "p": + keys_l3["eyes"].push(value); + break; + + case "m": + case "mane": + case "ma": + case "man": + case "manes": + case "crinière": + case "cri": + case "crin": + case "crini": + case "criniè": + case "crinie": + case "crinièr": + case "crinier": + case "cr": + case "criniere": + case "hair": + case "ha": + case "hai": + case "h": + case "hairs": + case "cheveux": + case "che": + case "chev": + case "cheve": + case "cheveu": + case "ch": + keys_l3["mane"].push(value); + break; + } + } + + for (let key of Object.keys(keys_l3)) { + let values = keys_l3[key]; + + for (let value of values) { + switch (value) { + case "blac": + case "black": + case "k": + case "n": + case "no": + case "noi": + case "noir": + case "d": + case "da": + case "dar": + case "dark": + if (keys[key] === null) keys[key] = []; + keys[key].push(0); + break + + case "blan": + case "blanc": + case "w": + case "wh": + case "whi": + case "whit": + case "white": + case "l": + case "li": + case "lig": + case "ligh": + case "light": + if (keys[key] === null) keys[key] = []; + keys[key].push(1); + break + + case "gra": + case "gray": + case "grey": + case "gri": + case "gris": + if (keys[key] === null) keys[key] = []; + keys[key].push(2); + break + + case "r": + case "ro": + case "rou": + case "roug": + case "rouge": + case "re": + case "red": + if (keys[key] === null) keys[key] = []; + keys[key].push(3); + break + + case "o": + case "or": + case "ora": + case "oran": + case "orang": + case "orange": + case "go": + case "gol": + case "gold": + if (keys[key] === null) keys[key] = []; + keys[key].push(4); + break + + case "y": + case "ye": + case "yel": + case "yell": + case "yello": + case "yellow": + case "j": + case "jau": + case "jaun": + case "jaune": + if (keys[key] === null) keys[key] = []; + keys[key].push(5); + break + + case "g": + case "gr": + case "gre": + case "gree": + case "green": + case "v": + case "ve": + case "ver": + case "vert": + if (keys[key] === null) keys[key] = []; + keys[key].push(6); + break + + case "b": + case "bl": + case "ble": + case "blu": + case "bleu": + case "blue": + case "s": + case "sk": + case "sky": + case "c": + case "ci": + case "cie": + case "ciel": + case "t": + case "tu": + case "tur": + case "turq": + case "turqu": + case "turquo": + case "turquoi": + case "turquois": + case "turquoise": + case "cy": + case "cya": + case "cyan": + if (keys[key] === null) keys[key] = []; + keys[key].push(7); + break + + case "vi": + case "vio": + case "viol": + case "viole": + case "violet": + case "p": + case "pu": + case "pur": + case "purp": + case "purpl": + case "purple": + case "m": + case "ma": + case "mag": + case "mage": + case "magen": + case "magent": + case "magenta": + if (keys[key] === null) keys[key] = []; + keys[key].push(8); + break + + case "ros": + case "rose": + case "pi": + case "pin": + case "pink": + if (keys[key] === null) keys[key] = []; + keys[key].push(9); + break + + case "br": + case "bro": + case "brow": + case "brown": + case "mar": + case "marr": + case "marro": + case "marron": + if (keys[key] === null) keys[key] = []; + keys[key].push(10); + break + } + } + } + + for (let criteria of Object.keys(keys)) { + let values = keys[criteria]; + + for (let pony of Object.keys(db)) { + let data = db[pony]; + + for (let value of values) { + if (data[criteria].includes(value)) { + if (typeof matches[pony] !== "number") matches[pony] = 0; + matches[pony]++; + } + } + } + } + + sorted = Object.keys(matches).sort(function(a,b){return matches[a]-matches[b]}).reverse(); + sortedMatches = {}; + sortedMatchesFirst = {}; + + index = 0; + for (let item of sorted) { + if (index < 30) sortedMatches[item] = matches[item]; + index++; + } + + index = 0; + for (let item of sorted) { + if (index < 5) sortedMatchesFirst[item] = matches[item]; + index++; + } + + queryItems = []; + + maneColor = [ l("any color", "de n'importe quelle couleur", uid, gid) ]; + coatColor = [ l("any color", "de n'importe quelle couleur", uid, gid) ]; + eyesColor = [ l("any color", "de n'importe quelle couleur", uid, gid) ]; + + if (keys["mane"].length > 0) maneColor = []; + if (keys["coat"].length > 0) coatColor = []; + if (keys["eyes"].length > 0) eyesColor = []; + + operations = 0; + for (let value of keys["mane"]) { + operations++; + switch (value) { + case 0: maneColor.push(l("black", "noire", uid, gid)); break; + case 1: maneColor.push(l("white", "blanche", uid, gid)); break; + case 2: maneColor.push(l("gray", "grise", uid, gid)); break; + case 3: maneColor.push(l("red", "rouge", uid, gid)); break; + case 4: maneColor.push(l("orange", "orange", uid, gid)); break; + case 5: maneColor.push(l("yellow", "jaune", uid, gid)); break; + case 6: maneColor.push(l("green", "verte", uid, gid)); break; + case 7: maneColor.push(l("blue", "bleue", uid, gid)); break; + case 8: maneColor.push(l("purple", "violette", uid, gid)); break; + case 9: maneColor.push(l("pink", "rose", uid, gid)); break; + case 10: maneColor.push(l("brown", "marron", uid, gid)); break; + } + } + + for (let value of keys["coat"]) { + operations++; + switch (value) { + case 0: coatColor.push(l("black", "noire", uid, gid)); break; + case 1: coatColor.push(l("white", "blanche", uid, gid)); break; + case 2: coatColor.push(l("gray", "grise", uid, gid)); break; + case 3: coatColor.push(l("red", "rouge", uid, gid)); break; + case 4: coatColor.push(l("orange", "orange", uid, gid)); break; + case 5: coatColor.push(l("yellow", "jaune", uid, gid)); break; + case 6: coatColor.push(l("green", "verte", uid, gid)); break; + case 7: coatColor.push(l("blue", "bleue", uid, gid)); break; + case 8: coatColor.push(l("purple", "violette", uid, gid)); break; + case 9: coatColor.push(l("pink", "rose", uid, gid)); break; + case 10: coatColor.push(l("brown", "marron", uid, gid)); break; + } + } + + for (let value of keys["eyes"]) { + operations++; + switch (value) { + case 0: eyesColor.push(l("black", "noirs", uid, gid)); break; + case 1: eyesColor.push(l("white", "blancs", uid, gid)); break; + case 2: eyesColor.push(l("gray", "gris", uid, gid)); break; + case 3: eyesColor.push(l("red", "rouges", uid, gid)); break; + case 4: eyesColor.push(l("orange", "oranges", uid, gid)); break; + case 5: eyesColor.push(l("yellow", "jaunes", uid, gid)); break; + case 6: eyesColor.push(l("green", "verts", uid, gid)); break; + case 7: eyesColor.push(l("blue", "bleus", uid, gid)); break; + case 8: eyesColor.push(l("purple", "violets", uid, gid)); break; + case 9: eyesColor.push(l("pink", "roses", uid, gid)); break; + case 10: eyesColor.push(l("brown", "marrons", uid, gid)); break; + } + } + + final = l(maneColor.join(", ") + " mane; " + coatColor.join(", ") + " coat; " + eyesColor.join(", ") + " eyes", "crinière " + maneColor.join(", ") + " ; peau " + coatColor.join(", ") + " ; yeux " + eyesColor.join(", "), uid, gid); + + if (operations > 3) { + finalTrimmed = operations + " conditions"; + } else { + if (final.length > 180) { + finalTrimmed = final.substring(0, 180) + "..."; + } else { + finalTrimmed = final; + } + } + + max = 0; + for (let match of Object.keys(sortedMatches)) { + if (sortedMatches[match] > max) max = sortedMatches[match]; + } + + exact = []; + for (let match of Object.keys(sortedMatches)) { + if (sortedMatches[match] === max) exact.push(match); + } + + return { + results: Object.keys(sortedMatches), + first: Object.keys(sortedMatchesFirst), + exact, + query: finalTrimmed, + full: final + } +}
\ No newline at end of file diff --git a/modules/registers.js b/modules/registers.js index a8f2e7f..9ed0a74 100755 --- a/modules/registers.js +++ b/modules/registers.js @@ -59,6 +59,14 @@ module.exports = [ .setAutocomplete(true)
),
new SlashCommandBuilder()
+ .setName('color')
+ .setDescription("Searches for a pony from their coat, mane or eyes color(s)")
+ .addStringOption(option =>
+ option.setName("query")
+ .setDescription("The criteria. Run the command without a query to get help.")
+ .setRequired(false)
+ ),
+ new SlashCommandBuilder()
.setName('info')
.setDescription("Gets stats and info about the bot"),
new SlashCommandBuilder()
|