From 48cf20a9b627b75da8aeff7782f1ae57a5b07d22 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 4 Apr 2021 20:09:41 -0400 Subject: [PATCH] Strain Command --- commands/search/strain.js | 73 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 commands/search/strain.js diff --git a/commands/search/strain.js b/commands/search/strain.js new file mode 100644 index 00000000..811b790f --- /dev/null +++ b/commands/search/strain.js @@ -0,0 +1,73 @@ +const Command = require('../../structures/Command'); +const { MessageEmbed } = require('discord.js'); +const request = require('node-superfetch'); +const cheerio = require('cheerio'); +const { formatNumber } = require('../../util/Util'); + +module.exports = class StrainCommand extends Command { + constructor(client) { + super(client, { + name: 'strain', + aliases: ['weed', 'marijuana', 'cannabis', 'leafly', 'marijuana-strain', 'weed-strain', 'cannabis-strain'], + group: 'search', + memberName: 'strain', + description: 'Responds with information on a cannabis strain.', + clientPermissions: ['EMBED_LINKS'], + credit: [ + { + name: 'Leafly', + url: 'https://www.leafly.com/', + reason: 'API' + } + ], + args: [ + { + key: 'query', + prompt: 'What strain would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const strain = await this.search(query); + if (!strain) return msg.say('Could not find any results.'); + const effects = Object.values(strain.effects).sort((a, b) => b.score - a.score).slice(0, 3); + const embed = new MessageEmbed() + .setColor(0x017C6A) + .setAuthor('Leafly', 'https://i.imgur.com/KQ0ABhI.png', 'https://www.leafly.com/') + .setTitle(strain.name) + .setThumbnail(strain.nugImage || null) + .setDescription(strain.shortDescriptionPlain || 'No description.') + .setURL(`https://www.leafly.com/strains/${strain.slug}`) + .setFooter(strain.subtitle) + .addField('❯ Effects', effects.map(effect => effect.name)) + .addField('❯ Phenotype', data.phenotype, true) + .addField('❯ Rating', `${formatNumber(data.averageRating)} ⭐`, true); + return msg.embed(embed); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async search(query) { + const { text } = await request + .get('https://www.leafly.com/search') + .query({ + q: query, + searchCategory: 'strain' + }); + const $ = cheerio.load(text); + const data = JSON.parse($('script[id="__NEXT_DATA__"]')[0].children[0].data) + .props + .initialProps + .pageProps + .componentProps + .searchProps + .strain; + if (!data.length) return null; + return data[0]; + } +}; diff --git a/package.json b/package.json index ac7390e6..6326ed10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "134.6.2", + "version": "134.7.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true,