From ae59abaf71f14c25947d699a7bb0f2193d40b8a8 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 1 May 2024 23:14:31 -0400 Subject: [PATCH] Update Font system --- Xiao.js | 6 +++--- commands/analyze/is-it-down.js | 1 + package.json | 2 +- structures/Client.js | 19 ++----------------- structures/Tensorflow.js | 13 ++++++------- structures/{ => fonts}/Font.js | 0 structures/fonts/FontManager.js | 24 ++++++++++++++++++++++++ util/Util.js | 2 ++ 8 files changed, 39 insertions(+), 28 deletions(-) rename structures/{ => fonts}/Font.js (100%) create mode 100644 structures/fonts/FontManager.js diff --git a/Xiao.js b/Xiao.js index 2f0124e7..21d4f885 100644 --- a/Xiao.js +++ b/Xiao.js @@ -221,7 +221,7 @@ client.on('ready', async () => { // Register all canvas fonts try { - await client.registerFontsIn(path.join(__dirname, 'assets', 'fonts')); + await client.fonts.registerFontsIn(path.join(__dirname, 'assets', 'fonts')); client.logger.info('[FONTS] All fonts loaded.'); } catch (err) { client.logger.error(`[FONTS] Failed to load fonts\n${err.stack}`); @@ -243,9 +243,9 @@ client.on('ready', async () => { client.logger.error(`[ADULT SITES] Failed to fetch list\n${err.stack}`); } - // Fetch NSFW model + // Set up nsfwjs try { - await client.tensorflow.loadNSFWModel(); + await client.tensorflow.loadNSFWJS(); client.logger.info('[NSFW MODEL] Loaded NSFW model.'); } catch (err) { client.logger.error(`[NSFW MODEL] Failed to load NSFW model\n${err.stack}`); diff --git a/commands/analyze/is-it-down.js b/commands/analyze/is-it-down.js index b1a7a7ad..e8e2a253 100644 --- a/commands/analyze/is-it-down.js +++ b/commands/analyze/is-it-down.js @@ -32,6 +32,7 @@ module.exports = class IsItDownCommand extends Command { } async run(msg, { url }) { + if (!parseDomain || !ParseResultType) return msg.reply('Give me a second, still getting ready.'); const { type, domain, topLevelDomains } = parseDomain(url.hostname); if (type !== ParseResultType.Listed) return msg.reply('This domain is not supported.'); const { text } = await request diff --git a/package.json b/package.json index e87d9456..6521a3a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "148.0.0", + "version": "149.0.0", "description": "Your personal server companion.", "main": "Xiao.js", "private": true, diff --git a/structures/Client.js b/structures/Client.js index f31d5840..be13bf03 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -1,14 +1,10 @@ const CommandClient = require('../framework/Client'); const request = require('node-superfetch'); -const { Collection } = require('@discordjs/collection'); const winston = require('winston'); -const fontFinder = require('font-finder'); const moment = require('moment-timezone'); -const fs = require('fs'); -const path = require('path'); const Redis = require('./Redis'); const Tensorflow = require('./Tensorflow'); -const Font = require('./Font'); +const FontManager = require('./fonts/FontManager'); const PhoneManager = require('./phone/PhoneManager'); const TimerManager = require('./remind/TimerManager'); const PokemonStore = require('./pokemon/PokemonStore'); @@ -26,7 +22,7 @@ module.exports = class XiaoClient extends CommandClient { winston.format.printf(log => `[${log.timestamp}] [${log.level.toUpperCase()}]: ${log.message}`) ) }); - this.fonts = new Collection(); + this.fonts = new FontManager(this); this.redis = Redis ? Redis.db : null; this.timers = new TimerManager(this); this.pokemon = new PokemonStore(); @@ -38,17 +34,6 @@ module.exports = class XiaoClient extends CommandClient { this.adultSiteList = null; } - async registerFontsIn(filepath) { - const files = fs.readdirSync(filepath); - for (const file of files) { - const metadata = await fontFinder.get(path.join(filepath, file)); - const font = new Font(path.join(filepath, file), file, metadata); - this.fonts.set(file, font); - font.register(); - } - return this.fonts; - } - setTimezones() { moment.tz.link('America/Vancouver|Neopia'); moment.tz.link('America/Los_Angeles|Discord'); diff --git a/structures/Tensorflow.js b/structures/Tensorflow.js index 17712091..8a852f2b 100644 --- a/structures/Tensorflow.js +++ b/structures/Tensorflow.js @@ -8,18 +8,17 @@ module.exports = class Tensorflow { constructor(client) { Object.defineProperty(this, 'client', { value: client }); - this.nsfwModel = null; - this.faceModel = faceDetection.SupportedModels.MediaPipeFaceDetector; + this.nsfwjs = null; this.faceDetector = null; } - async loadNSFWModel() { - const nsfwModel = await nsfw.load( + async loadNSFWJS() { + const nsfwjs = await nsfw.load( `${url.pathToFileURL(path.join(__dirname, '..', 'tf_models', 'nsfw', 'web_model')).href}/`, { type: 'graph' } ); - this.nsfwModel = nsfwModel; - return this.nsfwModel; + this.nsfwjs = nsfwjs; + return this.nsfwjs; } async loadFaceDetector() { @@ -42,7 +41,7 @@ module.exports = class Tensorflow { async isImageNSFW(image, bool = true) { const img = await tfnode.node.decodeImage(image, 3); - const predictions = await this.nsfwModel.classify(img); + const predictions = await this.nsfwjs.classify(img); img.dispose(); if (bool) { const results = []; diff --git a/structures/Font.js b/structures/fonts/Font.js similarity index 100% rename from structures/Font.js rename to structures/fonts/Font.js diff --git a/structures/fonts/FontManager.js b/structures/fonts/FontManager.js new file mode 100644 index 00000000..de5e655e --- /dev/null +++ b/structures/fonts/FontManager.js @@ -0,0 +1,24 @@ +const { Collection } = require('@discordjs/collection'); +const fs = require('fs'); +const path = require('path'); +const fontFinder = require('font-finder'); +const Font = require('./Font'); + +module.exports = class FontManager extends Collection { + constructor(client, options) { + super(options); + + Object.defineProperty(this, 'client', { value: client }); + } + + async registerFontsIn(filepath) { + const files = fs.readdirSync(filepath); + for (const file of files) { + const metadata = await fontFinder.get(path.join(filepath, file)); + const font = new Font(path.join(filepath, file), file, metadata); + this.set(file, font); + font.register(); + } + return this; + } +}; diff --git a/util/Util.js b/util/Util.js index 02d6ea34..b678169b 100644 --- a/util/Util.js +++ b/util/Util.js @@ -211,6 +211,7 @@ module.exports = class Util { } static async isUrlNSFW(uri, siteList) { + if (!parseDomain || !ParseResultType) throw new Error('parse-domain still loading'); const parsed = new URL(uri); const { type, domain, topLevelDomains } = parseDomain(parsed.hostname); if (type !== ParseResultType.Listed) return null; @@ -230,6 +231,7 @@ module.exports = class Util { } static stripNSFWURLs(str, siteList, text = '[redacted nsfw url]') { + if (!parseDomain || !ParseResultType) throw new Error('parse-domain still loading'); if (!str) return ''; const uris = str.match(/(https?:\/\/\S+)/g); if (!uris) return str;