UNPKG

discord-canvas-easy

Version:

Simple canvas with discord-canvas-easy

137 lines (106 loc) 4.48 kB
const Canvas = require("canvas") const Discord = require("discord.js") module.exports = class Home { constructor() { this.guild = null; this.background = null; this.user = null; this.text = null; this.colorFont = null; } /** * @param {Discord.Guild} value */ setGuild(value) { this.guild = value; return this; } /** * @param {Discord.User} value */ setUser(value) { this.user = value; return this; } /** * @param {string} value */ setBackground(value) { this.background = value; return this; } /** * @param {string} value */ setText(value) { this.text = value; return this; } /** * @param {string} value */ setColorFont(value) { this.colorFont = value; return this; } async toHome() { if(this.guild === null || this.guild.id === undefined || this.guild.id === null) throw new Error("The .setGuild must be a Discord Guild !") if(this.background === null || typeof this.background !== "string") throw new Error("The .setBackground must be a string !") if(this.user === null || this.user.id === undefined || this.user.id === null) throw new Error("The .setUser must be a Discord User !") if(this.text === null || typeof this.text !== "string") this.text = "Welcome on the server {server.name} !"; if(this.colorFont === null || typeof this.colorFont !== "string" || !this.colorFont.match(new RegExp(/^#[0-9a-f]{6}/i))) this.colorFont = "#ffffff"; const canvas = Canvas.createCanvas(800, 300) const ctx = canvas.getContext("2d") const background = await Canvas.loadImage(this.background) ctx.drawImage(background, 0, 0, canvas.width, canvas.height) const opacity = await Canvas.loadImage(`${__dirname}/../Assets/rank_black.png`) ctx.drawImage(opacity, 0, 0, canvas.width, canvas.height) const member = this.guild.members.cache.get(this.user.id) const status = member ? member.presence ? member.presence.status : "offline" : "offline"; this.text = this.text.replace(/{user.username}/g, `${this.user.username}`) this.text = this.text.replace(/{user.tag}/g, `${this.user.tag}`) this.text = await this.text.replace(/{server.name}/g, `${this.guild.name}`) this.text = await this.text.replace(/{server.memberCount}/g, `${this.guild.memberCount}`) ctx.beginPath() ctx.globalAlpha = 0.75 ctx.fillStyle = "#000000" ctx.fillRect(275, 50, 450, 50) ctx.closePath() ctx.globalAlpha = 1 ctx.font = '40px "Futura Book"' ctx.fillStyle = this.colorFont; ctx.fillText(`${this.user.username}`, 285, 90) const hashtag = await Canvas.loadImage(`${__dirname}/../Assets/hashtag.png`) ctx.drawImage(hashtag, 275, 110, 80, 80) ctx.beginPath() ctx.globalAlpha = 0.75 ctx.fillStyle = "#000000" ctx.fillRect(375, 110, 350, 80) ctx.closePath() ctx.globalAlpha = 1 ctx.font = '60px "Futura Book"' ctx.fillStyle = this.colorFont; ctx.fillText(`${this.user.discriminator}`, 385, 170) ctx.beginPath() ctx.globalAlpha = 0.75 ctx.fillStyle = "#000000" ctx.fillRect(275, 200, 450, 50) ctx.closePath() ctx.globalAlpha = 1 ctx.font = '20px "Futura Book"' ctx.fillStyle = this.colorFont; ctx.fillText(`${this.text}`, 285, 232.5) ctx.beginPath() ctx.arc(160, 150, 108, 0, 2 * Math.PI, true) ctx.closePath() ctx.fillStyle = status === "online" ? "#3ba55c" : status === "dnd" ? "#ed4245" : status === "stream" ? "#593695" : status === "idle" ? "#faa61a" : status === "offline" ? "#747f8d" : "" ctx.fill() ctx.beginPath() ctx.arc(160, 150, 100, 0, 2 * Math.PI, true) ctx.closePath() ctx.clip() const avatar = await Canvas.loadImage(member ? member.avatar ? member.avatarURL({extension: 'png'}) : this.user.displayAvatarURL({extension: 'png'}) : this.user.displayAvatarURL({extension: 'png'})) ctx.drawImage(avatar, 60, 50, 200, 200) return canvas; } }