mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Add global client font manager
This commit is contained in:
@@ -57,6 +57,9 @@ client.registry
|
|||||||
client.on('ready', async () => {
|
client.on('ready', async () => {
|
||||||
client.logger.info(`[READY] Logged in as ${client.user.tag}! ID: ${client.user.id}`);
|
client.logger.info(`[READY] Logged in as ${client.user.tag}! ID: ${client.user.id}`);
|
||||||
|
|
||||||
|
// Register all canvas fonts
|
||||||
|
await client.registerFontsIn(path.join(__dirname, 'assets', 'fonts'));
|
||||||
|
|
||||||
// Set up existing timers
|
// Set up existing timers
|
||||||
await client.timers.fetchAll();
|
await client.timers.fetchAll();
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@
|
|||||||
"eslint": "^7.19.0",
|
"eslint": "^7.19.0",
|
||||||
"expr-eval": "^2.0.2",
|
"expr-eval": "^2.0.2",
|
||||||
"fen-validator": "^1.4.1",
|
"fen-validator": "^1.4.1",
|
||||||
|
"font-finder": "^1.1.0",
|
||||||
"gifencoder": "^2.0.1",
|
"gifencoder": "^2.0.1",
|
||||||
"gm": "^1.23.1",
|
"gm": "^1.23.1",
|
||||||
"html-entities": "^2.1.0",
|
"html-entities": "^2.1.0",
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ const { CommandoClient } = require('discord.js-commando');
|
|||||||
const { WebhookClient } = require('discord.js');
|
const { WebhookClient } = require('discord.js');
|
||||||
const Collection = require('@discordjs/collection');
|
const Collection = require('@discordjs/collection');
|
||||||
const winston = require('winston');
|
const winston = require('winston');
|
||||||
|
const fontFinder = require('font-finder');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const Redis = require('./Redis');
|
const Redis = require('./Redis');
|
||||||
|
const Font = require('./Font');
|
||||||
const PhoneManager = require('./phone/PhoneManager');
|
const PhoneManager = require('./phone/PhoneManager');
|
||||||
const TimerManager = require('./remind/TimerManager');
|
const TimerManager = require('./remind/TimerManager');
|
||||||
const PokemonStore = require('./pokemon/PokemonStore');
|
const PokemonStore = require('./pokemon/PokemonStore');
|
||||||
@@ -33,6 +35,7 @@ module.exports = class XiaoClient extends CommandoClient {
|
|||||||
winston.format.printf(log => `[${log.timestamp}] [${log.level.toUpperCase()}]: ${log.message}`)
|
winston.format.printf(log => `[${log.timestamp}] [${log.level.toUpperCase()}]: ${log.message}`)
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
this.fonts = new Map();
|
||||||
this.redis = Redis ? Redis.db : null;
|
this.redis = Redis ? Redis.db : null;
|
||||||
this.webhook = new WebhookClient(XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, { disableMentions: 'everyone' });
|
this.webhook = new WebhookClient(XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, { disableMentions: 'everyone' });
|
||||||
this.timers = new TimerManager(this);
|
this.timers = new TimerManager(this);
|
||||||
@@ -51,6 +54,17 @@ module.exports = class XiaoClient extends CommandoClient {
|
|||||||
this.leaveMessages = leaveMsgs;
|
this.leaveMessages = leaveMsgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async registerFontsIn(path) {
|
||||||
|
const files = fs.readdirSync(path);
|
||||||
|
for (const file of files) {
|
||||||
|
const metadata = await fontFinder.get(path.join(path, file));
|
||||||
|
const font = new Font(path.join(path, file), file, metadata);
|
||||||
|
this.fonts.set(file, font);
|
||||||
|
font.register();
|
||||||
|
}
|
||||||
|
return this.fonts;
|
||||||
|
}
|
||||||
|
|
||||||
importBlacklist() {
|
importBlacklist() {
|
||||||
const read = fs.readFileSync(path.join(__dirname, '..', 'blacklist.json'), { encoding: 'utf8' });
|
const read = fs.readFileSync(path.join(__dirname, '..', 'blacklist.json'), { encoding: 'utf8' });
|
||||||
const file = JSON.parse(read);
|
const file = JSON.parse(read);
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
const { registerFont } = require('canvas');
|
||||||
|
const weights = {
|
||||||
|
100: 'thin',
|
||||||
|
200: 'extraLight',
|
||||||
|
300: 'light',
|
||||||
|
400: 'normal',
|
||||||
|
500: 'medium',
|
||||||
|
600: 'semiBold',
|
||||||
|
700: 'bold',
|
||||||
|
800: 'extraBold',
|
||||||
|
900: 'heavy',
|
||||||
|
950: 'extraBlack'
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = class Font {
|
||||||
|
constructor(path, filename, metadata) {
|
||||||
|
this.path = path;
|
||||||
|
this.name = metadata.name || filename;
|
||||||
|
this.style = metadata.style === 'regular' ? 'normal' : metadata.style || 'normal';
|
||||||
|
this.weight = weights[metadata.weight] || metadata.weight || 'normal';
|
||||||
|
this.type = metadata.type;
|
||||||
|
this.registered = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
register() {
|
||||||
|
if (this.registered) return null;
|
||||||
|
this.registered = true;
|
||||||
|
return registerFont(this.path, { family: this.name, style: this.style, weight: this.weight });
|
||||||
|
}
|
||||||
|
|
||||||
|
toCanvasString(size) {
|
||||||
|
return `${this.style} ${this.weight} ${size}px ${this.name}`;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user