mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Update Font system
This commit is contained in:
@@ -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}`);
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "148.0.0",
|
||||
"version": "149.0.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"private": true,
|
||||
|
||||
+2
-17
@@ -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');
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user